How do I select current database name and all the schema for the database name in SYBASE ASE using a query - sybase

How do I select current database name and all the schema for the database name in SYBASE ASE using a query
tried Select db_name
gives invalid column name

Current database:
select db_name()
By 'schema' I'm assuming you mean a user (in the database) who owns at least one object:
select distinct(user_name(uid)) from sysobjects
NOTE: I'm not sitting in front of an ASE instance at the moment so can't recall, or verify, if the 2nd query could generate a NULL ... should be easy enough to test and add an optional where clause to filter out as needed

Related

List Database Scoped DM Views in SQL Server

Server-scoped Dynamic Management View are stored only in the Master database.
SELECT name, type, type_desc
FROM sys.system_objects
WHERE name LIKE 'dm%'
ORDER BY name
How to list Database-Scoped DM Views and where it stored?
Can you advise?
Server-scoped Dynamic Management View are stored only in the Master
database.
Not exactly. DMVs are stored in the internal mssqlsystemresource database but are visible in all databases. You should get the same results if you run your query from any database, assuming you're not limited by permissions.
Database-scoped DMVs generally have prefix 'dm_db_' and can be listed with the query below.
SELECT name, type, type_desc
FROM sys.system_objects
WHERE name LIKE N'dm%[_]db[_]%'
ORDER BY name;

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?
All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
Make sure the Database is in the Linked Server if they are not on the same server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
If on same server:
SELECT * FROM [DB].[dbo].[Table]

`Invalid object name` unless I specify database name in SQL Server 2008 queries

Up until today I've been able to run queries without using the [databaseName].[dbo].[fieldName] syntax. And all of a sudden, if I use select * from myTable I get an error for an invalid object. I can't possibly think of something that happened between shutting down my PC yesterday and today. Anyone know anything about this?
Msg 208, Level, 16, State 1 Line 1
Invalid object name 'mytable'
It's only been since today that I have to include the database name in the query. There are no other connections open and no other users of this instance of SQL Server.
I think you mean a query of the form select [fieldName] from [databaseName].[dbo].[mytable]
Here are some possible things to look out for:
Make sure that you are in the correct database context / catalogue (i.e. use [databasename], or select the correct database from the Available Databases drop down in SSMS)
Ensure that if you have a case sensitive collation on your database that the object names in your query match the exact case.
Check that the default schema for your user hasn't changed on this database. Although the default schema is usually [dbo], it can be changed.
Edit : More ideas:
Do SELECT DB_NAME() to see what the current database name is.
Check to see if someone has dropped the table or view entirely, e.g. from the target database, run:
Select * from sysobjects where name = 'myobject'
OR
Select * from sys.tables where name = 'mytable'
OR
Select * from INFORMATION_SCHEMA.TABLES

get schema name

can some one pls tell me how to get the 'schema name' for a particular 'sequence' using jdbc
I assume you are using Oracle DB.
If that's the case you can run the query given below via JDBC and get the column named sequence_owner for the Schema Name to which a sequence belongs to.
SELECT sequence_owner
FROM all_sequences
WHERE sequence_name = '<YOUR_SEQUENCE_NAME>'
select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA
This is return all schema names.

How can I get a list of all of the user databases via t-sql?

I want to get a list of all of the user databases from an mssql server instance. What's the best way to do this?
I know I can select from sys.databases, but I don't see any way to filter out system databases besides hardcoding a list of names to exclude.
I need the script to work on 2000/2005 and 2008.
If the approach I listed above is the only way to go, what are list of names I should exclude? I don't know if 2005 or 2008 added any new system databases off the top of my head.
Was looking in to this again today and decided to profile what Management Studio was doing to populate the Object Explorer details.
Turns out the solution Microsoft have implemented is pretty simplistic and boils down to the following:
SELECT *
FROM master.sys.databases
WHERE Cast(CASE WHEN name IN ('master', 'model', 'msdb', 'tempdb') THEN 1 ELSE is_distributor END As bit) = 0
Please note that this was performed using SSMS 2008R2 (10.50.4033.0).
The first query will return a table with data regarding all of the databases on the instance:
Select *
From sys.databases
From this table you'll notice you can narrow down the scope of data you're looking for by using the WHERE clause. For example, the following queries will essentially return the same result table (the one you're most likely looking for):
Select *
From sys.databases
Where database_id > 5
Select *
From sys.databases
Where len(owner_sid)>1
These queries will work in SQL Server 2008 and 2012.
On SQL Server 2008 R2 Express, looks like I cannot reliably use any of the above methods. INFORMATION_SCHEMA.SCHEMATA only shows me information in the current database, db_id (database_id) #5 is my first user database, and owner_sid on two of my user databases on one of my mirrored databases (running on SQL Server 2008 R2 Standard) shows owner_sid = 1 for my two most recently created databases. (PablolnNZ's comment above is correct: I did not set an explicit owner for those two databases so it still shows as having an owner of 'sa'.)
The only reliable means I was able to use was the following:
SELECT name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'tempdb', 'msdb', 'Resource')
This works in 2005, not 100% sure about the other versions but I think it will fly. It's a bit of a hack but might get you what you need:
select * from sys.databases where len(owner_sid)>1
As nasty as it sounds to hardcode things. The names and number of system databases has been fairly consistent for several versions of SQL. However, if that is too unpleasant you could semi-hardcode them into a table and then plug that into your query.
Not sure if you can offhand. One note -- on 2k you'll have to use master.dbo.sysdatabases and not master.sys.databases (which doesn't exist in 2k).
Starting with SQL Server 2008 you have access to a view called sys.databases which when joined with sys.server_principals can eliminate the databases owned by sa, which you can (most often) safely discern are the "system databases". Thus, allowing you to filter these items out.
select
d.name
,d.database_id
from
sys.databases d
join
sys.server_principals p
on p.sid = d.owner_sid
where
p.name <> 'sa';

Resources