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
 General SQL Server Forums
 New to SQL Server Programming
 create table

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-01-23 : 07:25:09
Hi,

i want to create table [products1] where column names of table are read from another table as values.

example:

create table products1
(select names from tbl_products)

I know it is a bit strange, but i would need such function or procedure.

thank you

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-23 : 07:27:05
[code]Select *
into products1
from tbl_products where 1=0[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-23 : 07:29:18
Select top 0 *
into products1
from tbl_products



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-23 : 07:34:50
If you need data remove where condition or top 0
or Script the table;rename the name;run it;copy the data to it

Madhivanan

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

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-01-23 : 07:37:21
hi,

i'm sorry for being vague.

tbl_products consist of
id
name
description

and i want values from tbl_products.name to be named as columns in new table products1

hope this is better explained :)
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-23 : 07:38:14
Also, above two methods won't copy constraints or indexes of the original table. So scripting the original table and changing table name is the best option.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-23 : 07:39:44
quote:
Originally posted by slimt_slimt

hi,

i'm sorry for being vague.

tbl_products consist of
id
name
description

and i want values from tbl_products.name to be named as columns in new table products1

hope this is better explained :)


Did you follow my suggestion?

Madhivanan

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

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-23 : 07:43:51
What what will be the data type for each of the column? This is possible with only dynamic sql.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-01-23 : 07:47:51
data type for all columns is "int".
i was thinking of dynamic sql too.
Go to Top of Page
   

- Advertisement -