Pages

Sunday, September 18, 2011

SQL WHILE Begin End

USE CommunityUseOfPublicSchools;





DECLARE @name VARCHAR(MAX);
DECLARE @sql VARCHAR(MAX);



DECLARE table_cursor CURSOR FOR
SELECT  ST.name FROM sys.tables ST



OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN

SET @sql= 'drop table "'+@name+'" ';
EXECUTE(@sql);

FETCH NEXT FROM table_cursor INTO @name
END
CLOSE table_cursor
 DEALLOCATE table_cursor

No comments:

Post a Comment