Lisa Callahan
asked on
Dynamic SQL CASE STATEMENT in a text string
I have created some code that will replace values using one long string. The problem is that the syntax is different if there is a NULL value in the variable.
I have the following SQL:
SET Parameters = '<ParameterValues><Paramet erValue><N ame>SPLC</ Name><Valu e>' + @SPLC + '</Value></ParameterValue> </Paramete rValues>'
that I want to replace with a CASE STATEMENT for the @SPLC value:
CASE WHEN @SPLC is null THEN '</ParameterValue>' ELSE '<Value>' + @SPLC + '</Value></ParameterValue> '
so the new code would look like this:
SET Parameters = '<ParameterValues><Paramet erValue><N ame>SPLC</ Name><Valu e>' + CASE WHEN @SPLC is null THEN '</ParameterValue>' ELSE '<Value>' + @SPLC + '</Value></ParameterValue> ' + '</ParameterValues>'
But I can't figure it out.
Thank you!
I have the following SQL:
SET Parameters = '<ParameterValues><Paramet
that I want to replace with a CASE STATEMENT for the @SPLC value:
CASE WHEN @SPLC is null THEN '</ParameterValue>' ELSE '<Value>' + @SPLC + '</Value></ParameterValue>
so the new code would look like this:
SET Parameters = '<ParameterValues><Paramet
But I can't figure it out.
Thank you!
ASKER
Thank you. That does get me a bit further but you are correct in that is not what I am after. I want the string to look like this if the value is not null:
<ParameterValues><Paramete rValue><Na me>SPLC</N ame><Value >' + @SPLC + '</Value></ParameterValue> </Paramete rValues>
and this if the value is null:
<ParameterValues><Paramete rValue><Na me>SPLC</N ame></Para meterValue ></Paramet erValues>
<ParameterValues><Paramete
and this if the value is null:
<ParameterValues><Paramete
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YES!
This is exactly what I am after. Thank you for the very helpful explanation as well. I am always trying to improve my coding :)
This is exactly what I am after. Thank you for the very helpful explanation as well. I am always trying to improve my coding :)
You're welcome. Glad it helped :)
Open in new window
But that produces this, which isn't valid XML (if that's what you're after)
<ParameterValues><Paramete