简体   繁体   English

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

[英]This is not permitted when the subquery follows =, !=, <, <= , >, >=

I have built a sql job to update some value in table regarding to the condition in the where clause as below: 我建立了一个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)

But the following error appeared when I try to run this script: 但是当我尝试运行此脚本时出现以下错误:

Msg 512, Level 16, State 1, Procedure UpdateContainersStatistcs, Line 9 Subquery returned more than 1 value. 消息512,级别16,状态1,过程UpdateContainersStatistcs,行9子查询返回的值大于1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。 The statement has been terminated. 该语句已终止。

The reason is because of trigger created on the table. 原因是由于在表上创建了触发器。 I disabled this trigger and everything is working well the trigger script is as below: 我禁用了该触发器,并且触发器脚本运行正常,如下所示:

    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

Query is totally correct but the reason of the error was a trigger created on the same table. 查询是完全正确的,但错误的原因是在同一张表上创建的触发器。

I disabled the trigger on the table and now everything is working well 我禁用了桌上的触发器,现在一切正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或用作表达式时,不允许使用 - Not permitted when the subquery follows =, !=, <, <= , >, >= or when used as an expression SQL错误:当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或将子查询用作表达式时,不允许这样做 - SQL ERROR: This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或将子查询用作表达式时,不允许这样做 - This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression SQL错误-子查询后跟=,!=,&lt;,&lt;=,&gt;,&gt; =或将子查询用作表达式时,不允许这样做 - Error in SQL - This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression 子查询返回的值超过1。 当子查询跟随=,!=时,不允许这样做, - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, 子查询返回的值超过1。 当子查询遵循=,!=,&lt;, - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, 子查询返回的值超过1。 当子查询遵循=,!=, - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, 子查询返回超过 1 个值。 当子查询跟随 =, ,=, &lt;, &lt;=, &gt;, &gt;= 或 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =…?时,不允许这样做。 - Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=, <, <= , >, >= …? 子查询返回的值超过1。 当子查询遵循=时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows =
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM