I have a table like this in SQL Server:
我在SQL Server中有这样的表:
varID(PK) dataID(PK) is_used
A 1 0
B 1 0
Then I'm loading data to update is_used to 1 if the varID/dataID
combo exists and add it in otherwise.
然后,如果varID / dataID组合存在,我将加载数据以将is_used更新为1,否则添加它。
So I have to insert/update these varID/dataID
combos.
所以我必须插入/更新这些varID / dataID组合。
varID(PK) dataID(PK)
B 1
C 1
So the updated table would look like this:
所以更新后的表格如下所示:
varID(PK) dataID(PK) is_used
A 1 0
B 1 1
C 1 1
What's the easiest way to do this? I will do it in a stored procedure.
最简单的方法是什么?我将在存储过程中执行此操作。
1 个解决方案
#1
5
Procedure tries to update is_used
given a key. If unsuccessful, inserts new row. Note I put 0 as default value for is_used - I think is_used = 1 for (C, 1) is an inadvertence.
过程尝试在给定密钥的情况下更新is_used。如果不成功,则插入新行。注意我把0作为is_used的默认值 - 我认为(C,1)的is_used = 1是无意的。
create proc AddVarDataCombo (@varID varchar(100), @dataID int)
as
set nocount on
update ATable
set is_used = 1
where varID = @varID
and dataID = @dataID
if @@rowcount = 0
begin
insert into ATable
values (@varID, @dataID, 0)
end
#1
5
Procedure tries to update is_used
given a key. If unsuccessful, inserts new row. Note I put 0 as default value for is_used - I think is_used = 1 for (C, 1) is an inadvertence.
过程尝试在给定密钥的情况下更新is_used。如果不成功,则插入新行。注意我把0作为is_used的默认值 - 我认为(C,1)的is_used = 1是无意的。
create proc AddVarDataCombo (@varID varchar(100), @dataID int)
as
set nocount on
update ATable
set is_used = 1
where varID = @varID
and dataID = @dataID
if @@rowcount = 0
begin
insert into ATable
values (@varID, @dataID, 0)
end