不是很难的SQL语句,是改是插?

leonatcs 2006-05-24 11:53:53
一个表,其中每条记录用RecNo字段(是bigint型)为主键区分。
我要向该表里插入一条记录,但是如果已经有了这条记录,就修改它。
应该怎么写SQL语句。
...全文
169 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xeqtr1982 2006-05-25
  • 打赏
  • 举报
回复
create table t(RecNo bigint,name varchar(10))
go

create trigger cfq on t
instead of insert
as

if @@rowcount=0 return

if exists(select 1 from inserted a,t b where a.RecNo=b.RecNo)
begin
update b set b.name=a.name from inserted a,t b where a.RecNo=b.RecNo
end
else
begin
insert into t select * from inserted
end
go

insert into t select 1,'aa'
select * from t
go

insert into t select 1,'bb'
select * from t
go

drop trigger cfq
drop table t
lxzm1001 2006-05-25
  • 打赏
  • 举报
回复
if exists(select * from b where id=5)
update b set s='fadfsd' where id=5
else
insert b select 5,'fadfsd'
leonatcs 2006-05-25
  • 打赏
  • 举报
回复
谢谢二位!

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧