Monday, October 05, 2009

Get Tables has records

DECLARE @TableName VARCHAR(500)

DECLARE table_cursor CURSOR
FOR

SELECT [Name] FROM
sys.tables
order
by [Name]


 

TRUNCATE
TABLE __ImportTables

OPEN table_cursor

FETCH
NEXT
FROM table_cursor INTO @TableName

WHILE
@@FETCH_STATUS
= 0

BEGIN

    DECLARE @SQLString NVARCHAR(2000)

    DECLARE @ParmDefinition nvarchar(500);


 

    DECLARE @Count int

    SET @SQLString =
'SELECT @Count_Out = COUNT(*) FROM '
+ @TableName

    SET @ParmDefinition = N'@Count_Out int OUTPUT';


 

    EXECUTE
sp_executesql @SQLString, @ParmDefinition, @Count_Out=@Count OUTPUT;


 

    IF @Count > 0

        INSERT __ImportTables(TableName)
Values
(@TableName)


 

    FETCH
NEXT
FROM table_cursor INTO @TableName

END


 

CLOSE table_cursor

DEALLOCATE table_cursor


 


 


 

0 comments: