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 ''?''
'
Related
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/
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'
i built website that uses sql server and have this sql for deleting items by id that im getting from querystring:
DELETE FROM tablename WHERE GUID = 'param'
is it possible to inject sql that will return db_name() for example?
i know that i can inject sql only for select statements something like this
select name from tablename where 'parems'
union all
select db_name()
but what about delete statements
i know that i can drop table insert to table, but in this stage i need know if i can get kind of data, for ex.: db_name()
Sure. This value of param:
' OR ''='
will result in this statement:
DELETE FROM tablename WHERE GUID ='' OR '' = ''
which will delete all data in the table.
Yes, a query always returns a result, so if the database driver allows it you can simply add another query after the delete.
An input like this:
x'; select db_name() --
would give:
DELETE FROM tablename WHERE GUID = 'x'; select db_name() --'
You should read the wiki & MS docs on SQL Injection
I think your asking if its possible using purely delete statement. If so, its possible by adding the OUTPUT clause like so:
DELETE Sales.ShoppingCartItem
OUTPUT DELETED.*
WHERE ShoppingCartID = 20621;
This will output the deletes rows.
To avoid SQL injections, you should use SQL parameters
I have multi databases with same structure its name like that "Client1234" the different in numbers beside "client" i have table called "Transactions" inside each database and i want to run query to get count all raws in "transactions" table in all databases.
also when i select database i need to check it has the client word and it has numbers beside the word.
Try to use sp_msforeachdb stored procedure like so:
create table #temp ([rows] int, [client] varchar(100))
exec sp_msforeachdb '
if ''?'' like ''Client%'' and exists(select * from ?.sys.tables t where t.name = ''Transactions'')
begin
insert into #temp select count(*), ''?'' from ?..Transactions
end
'
select * from #temp
drop table #temp
You can use dynamic SQL to create these queries:
select 'select count(*) from ' + name + '.dbo.transactions'
from master..sysdatabases
where name like 'Client%'
and isnumeric(substring(name,6,1))
This would return a result set with each row being a SQL query to count a specific database. It could be consumed by a programming language, used as a cursor, etc.. If you provide more detail I may be able to provide a better example.
When using Fosco's method, it is a good idea to put in brackets [] around the database name:
SELECT 'SELECT count(*) FROM ' + '[' + name + ']' + '.dbo.transactions'
FROM master..sysdatabases
WHERE name like 'Client%' and isnumeric(substring(name,6,1))
If the name and number of the databases you wish to query is not known beforehand then you can only do this by using a dynamic query. You'll need to generate a script like
SELECT COUNT(*) FROM Client1.dbo.Transactions
SELECT COUNT(*) FROM Client2.dbo.Transactions
...
Of course you need to have your appropriate permissions in place for each database.
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