SQL Server存储过程在网页上运行时返回错误

时间:2014-12-19 06:54:13

标签: sql-server sql-server-2008

当我在sql中运行查询然后它成功运行但是当它在网页中运行时它会抛出错误:

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   一种表达。声明已经终止。

存储过程:

    IF (@Mode='CurrentList') 
    BEGIN
        SELECT 
           ROW_NUMBER() OVER (ORDER BY emp1.Name ASC) AS RowNumber 
           ,emp1.empid 
           ,emp1.Name
           ,(SELECT emp2.Address FROM (SELECT Address,ID,Time, Number=ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Time DESC) FROM EMP_table2) emp2 join [EMP_table1] emp1  ON emp1.ID=emp2.ID WHERE Number = 1) as Address
          ,(SELECT DATEDIFF(MINUTE, emp2.Time , CURRENT_TIMESTAMP) FROM (SELECT ID,Time, Number=ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Time DESC) FROM EMP_table2) emp2 join [EMP_table1] emp1 ON emp1.ID=emp2.ID WHERE Number = 1) as Time       
        INTO 
           #CurrentDateList
        FROM 
           [EMP_table1] emp1 
        JOIN 
           [EMP_table3] emp3 ON emp1.Name = emp3.Name  
        WHERE 
           CURRENT_TIMESTAMP BETWEEN emp3.StartDate AND emp3.EndDate 

      SELECT @RecordCount = COUNT(*)
      FROM #CurrentDateList

      SELECT * FROM #CurrentDateList
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #CurrentDateList
END

1 个答案:

答案 0 :(得分:1)

您的子查询两次加入emp1,我不认为这是您想要的。我想你的子查询需要这种模式:

 ,(SELECT DATEDIFF(MINUTE, emp2.Time , CURRENT_TIMESTAMP) 
   FROM (SELECT ID,Time, 
          Number=ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Time DESC) 
          FROM EMP_table2) emp2 
   WHERE emp1.ID=emp2.ID AND Number = 1) as Time