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 multiple tables based on query result

Author  Topic 

welkin
Starting Member

3 Posts

Posted - 2008-07-16 : 21:58:27
Hi all,

I have a table where I can select a list of IDs from, and now I need to create a table for each ID (with names like table_[ID]). Ideally I would like the SQL equivalent of

for each ID in (SELECT ID from master_table)
CREATE TABLE table_ID (...)

though I've searched online and haven't had much luck. Can anyone point me in the right direction? I'm using MS SQL 2005. Thanks a million!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-16 : 22:03:11
sounds like it is not normalize.

Why do you want to create table in such manner ?

Why not just create a table and use ID as part of the primary key ?




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

welkin
Starting Member

3 Posts

Posted - 2008-07-16 : 22:07:23
I am trying to divide a large table into smaller tables for performance gain. The table currently contains information for multiple groups (each group has 10s of thousands of rows, each row is identified with a groupID), and I would like to create a table for each group.

Thanks,
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-16 : 22:09:52
you have to use cursor / loop with dynamic sql to do it


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-07-16 : 22:10:00
Pardon me, but I don't think you really want to do that. You would ideally want to create rows in a table for each record, not a table for each record (imagine what happens when you have thousands of tables all containing the same fields). Having a table for each record makes comparing, summing data a big pain in the ass.
Go to Top of Page

welkin
Starting Member

3 Posts

Posted - 2008-07-16 : 22:19:04
khtan: thanks, i'll look into that. :)

Eugene: it's slightly different. technically i want to do SELECT DISTINCT GROUP_ID FROM master_table; each group_ID corresponds to many, many rows.
Go to Top of Page
   

- Advertisement -