动态sql:无法找到多部分标识符错误

时间:2014-07-16 09:57:12

标签: sql sql-server

CREATE PROCEDURE sp_ViewEffortSummary
            @UserID                         INT         ,
            @Project_Name                   nVARCHAR(40),
            @Date                           DATETIME    
        AS 
        BEGIN     
    DECLARE @query          nVARCHAR(max)
    DECLARE @T_EmpID        nVARCHAR(10)
    DECLARE @T_ProjName     nVARCHAR(40)
    DECLARE @T_Date         nVARCHAR(15)
    SET @T_EmpID = convert(VARCHAR(10),@UserID);
    SET @T_ProjName = @Project_Name;
    SET @T_Date = convert(VARCHAR(15),@Date,111);
    SET @query = 'select '''+@T_EmpID+''' as EmployeeID';

    IF((select Proj_Team_Setup
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName
        and Date_of_work = @Date) is not null)
        SET @query += ',Proj_Team_Setup';
    IF((select Infra_Setup
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',Infra_Setup';
    IF((select tblPSU.Doc_Work 
        from tblPSU where 
        Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',tblPSU.Doc_Work';
    IF((select tblPSU.Rework 
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName
        and Date_of_work = @Date) is not null)
        SET @query += ',tblPSU.Rework';    


    IF((select Proj_Mgmt_Telcom 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',Proj_Mgmt_Telcom';
    IF((select tblPC.Doc_Work 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',tblPC.Doc_Work';
    IF((select tblPC.Rework 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',tblPC.Rework'; 


    IF((select Cust_Test_Proces 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',Cust_Test_Proces';
    IF((select tblKT.Doc_Work 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',tblKT.Doc_Work';
    IF((select tblKT.Rework 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',tblKT.Rework';     

    SET @query += ' from tblPSU a 
                    join tblPC b 
                    on a.Employee_ID = b.Employee_ID 
                    and a.Project_Name = b.Project_Name 
                    and a.Date_of_work = b.Date_of_work 
                    join tblKT c 
                    on b.Employee_ID = c.Employee_ID 
                    and b.Project_Name = c.Project_Name 
                    and b.Date_of_work = c.Date_of_work 
                    where 
                    a.Employee_ID = '+@T_EmpID+'
                    and a.Project_Name = '+@T_ProjName+
                    ' and a.Date_of_work = '''+@T_Date+''''
    IF (@query IS NOT NULL)
    Exec (@query)
    END

    EXEC sp_ViewEffortSummary '2312456','abc','2014-06-17 00:00:00.000'

执行此proc我正在检索错误

Invalid column name 'abc' and 
The multi-part identifier "tblPC.Doc_Work" could not be bound

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

您使用tblPSUtblPC的别名,并且必须使用 Alias.YourColumnName ,例如使用a.Doc_Workb.Doc_Work代替{ {1}}和tblPSU.Doc_Work

tblPS.Doc_Work

答案 1 :(得分:0)

您的查询中存在以下问题:

  1. 在第一个声明中你写了@UserID INT但是 执行Sproc时,您已将其写为字符串'2312456'。它 应如下所示为2312456,否则将声明更改为 nVARCHAR(10)
  2. 当表格别名时,请使用别名来引用“a.Proj_Team_Setup ..
  3. 之类的选择中的列”
  4. 由于abc是一个字符串,因此应使用正确的引号在最终查询中将其重复为“abc”。
  5. 以下应该工作:

    CREATE PROCEDURE sp_ViewEffortSummary
                @UserID INT,
                @Project_Name nVARCHAR(40),
                @Date DATETIME  
            AS 
            BEGIN     
    DECLARE @query          nVARCHAR(max)
    DECLARE @T_EmpID        nVARCHAR(10)
    DECLARE @T_ProjName     nVARCHAR(40)
    DECLARE @T_Date         nVARCHAR(15)
    SET @T_EmpID = convert(VARCHAR(10),@UserID);
    SET @T_ProjName = @Project_Name;
    SET @T_Date = convert(VARCHAR(30),@Date,111);
    SET @query = 'select '''+@T_EmpID+''' as EmployeeID';
    
    IF((select Proj_Team_Setup
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName
        and Date_of_work = @Date) is not null)
        SET @query += ',a.Proj_Team_Setup';
    IF((select Infra_Setup
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',a.Infra_Setup';
    IF((select tblPSU.Doc_Work 
        from tblPSU where 
        Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',a.Doc_Work';
    IF((select tblPSU.Rework 
        from tblPSU 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName
        and Date_of_work = @Date) is not null)
        SET @query += ',a.Rework';    
    
    
    IF((select Proj_Mgmt_Telcom 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',b.Proj_Mgmt_Telcom';
    IF((select tblPC.Doc_Work 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',b.Doc_Work';
    IF((select tblPC.Rework 
        from tblPC 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',b.Rework'; 
    
    
    IF((select Cust_Test_Proces 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',c.Cust_Test_Proces';
    IF((select tblKT.Doc_Work 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',c.Doc_Work';
    IF((select tblKT.Rework 
        from tblKT 
        where Employee_ID = @T_EmpID 
        and Project_Name = @T_ProjName 
        and Date_of_work = @Date) is not null)
        SET @query += ',c.Rework'; 
        SET @query += ' from tblPSU a 
                        join tblPC b 
                        on a.Employee_ID = b.Employee_ID 
                        and a.Project_Name = b.Project_Name 
                        and a.Date_of_work = b.Date_of_work 
                        join tblKT c 
                        on b.Employee_ID = c.Employee_ID 
                        and b.Project_Name = c.Project_Name 
                        and b.Date_of_work = c.Date_of_work 
                        where 
                        a.Employee_ID = '''+@T_EmpID+'''
                        and a.Project_Name = '''+@T_ProjName+
                        ''' and a.Date_of_work = '''+@T_Date+''''
    
    IF (@query IS NOT NULL)
        Exec (@query)
        END
    
        EXEC sp_ViewEffortSummary 2312456 ,'abc','2014-06-17 00:00:00.000'
    

答案 2 :(得分:0)

使用提供的别名(' a' /' b')加入并使用"引号"在projectname(nvarchar).try的动态查询中,带有a.Project_Name ='' + @ T_ProjName +'''。日期列有它