显示文档存储过程 - 如何选择两次批准的文档。

时间:2013-12-02 20:41:52

标签: sql sql-server stored-procedures

我有一个场景,当任何用户首次上传文档时,它会进行审批。首先,它是一个主管。当主管批准/拒绝文件时,只有批准的文件才会送给经理。然后,当经理批准/拒绝文件时,只有批准的文件才能送到董事处进行最终批准。

这是当前的存储过程:

ALTER PROCEDURE [dbo].[ALLDOCUMNETS]
   AS
 begin

  SELECT     dbo.DocumentInfo.DocID as DocumentID, 
   dbo.DocumentInfo.DocName as DocumentName, 
   dbo.DocumentInfo.Uploadfile as FileUploaded, 
    dbo.Userss.Email as UserEmail,
    dbo.Department.DepType as Department, 

    dbo.DocType.DocType as Document,
      dbo.DocumentInfo.UploadedBy as UploadedBy, 
      dbo.Approval.AppoveBy, dbo.ApproveType.ApproveType as Status
     FROM         dbo.DocumentInfo INNER JOIN
                  dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
        inner JOIN dbo.Userss on Userss.UserName =dbo.DocumentInfo.UploadedBy inner 
           JOIN
                  dbo.ApproveType ON dbo.Approval.ApproveID =     
         dbo.ApproveType.ApproveID INNER JOIN
                  dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
         INNER JOIN
                  dbo.DocType ON dbo.DocumentInfo.DocTypeID = dbo.DocType.DocTypeID

              end

这个sp的输出如下:

sp output

在上面,kanez是经理,而sundus是主管。

我只希望kanez批准的文件显示给导演以获得最终批准。现在,主管和经理批准的文件都将交给导演,但我只想向导演展示经理批准的文件。

我该怎么做?

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您提供样本数据/表格结构,那么我将提供比此更好的解决方案。

如果解决方案不起作用,那么解决方案之后的任何方式都会有效,那么我将提供另一种解决方案(如果发送结构)。

我认为DocId对于所有文档都是唯一的。

ALTER PROCEDURE [dbo].[ALLDOCUMNETS]
   AS
 begin
;with x as 
  (SELECT   ROW_NUMBER() over (partition by dbo.DocumentInfo.DocID
  ORDER BY dbo.Approval.seqno desc) ID,   dbo.DocumentInfo.DocID as DocumentID,
   dbo.DocumentInfo.DocName as DocumentName, 
   dbo.DocumentInfo.Uploadfile as FileUploaded, 
dbo.Userss.Email as UserEmail,
dbo.Department.DepType as Department, 

dbo.DocType.DocType as Document,
  dbo.DocumentInfo.UploadedBy as UploadedBy, 
  dbo.Approval.AppoveBy, dbo.ApproveType.ApproveType as Status
 FROM         dbo.DocumentInfo INNER JOIN
              dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
    inner JOIN dbo.Userss on Userss.UserName =dbo.DocumentInfo.UploadedBy inner 
       JOIN
              dbo.ApproveType ON dbo.Approval.ApproveID =     
     dbo.ApproveType.ApproveID INNER JOIN
              dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
     INNER JOIN
              dbo.DocType ON dbo.DocumentInfo.DocTypeID = dbo.DocType.DocTypeID

          end) SELECT DocumentID,DocumentName,FileUploaded,UserEmail,
          Department,Document,UploadedBy,AppoveBy,Status from x where X.Id=1

表结构不好。

在上一个查询中将updateddate更改为seqno desc,

然后你会得到最后更新的人记录他/她是什么