我目前正在构建一个存储过程,我的问题是我批量插入一个新表,然后比较与旧表的不同之处(如果它存在。它基于其唯一ID)。
我在此存储过程中运行动态查询,以便将所述表的旧版本的名称作为参数传递。但是,当我选择表格,或者尝试选择插入,插入等等时,表格将返回空白。这使得无法检查导入的新表中消失的行。旧的,loc。' + @ tablename +'表已存在于db上,并且在存储过程结束之前不会被删除。我这样做的订单有什么问题吗?我很茫然。此动态查询在单个语句(不是SP)中工作。我真正努力的这个查询的唯一部分是开始,选择原始的loc。表。当我打印脚本时,它看起来应该运行,表名称正确传递。
IF EXISTS (select distinct MerchantName from #Temp where Merchant_ID in
(select Merchant_ID from MerchantID_Table_Reference))
begin
declare @cmd nvarchar(4000)
set @cmd = '
select * from loc.'+@tablename+'
select * into #Temp3 from loc.'+@tablename+'
select * from #Temp3
select * into #closed from
(select Master_ID from #Temp3
except
select Master_ID from #Temp) t
select * from #closed
if exists (select * from #closed)
begin
update #Temp3
set ST_Status = (''Closed'')
where Master_ID in (select Master_ID from #closed)
insert into #Temp
select * from #Temp3
where Master_ID in (select Master_ID from #closed)
end
select * from #closed
drop table #closed
drop table loc.'+@tablename+'
drop table #Temp3
'
print (@cmd)
exec(@cmd)
end