Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

combining select statement in mssql Expand / Collapse
Author
Message
Posted Friday, April 17, 2009 8:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 17, 2009 1:27 AM
Points: 34, Visits: 99
hi i am a newbie to mssql. In my db i have three tables. I want to retrieve data
combining these 3 tables using select statement using while loop is there any suggestions these 3 tables contain similar fields.
Post #699488
Posted Friday, April 17, 2009 8:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 6,750, Visits: 13,896
Hello

Please can you provide more information about your tables, the columns, and the expected output?

Cheers

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #699497
Posted Friday, April 17, 2009 9:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 17, 2009 1:27 AM
Points: 34, Visits: 99
Hi
thanx for ur immediate response in my db i have Table1,Table2,Table3 tables i want to retrive data using select statement first i want to retrive data from Table1 after finishing Table1 now i want to retrive data for Table2 like this Table3 so i think it is better to go with while loop Table1,Table2,Table3 contains similar fields.So no problem arises
Post #699505
Posted Friday, April 17, 2009 9:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 6,750, Visits: 13,896
bvinay57 (4/17/2009)
Hi
thanx for ur immediate response in my db i have Table1,Table2,Table3 tables i want to retrive data using select statement first i want to retrive data from Table1 after finishing Table1 now i want to retrive data for Table2 like this Table3 so i think it is better to go with while loop Table1,Table2,Table3 contains similar fields.So no problem arises


There are far too many different ways of interpreting your requirement, you must provide much more information than this. What data do you want to retrieve from table1? Having retrieved it, do you want to add a subset of table2 to it? As rows or columns? What about table3?
How are the three tables related? What is the business context?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #699530
Posted Friday, April 17, 2009 11:48 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:31 AM
Points: 4,358, Visits: 9,537
Well - it really depends on what your table structures are, how they are related and what is the purpose of your queries.

If you want the results in a single resultset - then you are going to want to join or union. If you want three separate resultsets - then you will have three separate select statements.

We have no way of know what it is you are trying to accomplish without further information. Please review the article in my signature on how to post. It will help you post your question so we can answer it better.


Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #699666
Posted Saturday, April 18, 2009 4:16 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 17, 2009 1:27 AM
Points: 34, Visits: 99
Hi i want to place the data like this
for(int i=3;i<=5;i++)
{
select
adid,state,country
from
Tablei
}
Hi i want to place the data like this
Table1,Table2,Table3 tables i want to retrive data using select statement first i want to retrive data from Table1 after finishing Table1 now i want to retrive data for Table2 after combining Table1,Table2 i want to combine Table3 also so i think it is better to go with for loop Table1,Table2,Table3 contains similar fields.So no problem arises but it is not working any suggestions
Post #699970
Posted Saturday, April 18, 2009 9:22 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:31 AM
Points: 4,358, Visits: 9,537
Yes - I have a suggestion. Read the article I link to in my signature.

I can guarantee that you do NOT want to loop through the tables. Whatever you are trying to accomplish will most likely be accomplished using joins, as in:

SELECT {columns}
FROM table1 t1
INNER JOIN table2 t2 ON t2.key = t1.key
INNER JOIN table3 t3 ON t3.key = t2.key -- or would this be related back to t1?
WHERE {criteria to filter the results}

Looping in SQL is almost always the wrong way to do get the job done.

Again, review the article in my signature and see if you can post an example we can work with.


Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #700031
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse