2014-01-05 37 views
-2

我嘗試創建一個SQL查詢計數,我試圖表明通知主管,經理,總監,當任何用戶上傳的在asp.net與計數查詢連接在SQL

查詢

ALTER procedure [dbo].[countdocuments] 
    @DepID int 
as 
    SELECT 
     COUNT(*), 
     Designation.DesigID 
    FROM DocumentInfo 
    INNER JOIN dbo.Userss ON dbo.Userss.DesigID = dbo.Designation.DesigID 
    WHERE 
     Userss.DesigID = 'Finance' 
新文件

但是當我執行它,以下錯誤發生

消息4104,級別16,狀態1,過程countdocuments,行7
多部分標識符「dbo.Design ation.DesigID「不能被綁定。
Msg 4104,Level 16,State 1,Procedure countdocuments,Line 5
無法綁定多部分標識符「Designation.DesigID」。

確定這是我designation

DesigID DesigType 
    1  SuperVisor 
    2  Manager 
    3  Director 
    4  BasicUsers 

,這是documentinfo

DocID DocDescrit DocName UploadedDate Uploadfile DocTypeID DepID ApproveID UploadedBy UserID 
32 asp.net codescomputer 2013-12-30 22:30:00.623 details.docx 1 2 1amna 24 

,這是userss

UserID UserName Password UserTypeID DepID CreateDate Email PhoeNumber DesigID 
21 john abc 2 NULL NULL 2013-12-02 22:01:03.903 NULL [email protected] 12313 4 

,當我嘗試這個闕RY

ALTER procedure [dbo].[countdocuments] 
    @DepID int 
as 
BEGIN 
    SET NOCOUNT ON; 

    SELECT 
     COUNT(*) AS Cnt, Designation.DesigID 
    FROM 
     Designation 
    INNER JOIN 
     dbo.Userss ON dbo.Userss.DesigID = dbo.Designation.DesigID 
    WHERE 
     Userss.DesigID = @DepID 
    GROUP BY 
     Designation.DesigID 
END 

當我嘗試用@depid = 4執行這樣的表現我這個樣子,我不明白的地方是4從哪裏來..

Cnt DesigID 
4  4 
+0

+5:O和沒有人回答我...:/ – user3134694

+2

這是因爲它無法找到表名稱中的列名稱DesigId。指定表在哪裏?或者您是否打算將別名添加到DocumentInfo中作爲指定表 – DevelopmentIsMyPassion

+0

您可以提供架構嗎? –

回答

0
ALTER procedure [dbo].[countdocuments] 
@DepID int 
as 
BEGIN 
    SET NOCOUNT ON; 
    SELECT COUNT(*) AS Cnt,Designation.DesigID 
    FROM DocumentInfo inner join dbo.Userss 
    on dbo.Userss.DesigID = dbo.DocumentInfo.DesigID 
    WHERE Userss.DesigID = @DepID 
    GROUP BY DocumentInfo.DesigID 

END 

您在不同的表名你的JOIN條款和ON條款。我試圖修復它,假設您的DocumentInfo表中有DesigID列。在您的評論

+0

我只在用戶和指定表中設計了coulmn – user3134694

+0

你和我的答案將不起作用,因爲他已經說過他沒有在documentInfo表 – DevelopmentIsMyPassion

+0

@ user3134694中有DesigId在'DocumentInfo'中是否有任何引用'Designation'中的任何列的列? –

0

尋找嘗試以下

ALTER procedure [dbo].[countdocuments] 
@DepID int 
as 
BEGIN 
SET NOCOUNT ON; 
SELECT COUNT(*) AS Cnt,Designation.DesigID 
FROM Designation left join dbo.Userss 
on dbo.Userss.DesigID = dbo.Designation.DesigID 
left Join DocumentInfo on Userss.UserId= DocumentInfo.UserId and Designation.DepId = DocumentInfo.DepId 
WHERE Userss.DesigID = @DepID 
GROUP BY Designation.DesigID 

END 

或者試試下面

Looking at your comment try following 

ALTER procedure [dbo].[countdocuments] 
@DepID int 
as 
BEGIN 
SET NOCOUNT ON; 
SELECT COUNT(*) AS Cnt,Designation.DesigID 
FROM DocumentInfo left Join Userss on DocumentInfo.UserId = Userss.UserId 
left Join Designation on DocumentInfo.DepId = Designation.DeptId 
and Designation.DesigID = Userss.DesigId 
WHERE Userss.DesigID = @DepID 
GROUP BY Designation.DesigID 

END 
+0

沒有列設計ID – user3134694

+0

那麼爲什麼你添加了DocumentInfo表。加入Desig表的地方在哪裏? – DevelopmentIsMyPassion

+0

@ user3134694用戶和Desig表之間的關係是什麼? – DevelopmentIsMyPassion