update table1 set columnD='grape' where columnD=' ' --select @@rowcount if @@rowcount=0 .... else insert
IF @@ROWCOUNT = 0 insert into 表(字段列表) values(值 列表) 两行搞定
example: ------------------------------------------------ create table #t (ID int,Name varchar(20)) insert into #t select 1,'AAAA' insert into #t select 2,'BBBB' insert into #t select 3,'CCCC' insert into #t select 4,'DDDD' insert into #t select 5,'EEEE' insert into #t select 6,'FFFF' GO update #t set Name = 'GGGG' where ID = 7if @@rowcount<>0 insert into #t select 7,'GGGG' select * from #t--输出结果 ID Name --- ---- 1 AAAA 2 BBBB 3 CCCC 4 DDDD 5 EEEE 6 FFFF 7 GGGG
update ... if @@ERROR=0 if @@rowcount=0 insert ...
insert ...
--select @@rowcount
if @@rowcount=0
....
else
insert
insert into 表(字段列表) values(值 列表)
两行搞定
------------------------------------------------
create table #t (ID int,Name varchar(20))
insert into #t select 1,'AAAA'
insert into #t select 2,'BBBB'
insert into #t select 3,'CCCC'
insert into #t select 4,'DDDD'
insert into #t select 5,'EEEE'
insert into #t select 6,'FFFF'
GO
update #t set Name = 'GGGG' where ID = 7if @@rowcount<>0
insert into #t select 7,'GGGG'
select * from #t--输出结果
ID Name
--- ----
1 AAAA
2 BBBB
3 CCCC
4 DDDD
5 EEEE
6 FFFF
7 GGGG
if @@ERROR=0
if @@rowcount=0
insert ...