combining select statement in mssql

  • 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.

  • 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

  • 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

  • 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

  • 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
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

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

  • 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

  • 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
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

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

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply