子查询遵循=,!=,<,< =,>,> =时不允许这样做

时间:2016-04-06 13:00:22

标签: sql sql-server database sql-server-2008

我已经构建了一个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)

但是当我尝试运行此脚本时出现以下错误:

  

Msg 512,Level 16,State 1,Procedure UpdateContainersStatistcs,Line   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

1 个答案:

答案 0 :(得分:0)

查询完全正确,但错误的原因是在同一个表上创建的触发器。

我禁用了桌面上的触发器,现在一切正常