You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BEGIN TRANSACTION;
UPDATEdbo.t WITH (UPDLOCK, SERIALIZABLE) SET val = @val WHERE [key] = @key;
IF @@ROWCOUNT =0BEGIN
INSERT dbo.t([key], val) VALUES(@key, @val);
END
COMMIT TRANSACTION;
To get the id being upserted I used:
Declare @ID TABLE(InnerID INTnull);
UPDATEdbo.t WITH (UPDLOCK, SERIALIZABLE) SET val = @val
OUTPUT inserted.id INTO @ID # I also tried without the INTOWHERE [key] = @key;
IF @@ROWCOUNT !=0SELECT InnerID from @ID;
ELSE
BEGIN
INSERT dbo.t([key], val) VALUES(@key, @val);
select SCOPE_IDENTITY() as InnerID;
END
I tried to put this query with and without a prepared statement and through Exec, Query, and QueryRow and with and without sql.Named("ID", sql.Out{Dest: &reportID}), but I was never able to get back the ID.
In SQL Server Management Studio the above query returns one line with one col called InnerID for update and insert.
I am no expert in T-SQL and am new to this driver. There's something I likely don't understand / doing wrong.
Thanks for your help.
The text was updated successfully, but these errors were encountered:
Hello,
Thanks for your driver !
I followed code examples from https://sqlperformance.com/2020/09/locking/upsert-anti-pattern and particularly this one:
To get the id being upserted I used:
I tried to put this query with and without a prepared statement and through Exec, Query, and QueryRow and with and without
sql.Named("ID", sql.Out{Dest: &reportID}),
but I was never able to get back the ID.In SQL Server Management Studio the above query returns one line with one col called
InnerID
for update and insert.I am no expert in T-SQL and am new to this driver. There's something I likely don't understand / doing wrong.
Thanks for your help.
The text was updated successfully, but these errors were encountered: