SP_ExecuteSQL incorrect syntax

  • I have a sproc in SQL Server 2000 that dynamically builds a sql statement and then executes it using sp_executesql.

    When I execute it, however, I get an error message "Incorrect Syntax near" and then it return the first 128 characters of my SQL Statement.

    If I "print @sql" the statement before it is passed into sp_executesql and then declare and put it in a nvarchar(4000) variable and pass it to sp_executesql outside the stored proc it works fine.

    Here is the sproc. What am I missing?

    ALTER PROCEDURE Activity AS

    Declare @id as nvarchar(2)

    Declare @sql as nvarchar(4000)

    Declare @fld as nvarchar(7)

    set @id = 3

    Select @fld = colname from ae_adefs where datatypeid = 5 and appid = @id

    Declare @mth as nvarchar(2)

    Declare @yr as nvarchar(4)

    Declare @cnt as int

    Set @cnt = 12

    Select @mth = month(Dateadd(month, -@cnt, getdate()))

    Select @yr = year(Dateadd(month, -@cnt, getdate()))

    While @cnt > 0

    Begin

    If @sql is null

    set @sql = char(39) + 'Select ' + @id + ' as AppID,' + cast(@mth as nvarchar(2)) + ' as Month,' + cast(@yr as nvarchar(4)) + ' as DaYear, sum(numobjects) as pages_Added from ae_dt' + @id + ' where month(' + @fld + ') = ' + cast(@mth as nvarchar(2)) + ' and year(' + @fld + ') = ' + @yr

    Else

    set @sql = @sql + ' Select ' + @id + ' as AppID,' + cast(@mth as nvarchar(2)) + ' as Month,' + cast(@yr as nvarchar(4)) + ' as DaYear, sum(numobjects) as pages_Added from ae_dt' + @id + ' where month(' + @fld + ') = ' + cast(@mth as nvarchar(2)) + ' and year(' + @fld + ') = ' + @yr

    If @cnt > 0

    Begin

    set @sql = @sql + char(10)

    set @sql = @sql + ' Union All'

    set @sql = @sql + char(10)

    End

    Set @cnt = @cnt - 1

    set @mth = month(Dateadd(month, -@cnt, getdate()))

    set @yr = year(Dateadd(month, -@cnt, getdate()))

    End

    set @sql = substring(@sql, 0, len(@sql) - 9) + char(39)

    print @sql

    Exec sp_executesql @sql

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

  • added a few more '

    --ALTER PROCEDURE Activity

    --AS

    Declare @id as nvarchar(2)

    Declare @sql as nvarchar(4000)

    Declare @fld as nvarchar(7)

    set @id = 3

    SET @fld = 'test'

    --Select @fld = colname from ae_adefs where datatypeid = 5 and appid = @id

    Declare @mth as nvarchar(2)

    Declare @yr as nvarchar(4)

    Declare @cnt as int

    Set @cnt = 12

    Select @mth = month(Dateadd(month, -@cnt, getdate()))

    Select @yr = year(Dateadd(month, -@cnt, getdate()))

    While @cnt > 0

    Begin

    If @sql is null

    BEGIN

    set @sql = char(39) + 'Select ' + @id + ' as AppID,'

    + cast(@mth as nvarchar(2)) + ' as Month,'

    + cast(@yr as nvarchar(4))

    + ' as DaYear, sum(numobjects) as pages_Added from ae_dt '

    + @id + ' where month(''' + @fld + ''') = '

    + cast(@mth as nvarchar(2)) + ' and year(''' + @fld + ''') = '

    + @yr

    PRINT @sql

    END

    Else

    BEGIN

    set @sql = @sql + ' Select ' + @id + ' as AppID,'

    + cast(@mth as nvarchar(2)) + ' as Month,'

    + cast(@yr as nvarchar(4))

    + ' as DaYear, sum(numobjects) as pages_Added from ae_dt'

    + @id + ' where month(''' + @fld + ''') = '

    + cast(@mth as nvarchar(2)) + ' and year(''' + @fld + ''') = '

    + @yr

    PRINT @sql

    END

    If @cnt > 0

    Begin

    set @sql = @sql + char(10)

    set @sql = @sql + ' Union All'

    set @sql = @sql + char(10)

    End

    Set @cnt = @cnt - 1

    set @mth = month(Dateadd(month, -@cnt, getdate()))

    set @yr = year(Dateadd(month, -@cnt, getdate()))

    End

    set @sql = substring(@sql, 0, len(@sql) - 9) + char(39)

    print @sql

    Exec sp_executesql @sql

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

    Facts are stubborn things, but statistics are more pliable - Mark Twain
    Carolyn
    SQLServerSpecialists[/url]

  • Try removing the your char(39)'s.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Thanks for your responses.

    I found that if I removed the leading and trailing char(39)'s and instead put a leading "Begin" and trailing "End" like so

    set @sql = 'Begin ' + char(10) + @sql + char(10) + 'End'

    everything works like a charm.

  • You shouldn't need the begin and end either or the line feeds either.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply