sql - Conditional clause to DROP Table -
sql - Conditional clause to DROP Table -
i trying write sql procedure drop table have pattern in names. below code :
declare @temp table ( id bigint identity(1,1), tabname sysname not null ) insert @temp select table_name information_schema.tables table_name '%:%' declare @processedid bigint = 0 declare @tablename sysname select @processedid = id, @tablename = tabname @temp id > @processedid order id desc while(@processedid not null) begin drop table dbo.[@tablename] select @processedid = id, @tablename = tabname @temp id > @processedid order id desc end
but @tablename not replaced right table name. can 1 point me in right direction.
you need dynamically when want utilize variable name argument, need wrap in string , execute string , little changes while status fit.
i'd this:
declare @temp table ( id bigint identity(1,1), tabname sysname not null ): insert @temp select table_name information_schema.tables table_name '%:%'; declare @tablename varchar(255); declare @count int = isnull((select count(*) @temp t), 0); while(@count > 0) begin select @tablename = tabname @temp t; --if order of drop not important exec('drop table dbo.[' + @tablename+ ']'); delete @temp tabname = @tablename; set @count = isnull((select count(*) @temp t), 0); end
sql
Comments
Post a Comment