0

我尝试执行查询,但它显示数据超过 5 次。这是我使用的查询

ALTER procedure [dbo].[dddddd]
    @userid int
as
    SELECT DocumentInfo.DocID as DocumentID,
        dbo.DocumentInfo.DocName as DocumentName,
        dbo.DocumentInfo.Uploadfile as FileUploaded,
        dbo.DocType.DocType as Document,
        dbo.Department.DepType as Department,
        dbo.ApproveType.ApproveType AS ApproveID
    FROM dbo.DocumentInfo
      inner JOIN dbo.DocType ON dbo.DocumentInfo.DocTypeID=dbo.DocType.DocTypeID
      inner JOIN dbo.Department ON dbo.DocumentInfo.DepID=dbo.Department.DepID
      LEFT JOIN dbo.ApproveType ON dbo.ApproveType.approveid=dbo.Department.DepID  
      LEFT OUTER JOIN Approval a ON a.DocID = a.DocID
      JOIN ApproveType at ON at.ApproveID = ISNULL(a.Approveid, 3)  
    where UserID=@userid

看到这个结果。。

DocumentID  DocumentName    FileUploaded    Document    Department  ApproveID
    8   SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL
    8   SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL
     8  SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL
     8  SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL
   8    SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL
    8   SDASDASD    dsfsdf.docx MSWord  Human Resource  NULL

  9 dr reprt n analysis HR.docx MSWord  Human Resource  NULL
  9 dr reprt n analysis HR.docx MSWord  Human Resource  NULL
   9    dr reprt n analysis HR.docx MSWord  Human Resource  NULLL
 9  dr reprt n analysis HR.docx MSWord  Human Resource  NULL
 9  dr reprt n analysis HR.docx MSWord  Human Resource  NULL
  10    hr report   HR.docx MSWord  Human Resource  NULL
 10 hr report   HR.docx MSWord  Human Resource  NULL
 10 hr report   HR.docx MSWord  Human Resource  NULL
 10 hr report   HR.docx MSWord  Human Resource  NULL

错误在哪里?

4

4 回答 4

1

我猜,但这条线看起来不正确:

LEFT JOIN dbo.ApproveType ON dbo.ApproveType.approveid=dbo.Department.DepID  

approveidDepID真的是一回事吗?

于 2013-10-23T19:45:58.467 回答
1

您显然在其中一个未正确加入的联接中有 1:N 关系。解决此问题的一种方法是包含更多列,并查看哪些具有不适用于您的查询的不同值。

例如(完全猜测):

LEFT JOIN dbo.ApproveType ON dbo.ApproveType.approveid=dbo.Department.DepID

这会返回特定部门的所有批准吗?您是否需要过滤仅批准指定文档?

于 2013-10-23T19:47:18.347 回答
1

您的联接带回多行...无法提供确切的答案,但要找到导致它的联接:

SELECT *
FROM dbo.DocumentInfo
  inner JOIN dbo.DocType ON dbo.DocumentInfo.DocTypeID=dbo.DocType.DocTypeID
  inner JOIN dbo.Department ON dbo.DocumentInfo.DepID=dbo.Department.DepID
  LEFT JOIN dbo.ApproveType ON dbo.ApproveType.approveid=dbo.Department.DepID  
  LEFT OUTER JOIN Approval a ON a.DocID = a.DocID
  JOIN ApproveType at ON at.ApproveID = ISNULL(a.Approveid, 3)  
where documentid = 8

这将带回您遇到问题的 5 条记录以及文档 8 的所有列。查看每个值并验证这些是重复的行。如果您发现一列的 5 行具有不同的值,那么您就有了导致额外行的表。重新访问连接并添加新逻辑以将其限制为一行而不是 5。

替代方法是一次注释掉每个表并重新运行。如果您获得相同数量的行,那么该表很好......如果您注释掉一个表并获得 1 行,那么您已经找到了哪个表导致了额外的行,并且您需要在联接中添加新的逻辑把这个减少到一行。

于 2013-10-23T19:50:50.053 回答
0

The first thing I do to troubleshoot these problems is find the first shared ID for the join and check that. So if you got back 5 records that all had dbo.DocumentInfo.DocTypeID = 7 then do a select against dbo.DocType where DocTypeID=7. Do you get 5 answers. If not, go to the next join, select data from Department where DepartmentID=(whatever is repeating) until you find the join that is duplicating the data. Then figure out how the tables are joined to get a 1-1 join rather than 1 - M.

于 2013-10-23T19:49:04.650 回答