Исходный код вики Insert or Update

Версия 2.1 от Alexandr Fokin на 2020/03/21 22:17

Последние авторы
1 {{code language="sql"}}
2
3 Declare @key int = 12
4 Declare @msg nvarchar(max) = 'L2'
5
6 begin tran
7 if exists (
8 select *
9 from dbo.Log2Entity with (updlock,serializable)
10 where ID = @key
11 )
12 begin
13 update dbo.Log2Entity
14 set
15 Message = @msg
16 where ID =@key
17 end
18 else
19 begin
20 insert into dbo.Log2Entity (Message)
21 values (@msg)
22 end
23 commit tran
24
25 {{/code}}