Retrieving a SQL expression from a data field?

I need some help with how SQL works. Trying to get an auto-salutation to be generated from data entered in the Gen_Tables.

My thinking was that we could put a SQL expression into the description column in Gen_Tables and then use that as a string in a query, but it does not recognize it as SQL, only as a string.

E.G. the value in the field is: Name.FIRST_NAME+' '+Name.LAST_NAME

Store that value in a variable @value

I was hoping I could use that in a query like this: SELECT @value from Name where ID=’10000’

However that select statement just returns the string “Name.FIRST_NAME+' '+Name.LAST_NAME”

Know any way to tell SQL to recognize that as an expression, not a string?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

You'll need to construct the

You'll need to construct the whole query as dynamic sql and pass it to sp_executesql:

declare @sql nvarchar(4000)
set @sql = 'SELECT ' + @value + ' FROM [Name] WHERE [ID] = ''10000'''
EXEC sp_executesql @sql

Note that you want to be really, really careful about what you pass to this and how; see http://www.sommarskog.se/dynamic_sql.html for a more complete discussion.

Terrific! I see how that

Terrific! I see how that works. Thanks.
Ted Ericson. ASI/Product Management