Sunday, 18 August 2013

Print DATABASES and NO OF TABLES in each DATABASE--using CURSOR

------------CREATE PROC for Print DATABASES and NO OF TABLES in each DATABASE---------

ALTER PROC concatination
AS
BEGIN
DECLARE @qry varchar(100)
DECLARE @NAME VARCHAR(MAX)=''
DECLARE c1 CURSOR FOR SELECT name FROM sys.databases
OPEN C1
FETCH NEXT FROM C1 INTO @NAME
WHILE(@@FETCH_STATUS<>-1)
BEGIN
set @qry= 'select '+char(39)+@name+char(39)+',count(*) from '+@name+'.sys.tables'
--EXEC(@qry)
fetch next from c1 into @name
PRINT @qry
END
CLOSE C1
DEALLOCATE C1
END
EXEC concatination

No comments:

Post a Comment

EMP, DEPT Sample script

/****** Object:  Table [dbo].[DEPT]    Script Date: 19-05-2016 06:58:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET A...