I have a table like this in 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.
So I have to insert/update these varID/dataID
combos.
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.