January 7, 2011 at 4:51 pm
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!
January 7, 2011 at 5:27 pm
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);
January 8, 2011 at 11:28 am
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!
January 8, 2011 at 1:19 pm
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...
January 10, 2011 at 12:54 pm
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'
January 10, 2011 at 6:47 pm
I guess I'd like to know why this has to be in dynamic SQL to begin with.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 11, 2011 at 8:21 am
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