我的问题是:动态向一个表中追加纪录,如果表中一有这条纪录的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)
应该怎样写,谢谢
...全文
143打赏收藏
存在的纪录不允许插入
我的问题是:动态向一个表中追加纪录,如果表中一有这条纪录的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) 应该怎样写,谢谢
用条件判断:
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