如果有条件,则 upsert else 什么都不做
if condition, then upsert else do nothing
我试图仅在条件为真时才插入值,否则我不想对记录执行任何操作。当我再次 运行 时,我的代码似乎会生成主键错误(表明插入正在触发,但应跳过整个语句)
第二个错误 运行:Violation of PRIMARY KEY constraint 'PK__chicken__A2D9E564407C90CD'. Cannot insert duplicate key in object 'dbo.chicken'. The duplicate key value is (10118-78843).
查询:
IF((select status from chicken where [PrimaryKey]='10118-78843')!='paid')
update [eComm]..[chicken] set [Status]='Overdue' where [PrimaryKey]='10118-78843'
IF @@ROWCOUNT=0 insert into [eComm]..[chicken]([PrimaryKey],[Status]) values('10118-78843','Paid');
创建示例table代码:
create table chicken(
PrimaryKey nvarchar(50) not null,
Status nvarchar(10),
PRIMARY KEY (PrimaryKey)
)
目标(这是无效语法):
IF((select status from chicken where [PrimaryKey]='10118-78843')!='paid')
{
update [eComm]..[chicken] set [Status]='Overdue' where [PrimaryKey]='10118-78843'
IF @@ROWCOUNT=0 insert into [eComm]..[chicken]([PrimaryKey],[Status]) values('10118-78843','Paid');
}
否则,什么都不做
问题是您没有考虑如果存在现有行但状态为 paid
。
会发生什么
您需要稍微调整一下逻辑:
declare @status varchar(20) = (
select c.status
from chicken c
where c.[PrimaryKey] = '10118-78843'
);
if (@status != 'paid')
update [chicken]
set [Status] = 'Overdue'
where [PrimaryKey] = '10118-78843';
else if (@status is null)
insert into [chicken] ([PrimaryKey], [Status])
values ('10118-78843', 'Paid');
或者:
insert into [chicken] ([PrimaryKey], [Status])
select '10118-78843', 'Paid'
where not exists (select 1
from chicken c
where c.[PrimaryKey] = '10118-78843'
);
if (@@ROWCOUNT = 0)
update [chicken]
set [Status] = 'Overdue'
where [PrimaryKey] = '10118-78843'
and status != 'Paid';
我试图仅在条件为真时才插入值,否则我不想对记录执行任何操作。当我再次 运行 时,我的代码似乎会生成主键错误(表明插入正在触发,但应跳过整个语句)
第二个错误 运行:Violation of PRIMARY KEY constraint 'PK__chicken__A2D9E564407C90CD'. Cannot insert duplicate key in object 'dbo.chicken'. The duplicate key value is (10118-78843).
查询:
IF((select status from chicken where [PrimaryKey]='10118-78843')!='paid')
update [eComm]..[chicken] set [Status]='Overdue' where [PrimaryKey]='10118-78843'
IF @@ROWCOUNT=0 insert into [eComm]..[chicken]([PrimaryKey],[Status]) values('10118-78843','Paid');
创建示例table代码:
create table chicken(
PrimaryKey nvarchar(50) not null,
Status nvarchar(10),
PRIMARY KEY (PrimaryKey)
)
目标(这是无效语法):
IF((select status from chicken where [PrimaryKey]='10118-78843')!='paid')
{
update [eComm]..[chicken] set [Status]='Overdue' where [PrimaryKey]='10118-78843'
IF @@ROWCOUNT=0 insert into [eComm]..[chicken]([PrimaryKey],[Status]) values('10118-78843','Paid');
}
否则,什么都不做
问题是您没有考虑如果存在现有行但状态为 paid
。
您需要稍微调整一下逻辑:
declare @status varchar(20) = (
select c.status
from chicken c
where c.[PrimaryKey] = '10118-78843'
);
if (@status != 'paid')
update [chicken]
set [Status] = 'Overdue'
where [PrimaryKey] = '10118-78843';
else if (@status is null)
insert into [chicken] ([PrimaryKey], [Status])
values ('10118-78843', 'Paid');
或者:
insert into [chicken] ([PrimaryKey], [Status])
select '10118-78843', 'Paid'
where not exists (select 1
from chicken c
where c.[PrimaryKey] = '10118-78843'
);
if (@@ROWCOUNT = 0)
update [chicken]
set [Status] = 'Overdue'
where [PrimaryKey] = '10118-78843'
and status != 'Paid';