存在的纪录不允许插入

hljhl 2003-11-23 04:43:06
我的问题是:动态向一个表中追加纪录,如果表中一有这条纪录的gh,则不追加。我是这样写的但运行时不如意。
insert y_txb gh,xm,xb,csdate select gh,xm,xb,csdate from zzryxxb where datediff(year,csdate,getdate())>10 and not exists(select gh from y_txb)
应该怎样写,谢谢
...全文
14 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightheroes 2003-11-23
  • 打赏
  • 举报
回复
insert y_txb gh,xm,xb,csdate select gh,xm,xb,csdate from zzryxxb where datediff(year,csdate,getdate())>10 and gh not in (select gh from y_txb)
starwill 2003-11-23
  • 打赏
  • 举报
回复
用条件判断:
if not exists(select * from y_txb where gh = @gh,xm = @xxm...)
begin
insert y_txb gh,xm,xb,csdate select gh,xm,xb,csdate from zzryxxb where datediff(year,csdate,getdate())>10 and not exists(select gh from y_txb)
end
starwill 2003-11-23
  • 打赏
  • 举报
回复
insert y_txb gh,xm,xb,csdate select gh,xm,xb,csdate from zzryxxb where datediff(year,csdate,getdate())>10 and hg not exists(select gh from y_txb)

34,499

社区成员

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

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