SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


combining select statement in mssql


combining select statement in mssql

Author
Message
bvinay57
bvinay57
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
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.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16230 Visits: 19549
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
bvinay57
bvinay57
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16230 Visits: 19549
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
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7691 Visits: 9966
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 opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

bvinay57
bvinay57
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
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
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7691 Visits: 9966
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 opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search