T-Sql动态查询字符串限制

时间:2011-07-23 16:22:15

标签: sql tsql dynamic-sql

我得到一个奇怪的例外说“ “

Msg 203, Level 16, State 2, Line 53
The name 'SELECT 
        ISNULL(ESP.ID,-1) AS 'PayScaleID',
        ISNULL(E.Prefix,'') + ISNULL(E.SerialNo,0)   AS 'Token number',
        ISNULL(E.FirstName,'')+' '+ ISNULL(E.MiddleName,'')+' '+ISNULL(E.LastName,'') AS 'Employee name',   
        ISNULL(E.CostCentreCode,'') AS 'Cost centre code',
        ESP.TotalPresentDays AS 'Present days',
        ESP.TotalWeeklyOffDays AS 'Week offs',
        ESP.TotalPaidHolidays AS 'Paid holiDays',
        ESP.TotalAbsentDays AS 'Absent days',
        ESP.ArrearDays AS 'Arrear days',
        ESP.OTHours AS 'OTHours',
        ESP.TotalPaidAmount AS 'Total Amount',
        MAD.Name AS 'Allowance' ,
        X.Amount 
    INTO #temp
    FROM 
    (
        SELECT 
            -1 AS 'BillingI' is not a valid identifier."

我正在执行以下查询。 “

{

DECLARE @sql AS VARCHAR(8000)

SET @sql = 'SELECT 
        ISNULL(ESP.ID,-1) AS ''PayScaleID'',
        ISNULL(E.Prefix,'''') + ISNULL(E.SerialNo,0)   AS ''Token number'',
        ISNULL(E.FirstName,'''')+'' ''+ ISNULL(E.MiddleName,'''')+'' ''+ISNULL(E.LastName,'''') AS ''Employee name'',   
        ISNULL(E.CostCentreCode,'''') AS ''Cost centre code'',
        ESP.TotalPresentDays AS ''Present days'',
        ESP.TotalWeeklyOffDays AS ''Week offs'',
        ESP.TotalPaidHolidays AS ''Paid holiDays'',
        ESP.TotalAbsentDays AS ''Absent days'',
        ESP.ArrearDays AS ''Arrear days'',
        ESP.OTHours AS ''OTHours'',
        ESP.TotalPaidAmount AS ''Total Amount'',
        MAD.Name AS ''Allowance'' ,
        X.Amount 
    INTO #temp
    FROM 
    (
        SELECT 
            -1 AS ''BillingID'',
            ESP.ID AS ''EmployeeSalaryPaidID'',
            MAd.ID AS ''AllowanceID'',
            ESPD.PaidAmount AS ''Amount''
        FROM Employee_SalaryPaid ESP
        INNER JOIN Employee_SalaryPaidDetails ESPD ON ESPD.EmployeeSalaryPaidID = ESP.ID 
        INNER JOIN Employee E ON E.ID = ESP.EmployeeID 
        INNER JOIN Master_AllowanceDeduction MAD ON MAD.ID = ESPD.AllowanceDeductionID 
        WHERE ESP.YearAndMonth =201104
        UNION ALL
        SELECT
            CBR.ID AS ''BillingID'',
            CBRD.EmployeeSalaryPaidID,
            CBRD.AllowanceDeductionID AS ''AllowanceID'',
            CBRD.Amount AS ''Amount''

        FROM Company_BillRaised CBR
        INNER JOIN Company_BillRaisedDetails CBRD ON CBRD.BillRaisedID = CBR.ID
        WHERE CBR.MonthYear =201104
    )X 
    INNER JOIN Master_AllowanceDeduction MAD ON MAD.ID = X.AllowanceID
    INNER JOIN Employee_SalaryPaid ESP ON ESP.ID =X.EmployeeSalaryPaidID
    INNER JOIN Employee E ON E.ID= ESP.EmployeeID
    INNER JOIN dbo.vw_CompanyEmployeeIDs CE ON CE.EmployeeID = E.ID 
    WHERE CE.BranchID =73
DROP TABLE #temp'

 EXEC @sql 

}

我无法理解究竟是什么问题?像我一样执行Dynamic Sql时,我们是否有一些字符限制。我尝试打印我在@Sql中获得的内容,我可以得到我想要的结果。但是当我使用EXEC @Sql这样的时候,它给了我错误。

有什么建议吗?

3 个答案:

答案 0 :(得分:5)

尝试 将@sql声明为nvarchar(max) 那么

exec sp_executesql @sql

而不是

exec @sql

使用exec执行动态sql非常有限。请参阅此链接http://www.sommarskog.se/dynamic_sql.html

答案 1 :(得分:4)

尝试将最后一行更改为EXEC (@sql)

Execute a character string
{ EXEC | EXECUTE } 
    ( { @string_variable | [ N ]'tsql_string' } [ + ...n ] )
    [ AS { LOGIN | USER } = ' name ' ]
[;]

来源:

答案 2 :(得分:1)

您可以使用:

  1. 执行或执行:EXEC (@sql)

  2. 系统存储过程sp_execute。好处:可以在语法中使用其他参数并在执行前验证数据类型:execute sp_execute @sql