日期:2014-05-18  浏览次数:20488 次

保存数据时需要先判断一个条件的SQL怎么写呀?
表A
SheBeiID         type
00001                 12
00001                 13

表B
SheBeiID         type           value
00001                 12               78.08

比如现在有三份数据,00001   12   90、00001   13   70、00001   14   78.02
那么应该是最后一份不被保存,因为type=14的不存在表A里,属于不合法数据,请问这样的要求该如何写SQL?

------解决方案--------------------
if not exists (select * from A where type=14)
begin
/*你的语句*/

end
------解决方案--------------------
可以使用觸發器來控制

Create Trigger TR_Insert_B On B
Instead Of Insert
As
Insert B Select C.* From A Inner Join Inserted C On A.SheBeiID = C.SheBeiID And A.type = C.type
GO
------解决方案--------------------
insert tb
select * from tb1 where type in(select type from tb2)