sql server - Dynamic SQL and carriage breaks -
i trying insert table dynamic sql following code:
set @sqlinsert = 'insert ' + @tablename + ' (op__docid, op__parentid, op__folderid, clientkey, admissionkey, pgmkey, ' + 'pgmadmissionkey, staffkey, groupsize, mealtime, appmgrkey, sessionnum, facilitykey, ttmnotetext,' + @datefield + ', ' + @timefield + ') ' set @sqlvalues = 'values (' + cast(@newdocid varchar) + ', ' + cast(@clientkey varchar) + ', ' + cast(@clientkey varchar) + ', ' + cast(@clientkey varchar) + ', ' + cast(@admissionkey varchar) + ', ' + isnull(cast(@pgmkey varchar),'') + ', ' + isnull(cast(@pgmadmissionkey varchar),'') + ', ' + cast(@staffkey varchar) + ', ' + cast(@groupsize varchar) + ', ' + cast(@mealtime varchar) + ', ' + cast(@apprvmgr varchar) + ', ' + cast(@sessionnum varchar) + ', ' + cast(@facilitykey varchar) + ', ' --added 10/30/15 jf fc notes + @ttmnotetext +',' --added 10/30/15 jf fc notes + 'null' + ', ' + 'null' + ')' exec(@sqlinsert + @sqlvalues)
and want @ttmnotetext
include char(10)
, char(13)
following
set @ttmnotetext='purpose statement: '+char(13)+char(10) --added 10/30/15 jf fc notes + 'family vision: '+char(13)+char(10) + 'strengths: '+char(13)+char(10) + 'challenges: '+char(13)+char(10) + 'updates: '+char(13)+char(10) + 'progress in treatment goals/itfc program: '+char(13)+char(10) + 'services/supports needed: '+char(13)+char(10) + 'plan:'
i know not using correct number of single quotes or that. possible insert line/carriage breaks using dynamic sql
your entire @sqlvalues
variable missing lot of quotes. i'm not sure if accurate, need put single quotes around string values, since you're building dynamic statement, need escaped (escape '
'
):
set @sqlvalues = 'values (''' + cast(@newdocid varchar) + ''', ''' + cast(@clientkey varchar) + ''', ''' + cast(@clientkey varchar) + ''', ''' + cast(@clientkey varchar) + ''', ''' + cast(@admissionkey varchar) + ''', ''' + isnull(cast(@pgmkey varchar),'') + ''', ''' + isnull(cast(@pgmadmissionkey varchar),'') + ''', ''' + cast(@staffkey varchar) + ''', ''' + cast(@groupsize varchar) + ''', ''' + cast(@mealtime varchar) + ''', ''' + cast(@apprvmgr varchar) + ''', ''' + cast(@sessionnum varchar) + ''', ''' + cast(@facilitykey varchar) + ''', ''' --added 10/30/15 jf fc notes + @ttmnotetext +''',' --added 10/30/15 jf fc notes + 'null' + ', ' + 'null' + ')'
secondly, i'm not sure you're allowed use expressions within exec
. might need concatenation before passing exec
:
set @sqlinsert = @sqlinsert + @sqlvalues exec(@sqlinsert)
Comments
Post a Comment