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
 Transact-SQL (2005)
 select particular column

Author  Topic 

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2008-01-10 : 01:25:44
I have a table called Mysampple
I want to create a stored procedure to display a particular column of the table.
Here, I want to give column name as the parameter of the stored procedure.


Is it possible!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-10 : 01:34:02
You might need dynamic sql for this

CREATE PROC GetColumn
columnname varchar(100)
AS

DECLARE @Sql varchar(8000)

SELECT @Sql='SELECT ' + @columnname + ' FROM Table'
EXEC (@Sql)
GO

Can i ask why you want to do it this way?
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2008-01-10 : 02:08:05
Thank you visakh
quote:
Originally posted by visakh16

You might need dynamic sql for this

CREATE PROC GetColumn
columnname varchar(100)
AS

DECLARE @Sql varchar(8000)

SELECT @Sql='SELECT ' + @columnname + ' FROM Table'
EXEC (@Sql)
GO

Can i ask why you want to do it this way?

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-10 : 03:27:02
You should avoid passing object names as parameter
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
   

- Advertisement -