我试图找出代码来生成遍历表内容的循环并检查它是否仍然是有效列并删除所有不再有效的列。
declare @counter int
declare @col_count int
declare @col_name char(100)
declare @temp_sql char(100)
set @counter = 0
SELECT @col_count = COUNT(*) from sys.columns
where Object_ID = Object_ID(N'test')
AND name <> 'pk_test'
while @counter < @col_count
begin
with CTE as (
select name, row_number() over (order by column_id) as ColNo
from sys.columns where object_id = object_id('test') AND name <> 'pk_test'
)
select @col_name = name from CTE where ColNo = ((@counter) + 1)
SET @temp_sql = 'ALTER TABLE test DROP column ' + @col_name + ';'
EXECUTE (@temp_sql);
set @counter = @counter + 1
end
由于某种原因,EXECUTE 导致代码跳过所有其他调用,但如果我用注释取出执行并打印,它将显示所有有效的 drop 语句。
我试过这样做:
SET @temp_sql = @temp_swl + 'ALTER TABLE test DROP column ' + @col_name
enter code here
我试图一次合并所有的变更表,但它似乎不起作用。
我觉得我错过了一些小东西!