子查询返回的值超过1。当子查询跟随=,!=,> =或子查询用作表达式时,不允许这样做

时间:2013-09-17 14:29:03

标签: c# sql-server-2008

    ALTER PROCEDURE Dashbord @userid INT 
AS 
  BEGIN 
      DECLARE @reportmanager INT 
      DECLARE @admintype INT 

      SET @reportmanager = (SELECT Count(managerid) 
                            FROM   reportingperson 
                            WHERE  employeeid = @userid) 
      SET @admintype = (SELECT Max(admintype) 
                        FROM   employee 
                        WHERE  content_id = @userid) 

      IF EXISTS (SELECT 1 
                 WHERE  @reportmanager > 0 
                        AND @admintype = 0) 
        BEGIN 
            SELECT b.contentid, 
                   x.content_id, 
                   x.employeename, 
                   x.imagetype, 
                   CONVERT(VARCHAR(10), b.fromdate, 126)    AS fromdate, 
                   CONVERT(VARCHAR(10), b.todate, 126)      AS Todate, 
                   b.leaveapp, 
                   b.leavesubject, 
                   b.reason, 
                   ( CASE 
                       WHEN ( b.status = 0 ) THEN 'Pending' 
                       ELSE 'Approved' 
                     END )                                  AS status, 
                   CONVERT(VARCHAR(10), b.createddate, 126) AS Createddate 
            FROM   leaves b 
                   LEFT OUTER JOIN employee x 
                                ON b.employeeid = x.content_id 
            WHERE  ( b.employeeid = @userid ) 
                   AND b.status = 0 

            SELECT admintype 
            FROM   employee 
            WHERE  content_id = @userid 
        END 
      ELSE 
        BEGIN 
            SELECT b.contentid, 
                   x.content_id, 
                   x.employeename, 
                   x.imagetype, 
                   CONVERT(VARCHAR(10), b.fromdate, 126)    AS fromdate, 
                   CONVERT(VARCHAR(10), b.todate, 126)      AS Todate, 
                   b.leaveapp, 
                   b.leavesubject, 
                   b.reason, 
                   ( CASE 
                       WHEN ( b.status = 0 ) THEN 'Pending' 
                       ELSE 'Approved' 
                     END )                                  AS status, 
                   CONVERT(VARCHAR(10), b.createddate, 126) AS Createddate 
            FROM   leaves b 
                   LEFT OUTER JOIN employee x 
                                ON b.employeeid = x.content_id 
            WHERE  b.employeeid IN (SELECT employeeid 
                                    FROM   reportingperson 
                                    WHERE  managerid = @userid) 
                   AND b.status = 0 

            SELECT admintype 
            FROM   employee 
            WHERE  content_id = @userid 
        END 
  END 

我必须现在一旦该人批准了休假,通知必须转到另一个报告人然后批准休假然后员工可以去这个房子

1 个答案:

答案 0 :(得分:0)

我建议您接受每个查询和子查询,并自行测试,直到您检测到导致问题的那个

同样在查询的这一部分......

left outer join employee x on b.employeeid=x.content_id
where (b.employeeid=@userid 
) and b.status=0

如果您正在进行连接on b.employeeid=x.content_id,为什么然后添加WHERE b.employeeid=@userid这两个条件正在检查可能导致某些意外行为的同一字段b.employeeid