SQL get max id value from all tables in all databases - sql

I want to get the database name, schema name, table name and max id value from all tables in all databases. Using following query I get the DBname, Schema and TableName but cannot figure out how to get the max(id) value also into the table.
DECLARE #DBName VARCHAR(256)
DECLARE #varSQL VARCHAR(256)
DECLARE #getDBName CURSOR
SET #getDBName = CURSOR FOR SELECT name FROM sys.databases
CREATE TABLE #TmpTable (
DBName VARCHAR(256),
SchemaName VARCHAR(256),
TableName VARCHAR(256)
)
OPEN #getDBName
FETCH NEXT FROM #getDBName INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #varSQL = 'USE ' + #DBName + ';
INSERT INTO #TmpTable
SELECT '''+ #DBName + ''' AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM sys.tables'
EXEC(#varSQL)
FETCH NEXT FROM #getDBName INTO #DBName
END
CLOSE #getDBName
DEALLOCATE #getDBName
SELECT * FROM #TmpTable
DROP TABLE #TmpTable

SELECT
'#DBName' AS DBName,
IDENT_CURRENT(Name) MaxId,
SCHEMA_NAME(Schema_ID) AS SchemaName,
name AS TableName
FROM sys.Tables

Related

SQL query to find all references of a particular column in a database?

I need to write some SQL to find all references of a particular column in a database. The column that I'm trying to find references to exists in a different databases. I've found a few examples of finding references of a column that exist in the same database:
In SQL Server, how can I find everywhere a column is referenced?
But I'm having problems figuring out how to do this for a column that exists in a different database. Can you provide the SQL for this? For example purposes, let's refer to the external column I'm trying to find as:
MyExternalDB.MyExternalSchema.MyExternalTable.MyExternalColumn
Ok, just run this and make sure you set your ColumnName variable
USE [master];
GO
IF OBJECT_ID('tempdb..#columns') IS NOT NULL
DROP TABLE #columns;
GO
CREATE TABLE #columns
( databaseName nvarchar(MAX),
columnid int,
columnName nvarchar(MAX),
objectid int,
objectName nvarchar(MAX));
DECLARE #databaseName sysname;
DECLARE #columnName nvarchar(MAX) = 'ColumnName';
DECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC FOR
SELECT [name]
FROM [sys].[databases]
WHERE [state] = 0
AND [name] NOT IN ( 'tempdb', 'master', 'msdb', 'model' );
OPEN cur;
FETCH NEXT FROM cur
INTO #databaseName;
WHILE ( ##FETCH_STATUS != -1 )
BEGIN;
IF ( ##FETCH_STATUS != -2 )
BEGIN;
DECLARE #statement nvarchar(MAX);
SET #statement =N'Use '+ #databaseName +
N';
if EXISTS (SELECT name FROM sys.[columns] WHERE name = ''' + #columnName + ''')
BEGIN;
INSERT [#columns] ( [databaseName], [columnid], [columnName], [objectid], [objectName] )
SELECT ''' + #databaseName + N''',
c.[column_id],
c.[name],
o.[object_id],
o.[name]
FROM sys.[columns] c
INNER JOIN sys.[objects] o
ON [o].[object_id] = [c].[object_id]
WHERE c.[name] = ''' + #columnName + ''';
END;';
EXEC [sys].[sp_executesql] #stmt = #statement;
END;
FETCH NEXT FROM cur
INTO #databaseName;
END;
CLOSE cur;
DEALLOCATE cur;
SELECT * FROM [#columns];

SQL Server look for stored procedure

I'm using SQL Server Management Studio and I have multiple server connections and I need to know where a specific stored procedure exists in every server which contains multiple databases!
Looking for a query to run on each server
Here is a little script that you could run on each server.
I know that it uses a while loop, which is generally not preferred, someone might be able to improve on it.
Run the script from the master database.
SQL Query:
set nocount on
declare #procname varchar(150) = '<SPROCNAME_TO_FIND>' -- SET THIS TO THE STORED PROCEDURE NAME YOU ARE LOOKING FOR
declare #alldbonserver table
( databasename varchar(150),
doneflag bit )
declare #foundtbl table
( countcol int,
dbname varchar(150) )
declare #errortbl table
( dbname varchar(150) )
insert #alldbonserver
( databasename,
doneflag )
select sdb.name,
0
from sys.databases sdb
declare #curdbname varchar(150),
#sqlcmd varchar(max)
while exists (select 1
from #alldbonserver
where doneflag = 0)
begin
select top 1
#curdbname = databasename
from #alldbonserver
where doneflag = 0
select #sqlcmd = 'select distinct 1, ''' + #curdbname + ''' as dbname from ' + #curdbname + '.sys.objects where type = ''P'' and name = ''' + #procname + ''''
begin try
insert #foundtbl
( countcol, dbname )
exec(#sqlcmd)
end try
begin catch
insert #errortbl
values ( #curdbname )
end catch
update #alldbonserver
set doneflag = 1
where databasename = #curdbname
end
select dbname as 'Databases with stored procedure'
from #foundtbl
select dbname as 'Unable to access databases'
from #errortbl
set nocount off
Sample output:
Databases with stored procedure
-----------------------------------------
MainDatabase
SomeOtherDatabase
Unable to access databases
-----------------------------------------
model
ReportServer$SQLTemp
VeryPrivateDatabase
If you are using SSMS, you could create a server registration group that contains all of the servers you connect to, right click on the group and run this query against the group: exec sp_MSforeachdb 'select * from [?].sys.procedures where name=''<your_name_here>'''
This query will enumerate each of the databases on each of the servers in the group (note: the single quotes are like that for a reason...)
I just put this together on SQL2012. It should return all Stored Procs on a Server for a given name. If you leave #SPName blank, you will get all of them.
The Query utilizes sys.all_objects so you could easily change it to work the same for tables, functions, any or all db objects.
DECLARE #SPName VARCHAR(256)
SET #SPName = 'My_SP_Name'
DECLARE #DBName VARCHAR(256)
DECLARE #varSQL VARCHAR(512)
DECLARE #getDBName CURSOR
SET #getDBName = CURSOR FOR
SELECT name
FROM sys.databases
CREATE TABLE #TmpTable (DBName VARCHAR(256),
SchemaName VARCHAR(256),
SPName VARCHAR(256))
OPEN #getDBName
FETCH NEXT
FROM #getDBName INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #varSQL = 'USE [' + #DBName + '];
INSERT INTO #TmpTable
SELECT '''+ #DBName + ''' AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS SPName
FROM sys.all_objects
WHERE [type] = ''P'' AND name LIKE ''%' + #SPName + '%'''
EXEC (#varSQL)
FETCH NEXT
FROM #getDBName INTO #DBName
END
CLOSE #getDBName
DEALLOCATE #getDBName
SELECT *
FROM #TmpTable
DROP TABLE #TmpTable

how to check same Object Name across databases on the same server

I was trying to find duplicate object name across all databases on one server. Is there away to find all duplicate? I am using cursor to loop through the databases and I want to check if an object shows more than one and column called duplicates will be populated with Yes or No.
use the msforeachdb builtin function/proc, then insert those results into a temp table. Then query the temp table and do a groupby having count(1) > 1
create table #tables (db varchar(100) not null, name varchar(100) not null)
exec sp_msforeachdb 'use [?] insert into #tables select distinct db_name(), [name] from sys.objects where [type] = ''U'''
select name, count(1) from #tables group by name having count(1) > 1 order by 2 desc
drop table #tables
This isn't exact, but you get the idea. I'll keep tweaking until I know it's right.
Here's the cursor version:
set nocount on
SELECT name
into #dbs
FROM sys.databases
declare dbCursor cursor local forward_only for
select name from #dbs
declare #db varchar(100)
create table #tables (db varchar(100) not null, name varchar(100) not null)
open dbCursor
fetch next from dbCursor into #db
while (##fetch_status = 0) begin
begin try
exec ('use ' + #db + ' insert into #tables select distinct db_name(), [name] from sys.objects where [type] = ''U''')
print 'got ' + ##rowcount + ' from db ' + #db
end try
begin catch
print 'couldn''t retrieve data from database ' + #db
end catch
fetch next from dbCursor into #db
end
close dbCursor
deallocate dbCursor
select name, count(1) from #tables group by name having count(1) > 1 order by 2 desc
drop table #tables
drop table #dbs

Display all the names of databases containing particular table

I have many databases in my SQL Server.
I have to just search for database names containg particular table name Heartbitmaster
I have many databases such as Gotgold, DVD, etc and I just want to find database names from query that contain this table Heartbitmaster.
I searched I tried for query:
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'base table'
AND table_schema = 'Heartbitmaster'
but it didn't work.
I searched further and came across:
SELECT name, database_id, create_date
FROM sys.databases
but dont know how to arrange further where condition for search of table name
Please help me.
I got it done through following query:
SELECT name
FROM sys.databases
WHERE CASE WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[heartbit]', 'U')
END IS NOT NULL
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%tablename%'''
try this one
I needed something slightly different.
This will return all tables and their corresponding DBs with names containing the supplied string:
SELECT TABLE_NAME, TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%_<insert_name_here>';
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
As for the INFORMATION_SCHEMA or the SQL Server specific catalog views: as far as I know, those are always constrained to the current database you're in - so you cannot search across all databases on your server. SQL Search does this for you - by searching into every single database on the server.
Create Procedure as bellow
CREATE PROCEDURE usp_FindTableNameInAllDatabase
#TableName VARCHAR(256)
AS
DECLARE #DBName VARCHAR(256)
DECLARE #varSQL VARCHAR(512)
DECLARE #getDBName CURSOR
SET #getDBName = CURSOR FOR
SELECT name
FROM sys.databases
CREATE TABLE #TmpTable (DBName VARCHAR(256),
SchemaName VARCHAR(256),
TableName VARCHAR(256))
OPEN #getDBName
FETCH NEXT
FROM #getDBName INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #varSQL = 'USE ' + #DBName + ';
INSERT INTO #TmpTable
SELECT '''+ #DBName + ''' AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM sys.tables
WHERE name LIKE ''%' + #TableName + '%'''
EXEC (#varSQL)
FETCH NEXT
FROM #getDBName INTO #DBName
END
CLOSE #getDBName
DEALLOCATE #getDBName
SELECT *
FROM #TmpTable
DROP TABLE #TmpTable
GO
EXEC usp_FindTableNameInAllDatabase 'Address'
GO
exec usp_FindTableNameInAllDatabase 'user'
It Works!!!!!!!
Run this query for finding database name for a particular table
The paste table name in #tablename
Drop table #tempo for next time run
declare #tablename varchar(max) = 'patient'
declare #count int = (select max(database_id) FROM sys.databases)
declare #n int = 1
declare #dbname varchar(max)
declare #query nvarchar(max)
create table #tempo(Databasename varchar(max), tablename varchar(max))
while #n <= #count
begin
select #dbname = name from sys.databases where database_id = #n and service_broker_guid <> '00000000-0000-0000-0000-000000000000'
set #query = 'insert into #tempo(Databasename,tablename) select '''+#dbname+''' [Database],name from '+#dbname+'.sys.tables where name like ''%'+#tablename+'%'''
exec(#query)
set #n=#n+1;
end
select * from #tempo
At the last giving error like
Database 'databasename' cannot be opened because it is offline.
then run this query separately
select * from #tempo
I Complete sp_MSforeachdb and create a procedure like this :
create PROCEDURE findTableName
#tablename nvarchar(max)
AS
BEGIN
declare #sqlCommand nvarchar(max)
CREATE TABLE #t (DBName VARCHAR(256),SchemaName VARCHAR(256),TableName VARCHAR(256))
set #sqlCommand='insert into #t (DBName,SchemaName,TableName) exec sp_MSforeachdb ''SELECT "?" AS DB, SCHEMA_NAME(schema_id) AS SchemaName ,name FROM [?].sys.tables WHERE name like ''''%'+#tablename+'%'''''''
--print #sqlCommand
exec ( #sqlCommand)
select * from #t
END

Find database location of a table

I have several databases in my SQL Server 2008. And I forgot where some tables from, so I need to ask if there's a such query that finds the database location of a certain table?
I need something goes like this:
SELECT DATABASE_NAME FROM SQLSERVER WHERE TABLE= "TBL_PRODUCTS"
Addition:
Now, i need to know the database location of certain Views and Stored Procedures
Something like this:
SELECT DATABASE_NAME FROM SQLSERVER WHERE VIEW= "VW_PRODUCTS"
SELECT DATABASE_NAME FROM SQLSERVER WHERE StoredProcedure= "SP_PRODUCTS"
Thanks!
The following procedure will do the job:
CREATE PROCEDURE usp_FindTableNameInAllDatabase
#TableName VARCHAR(256)
AS
DECLARE #DBName VARCHAR(256)
DECLARE #varSQL VARCHAR(512)
DECLARE #getDBName CURSOR
SET #getDBName = CURSOR FOR
SELECT name
FROM sys.databases
CREATE TABLE #TmpTable (DBName VARCHAR(256),
SchemaName VARCHAR(256),
TableName VARCHAR(256))
OPEN #getDBName
FETCH NEXT
FROM #getDBName INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #varSQL = 'USE ' + #DBName + ';
INSERT INTO #TmpTable
SELECT '''+ #DBName + ''' AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM sys.tables
WHERE name LIKE ''%' + #TableName + '%'''
EXEC (#varSQL)
FETCH NEXT
FROM #getDBName INTO #DBName
END
CLOSE #getDBName
DEALLOCATE #getDBName
SELECT *
FROM #TmpTable
DROP TABLE #TmpTable
GO
EXEC usp_FindTableNameInAllDatabase 'Address'
GO
The result would look like that:
Source
Do this
Use Master
Go
select
'select '''+CAST(name as varchar(200))+''' from '+CAST(name as varchar(200))+'.sys.tables where name = yourTableName'
from sysdatabases
replacing yourTableName with the name of your table, with ' '
You will get selects; then run those and will get the results.
If there are more than 100 databases, use results to text, because to grid, you will only get 100 selects maximum