Shaiwal Tripathi Shaiwal Tripathi - 5 months ago 19
SQL Question

How to Parameterize query with special character in sql server

I want to execute query with concatenation of two columns so I have done this

Select Id, PtName + ' ('+Investigation+')' as PtName, Y, M, D, PtCode from DiagMain


But when I am trying parameterize same query It's not working. Like this

declare @Query nvarchar(MAX)
set @Query = 'Select Id, PtName + ''( +''Investigation''+ )'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'
Exec(@Query)


What I am doing wrong here ?

Answer Source

Your single quotes were misplaced, they should surround the brackets ( & ):

set @Query = 'Select Id, PtName + ''('' +Investigation+ '')'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'

You could debug this by using the print command:

print @Query