sp_executesql

  • I am not seeing the syntax error, help anyone?

    DECLARE @sourceTable nvarchar(50)

    DECLARE @sourceFieldId nvarchar(50)

    DECLARE @sourceDisplayField nvarchar(50)

    DECLARE @OutString nvarchar(500)

    DECLARE @strSql nvarchar(500)

    DECLARE @params nvarchar(20)

    SET @strSql =

    N'SELECT @Concat = ISNULL(@Concat + '', '', '''') + ISNULL(a.' + @sourceDisplayField + ', '''')' + CHAR(10)

    + 'FROM dbo.DataTable dt' + CHAR(10)

    + 'JOIN ' + @sourceTable + ' a ON dt.FieldValueId = a.' + CAST(@sourceFieldId AS varchar)+ CHAR(10)

    + 'WHERE dt.ddId = ' + CAST(@DocDataId AS varchar)

    + ' AND dt.dfOrgId = ' + CAST(@DocumentFieldOrgId AS varchar)

    SET @params = N'@Concat nvarchar(500) OUTPUT'

    EXECUTE sp_executesql @strSql, @params, @Concat = @OutString OUTPUT

    --This is the resulting string--

    SELECT @Concat = ISNULL(@Concat + ', ', '') + ISNULL(a.ServiceArea, '')

    FROM dbo.DataTable dt

    JOIN dbo.serviceArea a ON dt.FieldValueId = a.ServiceAreaId

    WHERE dt.ddId = 1 AND dt.dfOrgId = 151

  • Have you tried just running the resulting command?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • what error are you getting?

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Incorrect syntax near the keyword 'SELECT'.

  • yes, the output runs fine directly.

  • OK I don't get the same error, but here is what I think.

    If any of your variables that help build your string are null then it will all fall over.

    I would recommened using isullout side of the acutally string incase of this situation.

    I'll keep looking to see if there is anything else.

    Could you also give us some examples of your variable values?

    Thanks

    Chris

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Sorry , my bad, I have all the data I need 🙂

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Wow that was driving me crazy.

    the variable:

    @params is too short so it's causing everthing to fall over.

    Extend the length and all should be sorted 🙂

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Stobbs for president! Thanks 🙂

  • Just try this query,

    DECLARE @sourceTable nvarchar(50)

    DECLARE @sourceFieldId nvarchar(50)

    DECLARE @sourceDisplayField nvarchar(50)

    DECLARE @OutString nvarchar(500)

    DECLARE @strSql nvarchar(500)

    DECLARE @params nvarchar(20)

    DECLARE @DocDataId varchar(20)

    DECLARE @Concat varchar(200)

    SET @strSql =

    N'SELECT @Concat = ISNULL(@Concat + '', '', '''') + ISNULL(a.' + @sourceDisplayField + ', '''')' + CHAR(10)

    + 'FROM dbo.DataTable dt' + CHAR(10)

    + 'JOIN ' + @sourceTable + ' a ON dt.FieldValueId = a.' + CAST(@sourceFieldId AS varchar)+ CHAR(10)

    + 'WHERE dt.ddId = ' + CAST(@DocDataId AS varchar)

    + ' AND dt.dfOrgId = ' + CAST(@DocumentFieldOrgId AS varchar)

    SET @params = N'@Concat nvarchar(500) OUTPUT'

    EXECUTE sp_executesql @strSql, @params, @Concat = @OutString OUTPUT

    --This is the resulting string--

    SELECT @Concat = ISNULL(@Concat + ', ', '') + ISNULL(a.ServiceArea, '')

    FROM dbo.DataTable dt

    JOIN dbo.serviceArea a ON dt.FieldValueId = a.ServiceAreaId

    WHERE dt.ddId = 1 AND dt.dfOrgId = 151

    Thanks and Regards,
    Venkatesan Prabu, 😛
    My Blog:

    http://venkattechnicalblog.blogspot.com/

Viewing 10 posts - 1 through 9 (of 9 total)

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