按指定显示文件

时间:2014-02-03 15:49:34

标签: asp.net sql sql-server stored-procedures

当只有导演批准/拒绝文件或待处理时,我想向用户显示此文件是批准/拒绝..

这是我尝试的存储过程

ALTER procedure [dbo].[spdocuments]
as
   SELECT    
      dbo.DocumentInfo.DocName as DocumentName,
      dbo.DocumentInfo.UploadedDate as UploadedDate , 
      dbo.DocumentInfo.Uploadfile as FileUploaded,   
      dbo.DocumentInfo.UploadedBy as  UploadedBy,
      dbo.Department.DepType as Department,
      dbo.Approval.ApproveDate,
      dbo.Approval.AppoveBy,
      dbo.ApproveType.ApproveType as Status
   FROM         
      dbo.DocumentInfo
   INNER JOIN
      dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
   INNER JOIN
      dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
   INNER JOIN
      dbo.ApproveType ON dbo.Approval.ApproveID = dbo.ApproveType.ApproveID 
   INNER JOIN
      dbo.ApproveType AS ApproveType_1 ON dbo.Approval.ApproveID = ApproveType_1.ApproveID
   WHERE      
      (dbo.Approval.DesigID = 3)

当我执行它时,它会向我显示这样的结果

╔══════════════╦══════════════╦══════════════════════╦════════════╦══════════════════╦═════════════╦══════════════╦══════════╗
║ DocumentName ║ UploadedDate ║     FileUploaded     ║ UploadedBy ║    Department    ║ ApproveDate ║   AppoveBy   ║  Status  ║
╠══════════════╬══════════════╬══════════════════════╬════════════╬══════════════════╬═════════════╬══════════════╬══════════╣
║ asdasd       ║ 2014-01-22   ║ CV.doc               ║ amna       ║ Human Resource   ║ 2014-01-22  ║ wajeeha      ║ Pending  ║
║ ERP SYSTEM   ║ 2014-01-20   ║ system admin.doc     ║ noreen     ║ Computer Science ║ 2014-01-22  ║ suman        ║ Approve  ║
║ asdasd       ║ 2014-01-22   ║ CV.docx              ║ amna       ║ Human Resource   ║ 2014-01-22  ║ suman        ║ Pending  ║
║ ERP SYSTEM   ║ 2014-01-20   ║ system admin .doc    ║ noreen     ║ Computer Science ║ 2014-02-03  ║ ayesha       ║ Pending  ║
║ asdasd       ║ 2014-01-22   ║ CV.docx              ║ amna       ║ Human Resource   ║ 2014-02-03  ║ ayesha       ║ Pending  ║
║ kanaban      ║ 2014-02-03   ║ Presentation1.pptx   ║ hira       ║ Finance          ║ 2014-02-03  ║ ayesha       ║ Approve  ║
║ ERP SYSTEM   ║ 2014-01-20   ║ system admin .docx   ║ noreen     ║ Computer Science ║ 2014-02-03  ║ wajeeha      ║ Pending  ║
║ def          ║ 2014-01-22   ║ BISE RAWALPINDI.docx ║ noreen     ║ Human Resource   ║ 2014-02-03  ║ wajeeha      ║ Approve  ║
║ kanaban      ║ 2014-02-03   ║ Presentation1.pptx   ║ hira       ║ Finance          ║ 2014-02-03  ║ wajeeha      ║ Pending  ║
║ def          ║ 2014-01-22   ║ BISE RAWALPINDI.docx ║ noreen     ║ Human Resource   ║ 2014-02-03  ║ suman        ║ Pending  ║
║ kanaban      ║ 2014-02-03   ║ Presentation1.pptx   ║ hira       ║ Finance          ║ 2014-02-03  ║ suman        ║ Pending  ║
╚══════════════╩══════════════╩══════════════════════╩════════════╩══════════════════╩═════════════╩══════════════╩══════════╝

在上面的表中重复两次或多次,我希望记录在单个时间表中每个记录都是单次

1 个答案:

答案 0 :(得分:0)

在您的原始问题(编辑之前)中,您说您希望所有“已批准”,然后所有“待定”(未批准)。您还提到一些Approved比其他一些更重要,但我无法从数据或您的描述中确定哪些。但是,此查询显示如何获取已批准(每个文档只有一行),然后是任何待定(尚未批准)。

顺便说一下,这也可以使用一些连接语句而不是联合来完成,但有时这种格式更容易调试(因为你可以注释掉一个联合集或对其进行处理)分开)。

--Approved
SELECT
  dbo.DocumentInfo.DocName as DocumentName,
  dbo.DocumentInfo.UploadedDate as UploadedDate , 
  dbo.DocumentInfo.Uploadfile as FileUploaded,   
  dbo.DocumentInfo.UploadedBy as  UploadedBy,
  dbo.Department.DepType as Department,
  Max(dbo.Approval.ApproveDate) AS ApproveDate,
  Max(dbo.Approval.AppoveBy) AS ApproveBy,
  dbo.ApproveType.ApproveType as Status
FROM         
  dbo.DocumentInfo
INNER JOIN
  dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
INNER JOIN
  dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
INNER JOIN
  dbo.ApproveType ON dbo.Approval.ApproveID = dbo.ApproveType.ApproveID 
GROUP BY 
  dbo.DocumentInfo.DocName as DocumentName,
  dbo.DocumentInfo.UploadedDate as UploadedDate, 
  dbo.DocumentInfo.Uploadfile as FileUploaded,   
  dbo.DocumentInfo.UploadedBy as  UploadedBy,
  dbo.Department.DepType as Department
WHERE      
  (dbo.Approval.DesigID = 3)
AND dbo.ApproveType.ApproveType = 'Approve'

UNION

--Pending
SELECT
  dbo.DocumentInfo.DocName as DocumentName,
  dbo.DocumentInfo.UploadedDate as UploadedDate , 
  dbo.DocumentInfo.Uploadfile as FileUploaded,   
  dbo.DocumentInfo.UploadedBy as  UploadedBy,
  dbo.Department.DepType as Department,
  Max(dbo.Approval.ApproveDate) AS ApproveDate,
  Max(dbo.Approval.AppoveBy) AS ApproveBy,
  dbo.ApproveType.ApproveType as Status
FROM         
  dbo.DocumentInfo
INNER JOIN
  dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
INNER JOIN
  dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
INNER JOIN
  dbo.ApproveType ON dbo.Approval.ApproveID = dbo.ApproveType.ApproveID 
GROUP BY 
  dbo.DocumentInfo.DocName as DocumentName,
  dbo.DocumentInfo.UploadedDate as UploadedDate, 
  dbo.DocumentInfo.Uploadfile as FileUploaded,   
  dbo.DocumentInfo.UploadedBy as  UploadedBy,
  dbo.Department.DepType as Department
WHERE      
  (dbo.Approval.DesigID = 3)
AND dbo.ApproveType.ApproveType = 'Pending'
AND dbo.DocumentInfo.DocID NOT IN
( 
  --omit the pending that have been approved
  SELECT
    dbo.DocumentInfo.DocID
  FROM         
    dbo.DocumentInfo
  INNER JOIN
    dbo.Department ON dbo.DocumentInfo.DepID = dbo.Department.DepID 
  INNER JOIN
    dbo.Approval ON dbo.DocumentInfo.DocID = dbo.Approval.DocID 
  INNER JOIN
    dbo.ApproveType ON dbo.Approval.ApproveID = dbo.ApproveType.ApproveID 
  GROUP BY 
    dbo.DocumentInfo.DocID as DocumentName,
    dbo.Department.DepType as Department
  WHERE      
    (dbo.Approval.DesigID = 3)
  AND dbo.ApproveType.ApproveType = 'Approved'
)