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/
Related
We have a web base tool where its database is SQL Server and we don't have an idea what could be the name of the column in database wrt field name on the web page.
We don't have access to the code. Is there any possible way to find the names in database column names?
You can use ;
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
view and you can look at the INFORMATION_SCHEMA.COLUMNS article about this view.
Following code can help in finding the column name along with the table name in the database:
use <replace with DB_Name>
GO
select * from information_schema.COLUMNS where COLUMN_NAME like '%<replace with column_name>%'
/*Also remove the angular brackets<> as well while replacing the column_name and DB_Name */
The INFORMATION_SCHEMA.COLUMNS view allows you to get information about all columns for all tables and views within a database :
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
To query for just one table you can use a query like this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTable'
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 ''?'''
I am using sql server connections in sql developer with the help of a plug-in...
Now my question is I have a list of tables which belong to sql server connections but I don't have the information on, which table belongs to which schema?
I have tried using the script
select owner, table_name
from all_tables
where table_name like 'xxxxxxxx%';
but it didn't work out, can any one please help out on this???
Thanks in advance!!!
Select schema_name (schema_id), name
from sys.tables
where name like 'your pattern'
#Ben Thul has an answer that is absolutely fine.
This is just an alternative using INFORMATION_SCHEMA (both of which essentially use sys.objects under the hood):
SELECT
t.TABLE_CATALOG
,t.TABLE_SCHEMA
,t.TABLE_NAME
,t.TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_NAME LIKE '%<YOUTABLE>%'
I somehow managed to create a table in a database with a null table schema.
I can't query the table since it has no owner, and altering the table doesn't work for the same reason.
I would alter the table using:
ALTER SCHEMA null TRANSFER dbo.SubscriptionAnswerMR
But that doesn't work.
The information_schema.tables looks like this:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
q_Profiles NULL SubscriptionAnswerMR BASE TABLE
So my question is: How do I change q_Profiles' table_schema?
SQL Server 2000 (edit)
Microsoft SQL Server Management Studio 2008R2
You should be able to verify that your table is fine by seeing the result of the following query:
SELECT u.name
FROM q_Profiles..sysobjects AS o
INNER JOIN q_Profiles..sysusers AS u
ON o.uid = u.uid
WHERE o.name = 'SubscriptionAnswerMR';
This should be dbo unless someone explicitly created them with a different owner or used sp_changeobjectowner. Which you can use if you find that sysobjects also has the wrong answer:
EXEC sp_changeobjectowner 'SubscriptionAnswerMR', 'dbo';
ALTER SCHEMA is not valid here because it was introduced in SQL Server 2005. Though it would be useful for you to describe what "doesn't work" means.
INFORMATION_SCHEMA is a horribly unreliable set of views as #Pondlife points out. Also see the following, which doesn't help you much in SQL Server 2000, but should help going forward:
The case against INFORMATION_SCHEMA views
Also as a side note you seem to be confused about tables and database. TABLE_CATALOG is the database, not the table.
Did you note this comment in the documentation?
Do not use INFORMATION_SCHEMA views to determine the schema of an
object. The only reliable way to find the schema of a object is to
query the sys.objects catalog view or use the OBJECT_SCHEMA_NAME
function.
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 ''?''
'