Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
Business Intelligence
»
combining select statement in mssql
combining select statement in mssql
Rate Topic
Display Mode
Topic Options
Author
Message
bvinay57
bvinay57
Posted Friday, April 17, 2009 8:47 AM
SSC 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
ChrisM@Work
ChrisM@Work
Posted Friday, April 17, 2009 8:55 AM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 5,618,
Visits: 10,990
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
bvinay57
bvinay57
Posted Friday, April 17, 2009 9:07 AM
SSC 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
ChrisM@Work
ChrisM@Work
Posted Friday, April 17, 2009 9:38 AM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 5,618,
Visits: 10,990
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
Jeffrey Williams 3188
Jeffrey Williams 3188
Posted Friday, April 17, 2009 11:48 AM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:21 AM
Points: 4,317,
Visits: 9,216
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
bvinay57
bvinay57
Posted Saturday, April 18, 2009 4:16 AM
SSC 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
Jeffrey Williams 3188
Jeffrey Williams 3188
Posted Saturday, April 18, 2009 9:22 AM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:21 AM
Points: 4,317,
Visits: 9,216
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.