读书人

保存数据时需要先判断一个条件的SQL如

发布时间: 2012-01-10 21:26:51 作者: rapoo

保存数据时需要先判断一个条件的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)

读书人网 >SQL Server

热点推荐