SQL Server query to find all current database names - sql-server

I need a SQL query to find the names of existing databases.

Here is a query for showing all databases in one Sql engine
Select * from Sys.Databases

SELECT name
FROM sys.databases
You'll only see the databases you have permission to see.

Another to add to the mix:
EXEC sp_databases

I don't recommend this method... but if you want to go wacky and strange:
EXEC sp_MSForEachDB 'SELECT ''?'' AS DatabaseName'
or
EXEC sp_MSForEachDB 'Print ''?'''

You can also use these ways:
EXEC sp_helpdb
and:
SELECT name FROM sys.sysdatabases
Recommended Read:
Don't forget to have a look at sysdatabases VS sys.sysdatabases
A similar thread.

This forum suggests also:
SELECT CATALOG_NAME AS DataBaseName
FROM INFORMATION_SCHEMA.SCHEMATA

For people where "sys.databases" does not work,
You can use this aswell;
SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.COLUMNS

SELECT datname FROM pg_database WHERE datistemplate = false
#for postgres

Related

How to find database name of any table

How to find the database name for a particular table when we have created a table and we forgot the database where we have created the same.
I have found the solution, we can simply find a database where we have created the table.
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'tablename'
Try this EXEC sp_MSforeachdb
EXEC sp_MSforeachdb 'USE ? SELECT sc.TABLE_CATALOG, sc.TABLE_SCHEMA, sc.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS sc WHERE TABLE_NAME=''YourTableName'''
if you are using SQL Server 2012 or more then simply you can find DataBase name using INFORMATION_SCHEMA like
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME ='your_table_name'
Here TABLE_CATALOG column shows DataBase Name.
There is no SQL Server ready solution to your problem.
To resolve it you need to loop through each database and to put the information schema into new table and then to query it.
Some idea can be found in the site bellow:
https://blog.sqlauthority.com/2008/04/29/sql-server-find-table-in-every-database-of-sql-server/

In SQL Server, how to find the database name when I know only a function name

I know only the function name. I don't know the database name where the function is been created and present.
I have around 30 databases so it takes more time to run the function on all the databases to find the database. is there any simple way to get the database name using only function name.
i'm using SQL server. Thanks in advance.
Surprisingly Jeroen's code didn't work on my SQL 2014 server. Try the following.
EXEC sp_msforeachdb
'if exists(select 1 from [?].sys.objects where name=''YourProcHere'')
select ''?'' as DbName from [?].sys.objects where name=''YourProcHere'''
Pretty sure this works as well:
sp_msforeachdb 'USE ? select ''?'' DBNAME where object_id(N''YOUR_FUNCTION'') is not null'
Thanks #Jeroen Mostert, Below SQL works for me:
sp_msforeachdb 'IF OBJECT_ID(''[?].dbo.myfunction'') IS NOT NULL SELECT ''?'''

How does a table not being in information_schema or systables happen?

I often run into MSSQL databases that have many more tables than are listed in information_schema or systables.
For example, I'm querying a database right now but only getting the tables spt_fallback_db, spt_fallback_dev, spt_fallback_usg, spt_monitor, spt_values. (1)
How does this happen?
And - can it be fixed easily?
(1) I should clarify that this isn't a permissions issue, as I am sysadmin on the database ; there are around 200 tables and I have full permission on all of them.
#DeadZone was right on the money, the query had some issues.
I was using:
DECLARE #command varchar(1000)
SELECT #command = 'SELECT * FROM sysobjects WHERE xtype=''U'' '
EXEC sp_MSforeachdb #command
But it would only show system tables. So then I switched to a more direct query to see what was going on and was able to view the tables:
use MYDATABASENAME;
SELECT * FROM sysobjects WHERE xtype='U'

What's the equivalent of "Show Tables" in SQSH

How can I get sqsh to tell me which tables are available?
Does sp_tables work for you? Are you trying to get tab completion when creating a query?
After some help from this site and some trial and error:
select table_name from systable
go
Painfully enough, sp_help doesn't exist in my version.
Newer version use sysobjects:
SELECT name FROM sysobjects WHERE type = 'U';
Regards,
I am not familiar with systables. What flavor of Sybase are you running? ASA perhaps?
Please find appended a sqsh function (which you can put in your .sqshrc) which demonstrates some querying of the ASE (Adaptive Server Enterprise) catalog tables and the use of the Ed Barlow system stored procedure library http://www.edbarlow.com/gem/procs_only/index.htm to figure out what objects are in a database.
# Shorthand for sp__helptext or sp__revtable
\func -x ?
IF EXISTS (SELECT * FROM sysobjects WHERE name = \\'${1}\\')
BEGIN
DECLARE #type VARCHAR(3)
SELECT #type = type FROM sysobjects WHERE name = \\'${1}\\'
IF #type IN (\\'U\\')
exec sp__revtable ${1}
ELSE
exec sp__helptext ${1}
END
ELSE
-- default to sp__ls (which can list partial matches) if an exact match wasn't found in sysobjects
exec sp__ls ${1}
go
\done

Find a table across multiple databases SQL SERVER 2005

I exported a table to a server but I can't find the table. Maybe I didn't put the right destination database. How can I find this table if my server has multiple databases, without opening each one of them?
I use MS Sql Server Management Studio 2008.
Rough and dirty, but it would do the job.
-- Instructions. Replace "table_name_here" with actual table name
sp_MSforeachdb 'USE ?
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N''[table_name_here]'') AND OBJECTPROPERTY(id, N''IsUserTable'') = 1)
BEGIN
PRINT ''Found in db ?''
END'
One way
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'table_name'
Or if you are reasonably confident it would be in the dbo schema in whichever database
SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[table_name]', 'U')
END IS NOT NULL
Based off Martin Smith's answer above but generalised into a view to give a sort of cross-DB version of sys.tables -
CREATE VIEW ListTablesAllDBs
AS
SELECT
DB_NAME(database_id) as DBName,
OBJECT_SCHEMA_NAME(object_id,database_id) as SchemaName,
OBJECT_NAME(object_id,database_id) as TableName
FROM
[sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
Now, if only I can work out a way to do the same for columns.......
EDIT - Ignore this, finding it sometimes misses tables altogether.
Minor clarification just to avoid headaches for those with 'SuperUsers' who don't know how to name DBs:
EXEC sp_MSForEachDB '
USE [?]
IF OBJECT_ID(''mytable'') IS NOT NULL AND
OBJECTPROPERTY(OBJECT_ID(''mytable''), ''IsTable'') = 1
PRINT ''Found here: ?'''
select 'select * from '+name+'.sys.tables where name=
''[yourtable]'';' from sys.databases
Instead of [yourtable], type the name of the missing table, and run the result again.
EXEC sp_MSForEachDB '
USE ?
IF OBJECT_ID(''mytable'') IS NOT NULL AND
OBJECTPROPERTY(OBJECT_ID(''mytable''), ''IsTable'') = 1
PRINT ''?''
'

Resources