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.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 how to seelct column name dynamically

Author  Topic 

spshindagi
Starting Member

11 Posts

Posted - 2008-01-14 : 01:41:02
am passing column name as parameter, so i wanted to select column name dynamically.

eg: select columname1,columname2 where @param = columnname1,columnname2.
when @param =columnname1,columnname2. select should be of respective column. if @param = columname2,columnname8, then select columname2,columnname8 from table1 where coulmnname2='1' and columnname8='2'.

love all

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-14 : 03:07:21
Why do you want to do it dynamically?
Make sure you read this fully
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

spshindagi
Starting Member

11 Posts

Posted - 2008-01-31 : 04:58:34
finally i got answer by sending query parameter as array to function n then select '"+xay+",ac, from table where '"+gl"'='1'

love all
Go to Top of Page

aniketh
Starting Member

2 Posts

Posted - 2011-09-12 : 07:19:00
Hey Spshindagi,

Can you send the example of the above scenario. How you achieved.
Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-10-08 : 09:52:43
Hi,

You can create a query by concatenation string i.e. names of columns then use sp_executesql.

Refer for more: http://msdn.microsoft.com/en-us/library/ms175170.aspx

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-10-08 : 09:54:52
Hi,

You can create a query by concatenation string i.e. names of columns then use sp_executesql.

Refer for more: http://msdn.microsoft.com/en-us/library/ms175170.aspx

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page
   

- Advertisement -