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)
 How to dynamically create var tables

Author  Topic 

liangtp@hotmail.com
Starting Member

27 Posts

Posted - 2008-09-22 : 04:55:51
Hi guys,

I wish to dynamically create a var table based on the result of an SQL query. Example below.

select Names From table
Result:
1.Clinton
2.Obama
3.McCain

How shall I write my code so that the result would have the similar following effect:

declare @varTable table (
Clinton nvarchar(10),
Obama nvarchar(10),
McCain nvarchar(10)
)

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-22 : 04:58:52
Ok. so your columns will be data from table. so what should be data in it? What you need is crosstabed result.
Go to Top of Page

liangtp@hotmail.com
Starting Member

27 Posts

Posted - 2008-09-22 : 05:01:45
you are right. it is a 2-Dimensional cross tab result. I need to capture some counter value data.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-22 : 05:06:19
quote:
Originally posted by liangtp@hotmail.com

you are right. it is a 2-Dimensional cross tab result. I need to capture some counter value data.


Use PIVOT operator available in SQL 2005. Have a look at books online for syntax and usage

http://msdn.microsoft.com/en-us/library/ms177410.aspx
Go to Top of Page

liangtp@hotmail.com
Starting Member

27 Posts

Posted - 2008-09-22 : 05:23:00
Allright. Thank you.
Go to Top of Page
   

- Advertisement -