Skip to content

Instantly share code, notes, and snippets.

@cbertolasio
Created February 2, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbertolasio/622aab228f470ca173ec47c405be5164 to your computer and use it in GitHub Desktop.
Save cbertolasio/622aab228f470ca173ec47c405be5164 to your computer and use it in GitHub Desktop.
cursor to drop tables matching a given name in sql server
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