数据库中的存储过程问题

时间:2013-10-16 10:55:54

标签: sql stored-procedures

我使用此存储过程来显示待处理的文档

create procedure b
as
  select         
     dbo.DocumentInfo.DocID, dbo.DocumentInfo.DocName, 
     dbo.DocumentInfo.Uploadfile, dbo.DocType.Doc      Type,
     dbo.Department.DepType, ApproveType.ApproveType
 from 
     dbo.DocumentInfo 
 left join 
     dbo.DocType on dbo.DocumentInfo.DocTypeID = dbo.DocType.DocTypeID 
 left join 
     dbo.Department on dbo.DocumentInfo.DepID = dbo.Department.DepID 
 inner join      
     ApproveType on DocumentInfo.ApproveID = ApproveType.ApproveID 
                 and (ApproveType.ApproveID = 3)

但是当我查看此查询时,它无法向我显示任何文档..

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您的问题是您在连接排除数据的表上直接加入(INNER JOIN)。因此,表ApproveID中的列DocumentInfo不包含任何数据,或者列ApproveType的表ApproveID中没有匹配的行。

在我看来,如果你打算以这种方式使用这个存储过程,你在任何情况下使用连接都是错误的 - 你绝对可以考虑使用表别名,如下所示:

CREATE PROCEDURE b
AS
    SELECT  di.DocID, 
            di.DocName, 
            di.Uploadfile, 
            dt.DocType,
            d.DepType, 
            at.ApproveType
    FROM    DocumentInfo di
        LEFT OUTER JOIN
            DocType dt ON dt.DocTypeID = di.DocTypeID
        LEFT OUTER JOIN 
            Department d ON d.DepID = di.DepID
        LEFT OUTER JOIN
            ApproveType at ON at.ApproveID = di.ApproveID 
   AND  at.ApproveID = 3