积极答复者
小问题求解决~~

问题
-
我想要批量更新一下数据
先是查询一下
select * from card_group where cardid like'%n%'
返回12个结果。
接着
update card_group set time='2013-12-31 23:59:59.000' where cardid like'%n%'
服务器: 消息 512,级别 16,状态 1,过程 tr_upd_card_group
,行 30就出现错误 出现“子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
-------------------------------------------------
- 已编辑 looksoks 2012年12月3日 7:00
答案
-
你不会真删吧??????
你把 select cardid from inserted 改为 select top 1 cardid from inserted 应该可以避免错误,但是这样会不会破坏你的业务规则呢?如果破坏的话,那跟删触发器没有区别。如果OK的话,那你就加 TOP 1吧
Please click the Mark as Answer button if a post solves your problem!- 已编辑 Michael CS 2012年12月3日 9:27
- 已标记为答案 looksoks 2012年12月4日 4:36
全部回复
-
是表。
我
select * from card_group where cardid='bn000040'
返回只有1个
但是我
update card_group set time='2013-12-31 23:59:59.000' where cardid='bn000040'
却发现有2条
(所影响的行数为 1 行)
(所影响的行数为 1 行)
- 已编辑 Jacky_shen 2012年12月3日 4:14
-
我看了一下 触发器里面没有。
但是我发现 视图里面也有一个view_card_group。打开后发现内容和表里面card_group是一样的。
然后我
update view_card_group set time='2013-12-31 23:59:59.000' where cardid='bn000040'
也是
(所影响的行数为 1 行)
(所影响的行数为 1 行)
那肯定是有联级更新了
那么请问我这条语句应该如何执行成功呢?
update card_group set time='2013-12-31 23:59:59.000' where cardid like'%n%'
服务器: 消息 512,级别 16,状态 1,过程 tr_upd_card_group
,行 30子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。- 已编辑 looksoks 2012年12月3日 6:52
-
“服务器: 消息 512,级别 16,状态 1,过程 tr_upd_card_group”
这不是已经很明显了嘛,检查触发器tr_upd_card_group。
还有看看关联的外键表有没有什么其他的触发器
Please click the Mark as Answer button if a post solves your problem!
- 已编辑 Michael CS 2012年12月3日 6:58
-
-
对象名[sys].[all_objects] 无效。
你2000啊,我没有2000,网上搜一下吧
Please click the Mark as Answer button if a post solves your problem!- 已编辑 Michael CS 2012年12月3日 7:36
-
select * from syscomments
select * from sysobjects
找到了 大师在看一下。
CREATE TRIGGER tr_upd_card_group ON [dbo].[card_group] FOR UPDATE AS declare @webtimemark int select @webtimemark=webtimemark from inserted if @webtimemark=99
update card_group set webtimemark=0 where cardid=(select cardid from inserted) else u
- 已编辑 looksoks 2012年12月3日 8:23
-
select * from syscomments
select * from sysobjects
找到了 大师在看一下。
CREATE TRIGGER tr_upd_card_group ON [dbo].[card_group] FOR UPDATE AS declare @webtimemark int select @webtimemark=webtimemark from inserted if @webtimemark=99
update card_group set webtimemark=0 where cardid=(select cardid from inserted) else u
"select cardid from inserted" 返回多个值造成的,你现在应该知道怎么改了吧
Please click the Mark as Answer button if a post solves your problem! -
你把那个触发器删了算了。因为在我不知道你业务规则的情况下,告诉你加TOP 1 跟删除触发器 ,从本质上说,对我来说都是一样的,都有可能破坏你的业务规则,这也是在上面没有直接给你答案的原因,你明白了不?
Please click the Mark as Answer button if a post solves your problem!- 已编辑 Michael CS 2012年12月3日 9:13
-
你不会真删吧??????
你把 select cardid from inserted 改为 select top 1 cardid from inserted 应该可以避免错误,但是这样会不会破坏你的业务规则呢?如果破坏的话,那跟删触发器没有区别。如果OK的话,那你就加 TOP 1吧
Please click the Mark as Answer button if a post solves your problem!- 已编辑 Michael CS 2012年12月3日 9:27
- 已标记为答案 looksoks 2012年12月4日 4:36
-
LZ可以参考一下这篇文章,关于计算列的
http://database.ctocio.com.cn/dbzjdysummary/48/8730048_4.shtml
Nullability
一个计算列值的Nullability将由数据库引擎本身来确定。一个非空参照列的结果可能在某些情况下是空值,以便避免可能的益处或者下溢。如果需要,那么你可以使用ISNULL(check_expression,,常数)来提供一个NULL的替代值。
一些限制对于SQL Server 2000,你不能创建一个persisted计算列。
你不能从其它表中参照列直接用于一个计算列。
你不能在计算列中应用插入或者更新的语句。
LZ可以看一下你的表有没有计算列存在
- 已编辑 Steven.桦仔 2012年12月3日 14:26
-
我还没删。估计在查询分析器里也删不了。
我吧它替换null 说我不能替换 text 说是计算列
你好,
还是先了解下有这个触发器tr_upd_card_group的原因吧, 这样能更好的帮你修改代码。
Thanks.
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.