首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-10 
保存数据时需要先判断一个条件的SQL怎么写呀?表ASheBeiIDtype00001120000113表BSheBeiIDtypevalue00001127

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

热点排行