开发者

T-Sql Dynamic Query String Limit

I am getting a weird exception saying " "

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."

I am executing the following query. "

{

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 Com开发者_开发百科pany_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 

}

I cannot understand what exactly the problem is? Do we have some limits of characters while executing Dynamic Sql like I am doing. I tried printing what I am getting in @Sql, I can get the results what I am looking for. But when I go like this using EXEC @Sql it gives me the error.

Any suggestions?


Try Declaring @sql as nvarchar(max) then

exec sp_executesql @sql

instead of

exec @sql

Executing dynamic sql with just exec is very limited. See this link http://www.sommarskog.se/dynamic_sql.html.


Try changing your last line to EXEC (@sql).

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

Source:

  • EXECUTE (Transact-SQL)


You can use:

  1. Execute or exec: EXEC (@sql)

  2. The system stored procedure sp_execute. Benefits: can use additional parameters in the syntax and validate the data types before execution: execute sp_execute @sql

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜