Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-10-07 : 07:09:41
|
| Kostas writes "HelloI am trying to execute the following code:declare @theparameter nvarchardeclare @thefieldname nvarcharselect @theparameter = 'a'select @thefieldname = 'fldprodcode'--select * from tblProducts where @thefieldname like @theparameter + '%'select * from tblProducts where fldprodcode like @theparameter + '%'The first select statement, where i use a parameter as the field name will not work. Is there any workaround for this?Thank you" |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-07 : 07:13:08
|
| One of the ways is to use Dynamic SQLDeclare @sql varchar(1000)set @sql='select * from tblProducts where '+@thefieldname+' like '''+@theparameter + '%''')Exec(@sql)MadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-10-07 : 07:42:18
|
| Another way is to pass in one parameter for each possible fieldname. You could potentially avoid dynamic sql that way.Be One with the OptimizerTG |
 |
|
|
|
|
|