【发布时间】:2016-04-06 13:00:22
【问题描述】:
我已经构建了一个 sql 作业来更新表中关于 where 子句中的条件的一些值,如下所示:
Update c set isLoaded = 0 , LoadingState = 'L', isSent = 0
from Container c
where c.isloaded = 1 and DATEDIFF(second, dateadd(HOUR, c.LoadingInterval,c.entrydate), getdate()) / 3600.0 between c.LoadingInterval and (c.LoadingInterval + 2)
但是当我尝试运行此脚本时出现以下错误:
消息 512,级别 16,状态 1,过程 UpdateContainersStatistcs,行 9 子查询返回超过 1 个值。这是不允许的,当 子查询遵循 =、!=、、>= 或当子查询用作 一种表达。声明已终止。
原因是因为在表上创建了触发器。 我禁用了这个触发器,一切正常 触发脚本如下:
CREATE TRIGGER [dbo].[UpdateContainersStatistcs]
ON [dbo].[Container]
After Update
AS
BEGIN
declare @DayDateNow as int
set @DayDateNow = (select count( DayDate) From ContainersStatstics AS ConStc where DayDate= (SELECT CONVERT(date, getdate())) and ConStc.ContractorId =(Select Con.ContractorId from inserted as Con ) )
if @DayDateNow>0
BEGIN
if UPDATE(LoadingState)
BEGIN
if( (select i.LoadingState from inserted as i) = 'E')
begin
if ((select i.LoadingState from inserted as i)=(select LoadingState from Container as Cont where Cont.RFID = (select d.RFID from deleted as d)))
begin
update ContainersStatstics
set EmptyContainersCount= (select EmptyContainersCount From ContainersStatstics as ConStc where (DayDate= (SELECT CONVERT(date, getdate())) and ConStc.ContractorId =(Select Con.ContractorId from inserted as Con ))) + 1
, ContainersCount=(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con ))
where ContractorId =(Select Con.ContractorId from inserted as Con ) and DayDate= (SELECT CONVERT(date, getdate()))
end
end
else
begin
if (select Count(EmptyContainersCount) From ContainersStatstics) > 0
begin
if ((select i.LoadingState from inserted as i)=(select LoadingState from Container as Cont where Cont.RFID = (select d.RFID from deleted as d)))
begin
update ContainersStatstics
set EmptyContainersCount= (select EmptyContainersCount From ContainersStatstics as ConStc where (DayDate= (SELECT CONVERT(date, getdate())) and ConStc.ContractorId =(Select Con.ContractorId from inserted as Con ))) - 1
, ContainersCount=(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con ))
where ContractorId =(Select Con.ContractorId from inserted as Con ) and DayDate= (SELECT CONVERT(date, getdate()))
end
end
end
end
else if UPDATE(WashingStatus)
BEGIN
if( (select i.WashingStatus from inserted as i) = 'E')
BEGIN
if ((select i.WashingStatus from inserted as i)=(select WashingStatus from Container as Cont where Cont.RFID = (select d.RFID from deleted as d)))
BEGIN
update ContainersStatstics
set WashedContainersCount= (select WashedContainersCount From ContainersStatstics as ConStc where ( DayDate= (SELECT CONVERT(date, getdate())) )and ConStc.ContractorId =(Select Con.ContractorId from inserted as Con )) + 1
, ContainersCount=(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con ))
where ContractorId =(Select Con.ContractorId from inserted as Con ) and DayDate= (SELECT CONVERT(date, getdate()))
end
end
else
Begin
if (select Count(EmptyContainersCount) From ContainersStatstics) > 0
begin
if ((select i.WashingStatus from inserted as i)=(select WashingStatus from Container as Cont where Cont.RFID = (select d.RFID from deleted as d)))
begin
update ContainersStatstics
set WashedContainersCount= (select WashedContainersCount From ContainersStatstics as ConStc where ( DayDate= (SELECT CONVERT(date, getdate())) )and ConStc.ContractorId =(Select Con.ContractorId from inserted as Con )) - 1,
ContainersCount=(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con ))
where ContractorId =(Select Con.ContractorId from inserted as Con ) and DayDate= (SELECT CONVERT(date, getdate()))
end
end
end
end
end
else
BEGIN
if UPDATE(LoadingState)
BEGIN
if ((select i.LoadingState from inserted as i)= 'E')
begin
insert into ContainersStatstics
(EmptyContainersCount ,ContractorId,DayDate,ContainersCount,WashedContainersCount)values (1,(select x.ContractorId from inserted x),(SELECT CONVERT(date, getdate())),(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con )),0)
end
END
else if UPDATE(WashingStatus)
begin
if ((select i.WashingStatus from inserted as i)= 'E')
BEGIN
insert into ContainersStatstics
(EmptyContainersCount ,ContractorId,DayDate,ContainersCount,WashedContainersCount)values (0,(select x.ContractorId from inserted x),(SELECT CONVERT(date, getdate())),(select Count(Id) from Container where ContractorId =(Select Con.ContractorId from inserted as Con )),1)
end
end
end
END
【问题讨论】:
-
这个查询看起来不错。请寻找另一个具有类似
where key = (select key from othertable)的子查询的更新。 -
现在我知道这个表上有触发器的原因,但我不知道我应该怎么做才能解决这个问题;我无法禁用它
-
如果有这样的触发器 - 那么我担心你唯一的方法是确保更新一次只在一行上运行,因为看起来触发器制作不当并且无法处理多行。
-
请编辑您的问题以包含触发代码。这样有人可以帮助您重新编写它以修复此错误。
-
这个触发器包含很多代码。如果不解释其目的和相关表的结构,将不会很容易修复。
标签: sql sql-server database sql-server-2008