Statement [SQL] declare @ tablenamenchar (20) declare @ fieldnamenchar (50) declare @ SQLStringnvarchar (300) Statements for querying all tables in the database -- declare cursor; -- game
Statement [SQL] declare @ tablename nchar (20) declare @ fieldname nchar (50) declare @ SQLString nvarchar (300) DECLARE tnames_cursor cursor local FORWARD_ONLY READ_ONLY -- DECLARE the cursor for select name from sysobjects where type = U; -- game
Query statements of all tables in the database
[SQL]
Declare @ tablename nchar (20)
Declare @ fieldname nchar (50)
Declare @ SQLString nvarchar (300)
DECLARE tnames_cursor cursor local FORWARD_ONLY READ_ONLY -- DECLARE the CURSOR
FOR select name from sysobjects where type = 'U'; -- column required FOR cursor
Open tnames_cursor -- open the cursor
Fetch next from tnames_cursor INTO @ tablename -- move the cursor to assign values to the variable. It should correspond to the columns required by the cursor, and the sequence type should be consistent.
WHILE (@ FETCH_STATUS = 0) -- loop
BEGIN
BEGIN
Set @ fieldname = 'Count' + @ tablename
SET @ SQLString = n' select count (*) as '+ @ fieldname + 'from' + @ tablename + 'having count (*)> 100 ';
EXECUTE dbo. sp_executesql @ SQLString
END
Fetch next from tnames_cursor INTO @ tablename -- move the cursor to assign a value to the variable which corresponds to the columns required by the cursor. The sequence type should be consistent.
END
CLOSE tnames_cursor -- CLOSE the cursor
DEALLOCATE tnames_cursor -- release cursor