![](/img/trans.png)
[英]Not permitted when the subquery follows =, !=, <, <= , >, >= or when used as an expression
[英]This is not permitted when the subquery follows =, !=, <, <= , >, >=
我建立了一個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
查詢是完全正確的,但錯誤的原因是在同一張表上創建的觸發器。
我禁用了桌上的觸發器,現在一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.