我有一堆删除查询,我必须为每个查询复制值。例如;
delete from source where id in (3300,3301, 2872)
delete from performance where srcid in (3300,3301, 2872)
delete from title where id in (3300,3301, 2872)
delete from name where srcid in (3300,3301, 2872)
我想声明值(3300,3301,2872),因为它们每次都会更改。我必须为每个查询复制。我知道如何使用声明和声明@source =来声明一个数字,但是对于如何获取in感到困惑
我从网上尝试了许多变体。这是最新的变化。它没有给出错误,但是没有找到列表中的项目。我无法从网上弄清楚sp或函数的创建。
DECLARE @sourcelist VARCHAR = '2380,2379'
--SET @sourcelist = '2380' + ',' + '2379'
select * from dbo.Source where id in (@sourcelist)
DECLARE @sourcelist VARCHAR = '2380,2379'
--SET @sourcelist = '2380' + ',' + '2379'
select * from dbo.Source where id in (@sourcelist)
--------------------------------
这是sp的变体
DECLARE @sourcelist VARCHAR(500)
DECLARE @delete1 NVARCHAR(4000)
SET @delete1 = 'delete from [EID].[FileLoadLog] where srcid in (CAST(@sourcelist AS varchar(500)))'
BEGIN
EXEC sp_executesql @delete1
END
每个都是一行。我希望删除2行。
答案 0 :(得分:2)
您也可以使用表变量来完成
declare @items table(
id int
);
insert @items(id)
values (3300),(3301), (2872);
delete from source where id in (select id from @items);
delete from performance where srcid in (select id from @items);
delete from title where id in (select id from @items);
delete from name where srcid in (select id from @items);
答案 1 :(得分:1)
您似乎正在使用SQL Server,因此我将使用该语法。
只需将值存储在临时表中并使用它即可
select v.*
into #temp_ids
from (values (3300), (3301), (2872)) v(id);
delete from source where id in (select id from #temp_ids);
delete from performance where srcid in (select id from #temp_ids);
delete from title where id in (select id from #temp_ids);
delete from name where srcid in (select id from #temp_ids);
答案 2 :(得分:1)
有很多方法可以做到这一点。如果您想使用动态SQL,可以使用以下选项,其中我们将“令牌” 替换为您的ID字符串
示例
int main()
{
sTest data[SIZE]; //size isn't important
sTest *dataPtr = data;
setTeamA(dataPtr[0].teamA, "name1");
// ...
printf("%d", getNum(dataPtr[1].num)); // just an example. i know that it isn't initialized
// ...
return 0;
}