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)
 Dynamic Columns

Author  Topic 

swathigardas
Posting Yak Master

149 Posts

Posted - 2009-06-12 : 07:31:01
Hi ,

Is there way to add columns dynamically to my result set based on the input sent by the user. If the user sends the value as 2 , then first 2 columns of the result set should be sent as ouput.

Any help would be thankful

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-12 : 07:47:14
Yes.

1. Store the result with ALL possible columns in a temp table.
2. Build a dynamic SELECT statement to query the above temp table.
3. Only include as many columns as user supplies.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-12 : 08:34:28
try this Procedure

-- EXEC usp_DyanmicColumnBuilding 'Tbl_Pms_Fields',5
ALTER PROC usp_DyanmicColumnBuilding
(
@TableName VARCHAR(128),
@NoOfColumns INT
)
AS
SET NOCOUNT ON
BEGIN

CREATE TABLE #temp ( ColName VARCHAR(128), ordinalpos int )

INSERT INTO #temp
select column_name,ordinal_position from information_schema.columns
where table_name = @TableName
order by ordinal_position

declare @col varchar(8000)
select @col = stuff((select ','+colname from #temp where ordinalpos <= @NoOfColumns for xml path('')),1,1,'')

Declare @str varchar(max)
select @str = 'SELECT '+ @col +' FROM ' +@TableName

print @str
exec (@str)

END
SET NOCOUNT OFF


This is what i tried with above Peso Solution...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-12 : 08:59:16
quote:
Originally posted by swathigardas

Hi ,

Is there way to add columns dynamically to my result set based on the input sent by the user. If the user sends the value as 2 , then first 2 columns of the result set should be sent as ouput.

Any help would be thankful


This does what you want
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53384

Madhivanan

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

swathigardas
Posting Yak Master

149 Posts

Posted - 2009-06-12 : 09:49:05
Yes thanks so much for both the solution. But in my scenario the result set comes from a stored procedure.
SO i need to Store the result set in a #Temp Table and then apply the query given by you on the Temp Table.

Correct me if i am wrong.
Thanks a lot for the Solution.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-14 : 02:27:37
quote:
Originally posted by swathigardas

Yes thanks so much for both the solution. But in my scenario the result set comes from a stored procedure.
SO i need to Store the result set in a #Temp Table and then apply the query given by you on the Temp Table.

Correct me if i am wrong.
Thanks a lot for the Solution.



or you could do this

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx
Go to Top of Page
   

- Advertisement -