永发信息网

SQLSERVER子查询返回的值不止一个。

答案:2  悬赏:0  手机版
解决时间 2021-12-19 12:14
  • 提问者网友:嘚啵嘚啵
  • 2021-12-19 09:25
CREATE TRIGGER check_shippeddate ON Orders

FOR UPDATe

AS

IF((SELECT shippedDate FROM inserted) > RequiredDate))

BEGIN

PRINT 'Order was shipped after the required date'

END

GO
我的IF有问题,要怎么修改呢?

当这个执行时
UPDATE Orders
SET ShippedDate = '1994-04-20'
WHERe OrderID = 11051
ANDCustomerID = 'LAMAI'
ANDEmployeeID = 7
就会PRINT 消息出来,因为日期比RequiredDate大
最佳答案
  • 五星知识达人网友:十鸦
  • 2021-12-19 10:50
加条件使其唯一
要不排序取第一条
看你需求是什么
全部回答
  • 1楼网友:洎扰庸人
  • 2021-12-19 11:19
你说的是 select ...... from inserted; 返回多行的情况么? --假设这个a是主表 create table a( aid int identity(1, 1) primary key, money int ); -- 假设这个b是 日志表, 负责记录 a表 发生的变化。 create table b( bid int identity(1, 1) primary key, aid int, money int ); go -- 假设这个c也是日志表, 负责记录 a表 发生的变化。 -- 但是这个c表是用于演示 不正确使用的例子。 create table c( cid int identity(1, 1) primary key, aid int, money int ); go -- 测试的存储过程 -- 当 a 表 插入 / 更新 / 删除的时候i, 都触发。 create trigger aftera on a for insert,update,delete as declare @aid int, @money int; begin if exists(select 1 from inserted) and not exists(select 1 from deleted) begin -- 插入触发. -- 正确的使用. insert into b select inserted.aid, inserted.money from inserted; -- 不正确的使用. select @aid = aid, @money = money from inserted; insert into c values ( @aid, @money); end; if exists(select 1 from inserted) and exists(select 1 from deleted) begin -- 更新触发. -- 正确的使用. insert into b select inserted.aid, inserted.money - deleted.money from inserted, deleted where inserted.aid = deleted.aid; -- 不正确的使用. select @aid = aid, @money = money from inserted; select @money = @money - money from deleted; insert into c values ( @aid, @money); end; if not exists(select 1 from inserted) and exists(select 1 from deleted) begin -- 删除触发 -- 正确的使用. insert into b select deleted.aid, 0 - deleted.money from deleted; -- 不正确的使用. select @aid = aid, @money = 0 - money from deleted; insert into c values ( @aid, @money); end; end; go -- 一条sql语句,插入一条数据 insert into a values (100); -- 一条sql语句,插入多条数据 insert into a select 10 union all select 20; go -- 一条sql语句,更新一条数据 update a set money = money - 50 where aid = 1; -- 一条sql语句,更新多条数据 update a set money = money + 50 where aid != 1; go -- 一条sql语句,删除一条数据 delete from a where aid = 1; -- 一条sql语句,删除多条数据 delete from a where aid != 1; go select * from b go bid aid money ----------- ----------- ----------- 1 1 100 2 3 20 3 2 10 4 1 -50 5 3 50 6 2 50 7 1 -50 8 3 -70 9 2 -60 (9 行受影响) select * from c go cid aid money ----------- ----------- ----------- 1 1 100 2 2 10 3 1 -50 4 2 30 5 1 -50 6 2 -60 (6 行受影响) 这里, aid = 3 的数据,没有被触发器处理。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯