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 2000 Forums
 SQL Server Development (2000)
 Query from (variable number of tables) help !!!

Author  Topic 

transcom_bcn
Starting Member

16 Posts

Posted - 2006-07-28 : 07:16:03
- Hello, I'd need some help with this situation:
First I have a simple select that returns to me a variable number of resutls (which each result is a table name that I need later). Eg.:

select distinct ct_table from CONF_campaigns

- Then, with each one of those variable results, I have another query, who uses that result as the table name used in the FROM clause. Eg.:

SELECT ... FROM (table name obtained with the previous query) WHERE ...

- And I have to iterate the previous step 'n' times, where 'n' is the number of result that the first query returns.


- What solution could you recommend to me ???



- Thanks in advance!

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-07-28 : 08:05:58
1. u need to use dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

2. if ur real situation can be explained, v may find a better way, since dynamic SQL is not that good.

Srinika
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-07-28 : 08:18:10
what is the nature of the data in all the different tables? Should those tables all really be just one table (with an additional column perhaps)?

Be One with the Optimizer
TG
Go to Top of Page

transcom_bcn
Starting Member

16 Posts

Posted - 2006-07-28 : 09:00:04
quote:
Originally posted by Srinika

1. u need to use dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

2. if ur real situation can be explained, v may find a better way, since dynamic SQL is not that good.

Srinika





Yes, after reading that article, I think that I'd have to use Dynamic SQL in order to solve that problem. I agree that it might not be the best way, but after I could solve the problem, then I'll think about optimization.

Thank you!
Go to Top of Page

transcom_bcn
Starting Member

16 Posts

Posted - 2006-07-28 : 09:02:41
quote:
Originally posted by TG

what is the nature of the data in all the different tables? Should those tables all really be just one table (with an additional column perhaps)?

Be One with the Optimizer
TG



That's not appropriate in my case, because every table has more than 40 fields, and all those tables represent a different business data model, because each 'campaign' is as the clients wants it to be, and from one client to another, the requirements vary a lot.

Thanks!
Go to Top of Page
   

- Advertisement -