2016-04-06 8 views
0

は、私は以下のようにwhere句で条件に関するテーブル内のいくつかの値を更新するには、SQLの仕事を構築している次の時に許可されていません!これは、サブクエリが=、=、<, <= , >、> =

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サブクエリは複数の値を返しました。 サブクエリが、=、!<、< =、>、> =、またはサブクエリが式 として使用されている場合は、これは許可されません。ステートメントは終了されました。

理由は表に作成されたトリガーのためです。 私はこのトリガーを無効にし、すべてがうまく機能している トリガスクリプトは以下の通りです:

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 
+0

このクエリは書かれているように見えます。 'key =(othertableからキーを選択)'のような副問い合わせを持つ別の更新を探してください。 –

+0

今、私はこのテーブルにトリガがある理由を知っていますが、私はこの問題を解決するために何をすればよいかわかりません。私はそれを無効にすることはできません –

+1

そのようなトリガがある場合 - 私はあなたの唯一のアプローチは、トリガがうまく行かれていないと複数の行を処理することはできませんので、 –

答えて

0

クエリは完全に正しいですが、エラーの理由は、同じテーブルの上に作成されたトリガました。

テーブルのトリガーを無効にしたところ、すべて正常に動作しています。

関連する問題

 関連する問題