Query converted into Dynamic SQL string within Stored Proc yields syntax error

  • I've created a stored procedure that executes a dynamically generated query string based on a single input parameter. The resulting data looks something like this:

    emp_id display_name

    1 Alan (234)

    2 Bob (245234)

    3 Carl ()

    4 Christina (9556)

    5 Doug ()

    etc.

    This is the working SELECT statement that I use to retrieve the data:

    'SELECT emp_id, emp_name + '' '' + ''('' + CAST(COALESCE(emp_number,0) AS varchar(10)) + '')'' AS display_name FROM Employee ORDER BY emp_name'

    If an individual does not possess an emp_number the parentheses will still display regardless, and I wanted to clean this up a bit and remove them so I modified the above SELECT statement to look like this:

    'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') +' ')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name'

    This new query executes in SSMS Query Designer and yields the correct results (no parentheses without an accompanying emp_number), however, when I add this to the stored proc and execute it I receive "Incorrect syntax near ','."

    I've started to go blind trying to figure out where I went wrong with doubling up my quotes and was hoping another few sets of eyes could help me determine where I've gone wrong.

    Thanks!

  • jabberpunch (1/7/2011)


    I've created a stored procedure that executes a dynamically generated query string based on a single input parameter. The resulting data looks something like this:

    emp_id display_name

    1 Alan (234)

    2 Bob (245234)

    3 Carl ()

    4 Christina (9556)

    5 Doug ()

    etc.

    This is the working SELECT statement that I use to retrieve the data:

    'SELECT emp_id, emp_name + '' '' + ''('' + CAST(COALESCE(emp_number,0) AS varchar(10)) + '')'' AS display_name FROM Employee ORDER BY emp_name'

    If an individual does not possess an emp_number the parentheses will still display regardless, and I wanted to clean this up a bit and remove them so I modified the above SELECT statement to look like this:

    'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') +' ')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name'

    This new query executes in SSMS Query Designer and yields the correct results (no parentheses without an accompanying emp_number), however, when I add this to the stored proc and execute it I receive "Incorrect syntax near ','."

    I've started to go blind trying to figure out where I went wrong with doubling up my quotes and was hoping another few sets of eyes could help me determine where I've gone wrong.

    Thanks!

    Right where I put the asterisk (*) you had a space...

    'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') +'*')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name'

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Good eyes magoo!! Unfortunately that was just a typo on my part and there is no space in the stored proc, sorry about that.

    'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') + '')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name'

    When I run my stored proc it yields the syntax error "Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ','." and the problem area (comma) it's referring to is located between the two expressions of the NULLIF function...It's the first time I've used NULLIF so I hope I'm using this correctly?

    Again, thanks for any feedback!

  • I just tried to print your dynamic statement and it returned:

    SELECT emp_id, emp_name + COALESCE(NULLIF('(' + COALESCE(CAST(emp_number AS varchar(10)),') +')','(' + ')'),') AS display_name from Employee ORDER BY emp_name

    It seems like there are a few obsolete quotation marks...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • I don't know about obsolete(?) quotation marks, but simply not enough...I neglected to double up the quotes in either COASLESCE function, and now that I have it's working.

    'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'''') + '')'',''('' + '')''),'''') AS display_name from Employee ORDER BY emp_name'

  • I guess I'd like to know why this has to be in dynamic SQL to begin with.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/10/2011)


    I guess I'd like to know why this has to be in dynamic SQL to begin with.

    LOL I was thinking the same thing. Looks like dynamically hard coded sql to me. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Viewing 7 posts - 1 through 7 (of 7 total)

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