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 |
Jarrodr
Starting Member
11 Posts |
Posted - 2008-07-09 : 03:47:04
|
Hi all,Making use of parameters, how do I pass parameter to choose the columns/fields of a table. I know parameters are used in WHERE clauses to filter data but can parameters be used to choose the Column of a table ie. in a SELECT clause(The sql statement below does not work, as I have found out)SELECT @ColumnFROM For_SaleThanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-09 : 03:57:52
|
You need to use dynamic sql or write a series of CASE conditions to achieve this.eitherSET @Sql='SELECT '+ @Column + ' FROM For_Sale'EXEC(@Sql)orSELECT CASE @Column WHEN 'Col1' THEN Col1 WHEN 'Col2' THEN Col2 ... ENDFROM For_Sale |
 |
|
|
|
|