Created
February 2, 2018 17:01
-
-
Save cbertolasio/622aab228f470ca173ec47c405be5164 to your computer and use it in GitHub Desktop.
cursor to drop tables matching a given name in sql server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select name | |
from [tst-IrrigationReporting].sys.tables | |
where name like 'YourTableName_2018%' | |
-- and is_ms_shipped = 0; | |
DECLARE @cmd varchar(4000) | |
DECLARE cmds CURSOR FOR | |
SELECT 'drop table [' + Table_Name + ']' | |
FROM INFORMATION_SCHEMA.TABLES | |
WHERE Table_Name LIKE 'YourTableName_2018%' | |
OPEN cmds | |
WHILE 1 = 1 | |
BEGIN | |
FETCH cmds INTO @cmd | |
IF @@fetch_status != 0 BREAK | |
EXEC(@cmd) | |
END | |
CLOSE cmds; | |
DEALLOCATE cmds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment