2013-11-25 39 views
0

我試過這個存儲過程來顯示文件:只批准文件應顯示

ALTER procedure [dbo].[financespmanager] 
    @DepID int 
as 
    SELECT  
     dbo.DocumentInfo.DocID as DocumentID, 
     dbo.DocumentInfo.DocName as DocumentName, 
     dbo.DocumentInfo.Uploadfile as FileUploaded, 
     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.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 
    WHERE  
     DocumentInfo.DepID = @DepID 

現在,當我測試查詢拿給我這個樣子

DocumentID DocumentName FileUploaded Departmen UploadedBy AppoveBy Status 
25  css   dsfsdf.docx  Computer edward  halten  Rejected 
18  hr   HR.docx   Computer john   halten  Approved 
19  eco   eco 2.docx  Computer kety   halten  Rejected 
24  plan   December8.pdf Computer edward  halten  Approved 

現在,這裏我想說明只批准文件沒有拒絕和正在申請的文件

我該怎麼做?

回答

2

你只需要在你的WHERE第一個附加條件:

WHERE  
    DocumentInfo.DepID = @DepID 
    AND dbo.ApproveType.ApproveType = 'Approved' 

應該這樣做。

+0

當我嘗試你的查詢時,它顯示我errro」轉換失敗時轉換varchar值'批准'爲數據類型int「 – user2931015

+0

@ user2931015:看到我的更新 - 你可能有一個名爲'狀態' ' - 而且你還在'dbo.ApproveType.ApproveType'列中使用'Status'作爲列別名 - 這相當令人困惑,我會說... –

+0

當我像這樣使用AND AND St​​atus ='Approve'then它顯示我錯誤歧義列名'狀態'。當我使用像這樣ApproveType.ApproveID ='批准'然後它顯示錯誤轉換varchar值'Approve'到數據類型int轉換失敗int – user2931015

1

試...

AND ApproveType.ApproveType = 'Approved'在那裏where條款。

,或者改變存儲過程採取第二PARAM ...

ALTER procedure [dbo].[financespmanager] 
    @DepID int, 
    @Status varchar(30) = 'Approved' 
as 
--rest of sp 
WHERE 
DocumentInfo.DepID = @DepID 
AND ApproveType.ApproveType = @Status 

默認將Approved,但你也可以通過在另一個值來覆蓋...

EXEC [dbo].[financespmanager] @DepID = n, @Status = 'Rejected' 

這將允許您使用SP「原樣」,但也提供了覆蓋的靈活性。 「

+0

感謝您的幫助 – user2931015

+0

@ user2931015,當您使用'status'時沒有看到我的答案不適合您? – christiandev

+0

我實施了這兩個查詢和兩個作品 – user2931015