条件插入的SQL语句?

edwardlj 2003-10-21 11:15:48
有没有哪种很强的SQL语句可以实现若表中已经存在该记录,则不作任何操作,若不存在,则将其插入表中的方法,多谢了.
...全文
147 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dennis618 2003-10-21
  • 打赏
  • 举报
回复
declare @tmp table (a varchar(10))
insert into @tmp values('aa')
insert into @tmp select 'bb' where 'bb' not in (select a from @tmp where a='bb')
insert into @tmp select 'bb' where 'bb' not in (select a from @tmp where a='bb')
select * from @tmp
welyngj 2003-10-21
  • 打赏
  • 举报
回复
设计表的时候就应该用primary key 约束
sdhdy 2003-10-21
  • 打赏
  • 举报
回复
if not exists(select * from tablename where 你的条件)
insert tablename values(1,2,...)
gmlxf 2003-10-21
  • 打赏
  • 举报
回复
insert t(col1,col2) select 'a','b' where 'a' not in (select col1 from t) and 'b' not in(select col2 from t)
pengdali 2003-10-21
  • 打赏
  • 举报
回复
insert 表 (aa,bb) select 'aa的值','bb的值' where 'aa的值' not in (select aa from 表)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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