子查询应该返回超过1的值

时间:2014-09-11 09:58:38

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

ALTER TRIGGER [dbo].[DEL_RouteStops_CatalogID]
   ON  [dbo].[RouteStops]
   AFTER DELETE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    DECLARE @rowcount int
    DECLARE @rowsexist bit

    SET @rowcount = (SELECT COUNT(a.RouteStopID) FROM RouteStops a, deleted d WHERE a.StopCIXID = d.StopCIXID)


    IF (@rowcount > 0)
    BEGIN
    SET @rowsexist = 'true'

**SUPPOSEDLY THE BELOW LINE RETURNS MORE THAN 1 RESULT?!**  
    IF @rowsexist = 'true'
    BEGIN   

    UPDATE LocationCatalog
    SET NextSubmitDate = (SELECT MIN(a.UD5) FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID))
    FROM RouteStops a, LocationCatalog b, deleted d
    WHERE b.RouteStopID = d.RouteStopID

    UPDATE LocationCatalog
    SET SubmitDay = (SELECT a.Route FROM RouteStops WHERE UD5 = (SELECT MIN(a.UD5) FROM RouteStops a, deleted d where a.StopCIXID = d.StopCIXID)),
        NextPickDate = (SELECT dateadd(D, a.UD2, b.NextSubmitDate) FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID)),
        NextDeliveryDate = (SELECT dateadd(D, a.UD3, b.NextSubmitDate) FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID))
    FROM RouteStops a, LocationCatalog b, deleted d
    WHERE b.RouteStopID = d.RouteStopID

    END

END
END

1 个答案:

答案 0 :(得分:1)

首先查看用于分配NextPickDateNextDeliveryDate的子查询 它们可能会返回多行吗?

这些子查询需要签出:

SELECT dateadd(D, a.UD2, b.NextSubmitDate) 
FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID)

SELECT dateadd(D, a.UD3, b.NextSubmitDate) 
FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID)

您可以尝试使用适当的聚合运算符来确保子查询只返回一行;例如:

SELECT min(dateadd(D, a.UD2, b.NextSubmitDate)) 
FROM RouteStops a, deleted d where a.StopCIXID IN (d.StopCIXID)