if these tables do not have any foreign keys, then instead of 'DELETE ' you can use TRUNCATE TABLE
DECLARE @thisTableName varchar(255)
DECLARE my_Cursor CURSOR FOR
SELECT [name] FROM sys.objects where [name] like 'z_%';
OPEN my_Cursor;
FETCH NEXT FROM my_Cursor INTO @thisTableName;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC('TRUNCATE TABLE '+@thisTableName);
FETCH NEXT FROM my_Cursor INTO @thisTableName;
END;
CLOSE my_Cursor;
DEALLOCATE my_Cursor;
GO
Main Topics
Browse All Topics





by: aneeshattingalPosted on 2009-08-16 at 21:21:12ID: 25111605
you need dynamic sql here
DECLARE @thisTableName varchar(255)
DECLARE my_Cursor CURSOR FOR
SELECT [name] FROM sys.objects where [name] like 'z_%';
OPEN my_Cursor;
FETCH NEXT FROM my_Cursor INTO @thisTableName;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC('DROP TABLE '+@thisTableName);
FETCH NEXT FROM my_Cursor INTO @thisTableName;
END;
CLOSE my_Cursor;
DEALLOCATE my_Cursor;
GO