tsql - Dynamic SQL used in Case Statement in Stored Procedure -


i developing proof of concept application requested client act automation system initial scrub of input data. 1 of steps in process @ description field , determine how maps clients provided description/code list specific code. thought of using giant case statement, however, due input data not being same, figured incorporate dynamic sql pass column name of input description field. below snippet of code far (the ellipses (...) represent code continuing unknown distance @ point , not in code.):

declare @desc varchar(100) set @desc = '<sourcedescrptioncolumn>'  declare @sql nvarchar(max) set @sql = '             select                <sourcedescrptioncolumn>               ,case when( ' + @desc + ' ''%club%'') ''00''                     when( ' + @desc + ' ''%ball%''  ,  ' + @desc + '  not ''%basket%'' , ' + @desc + ' not ''%base%'') ''01''                     when( ' + @desc + ' ''%glov%''  ,  ' + @desc + '  not ''%bat%'' , ' + @desc + ' not ''%golf%'' , ...) ''02''                     ...                     esle ''99''                 end desc_code             <someinputtable>' 

this work first 30 or "when" statements , begins fail. appears issue use of single quotes, cannot seem locate source.

are there limits number of ands or whens can used in type of sql usage? there better alternative?

what happening data being truncated others have mentioned. unless using unicode characters, i'd recommend sticking varchar. either way, set variables max , should fix truncation errors. , reason think quotes, because if truncate end quotes, of course you'll have unclosed quotes!

declare @desc varchar(max) = '<sourcedescrptioncolumn>'; declare @sql  varchar(max); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -