Friday, March 27, 2009

Cursor for get all the table name in database

This cursor is used for get all the table name form database.
and we can simply add the sql commands in this cursor. as
I shown in Print Statement.

Declare @t varchar (1024)
Declare tbl_cur cursor for
select TABLE_NAME from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME
OPEN tbl_cur
FETCH NEXT from tbl_cur INTO @t
WHILE @@FETCH_STATUS = 0
BEGIN
-- print ('sp_rename '+ @t +','+@t+'Notused')
print ('select * from '+ @t )
print 'GO'
FETCH NEXT from tbl_cur INTO @t
END
CLOSE tbl_cur
DEALLOCATE tbl_Cur

Thanks
HELPONDESK TEAM

No comments: