Insert data into single table from multiple tables

  • Hello Please Help me OUT,

    I am trying to insert data from different tables into single table

    Say I have a result table

    resultTB with following columns

    [resID] (int)identity , [PQID] int is not null, [col1] navarchar , [col2] nvarchar, [newColID]is not null

    I have my results in different tables, following is my list

    TB1

    [PQID] int is not null, [col1] nvarchar , [col2] nvarchar

    I have to insert specific data into result table from TB1 for specific PQID

    along with that I have newColID= '1'

    So how can I insert these two separate values into result table..

    Can anyone please suggest me any way to resolve this problem through sql programming

  • 1.If your new column is static then

    declare @newColID int, @PQID int

    Set @newColID = 1

    set @PQID =1234

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select PQID,col1,col2, @newColID

    from TB1 where TB1.[PQID] = @PQID

    2.If your new column resides in another table(TB2)

    If your new column is in a different table (related to TB1 through PQID)

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID

    from TB1 INNER JOIN TB2 ON TB1.PQID = TB2.PQID

    where TB1.[PQID] = @PQID

    Or

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID

    from TB1 , TB2

    where TB1.[PQID] = @PQID

    and TB1.PQID = TB2.PQID

    Hope this is what u are looking for.

  • Aiwa,

    I am confused by the source of your data to be inserted into resultTB. You say you have 2 different tables as source, but only list TB1.

    Also, is newColID a column in TB1?

    If TB1 is your only source table...

    INSERT into resultTB

    (PQID,col1,col2,newColID) --notice we do not explicity insert the identity column

    SELECT PQID,col1,col2,newColID

    FROM TB1 where PQID = ? and newColID = 1

    Todd Carrier
    MCITP - Database Administrator (SQL 2008)
    MCSE: Data Platform (SQL 2012)

  • i want to insert multiple data into multiple tables column

  • habib_nub (6/25/2008)


    i want to insert multiple data into multiple tables column

    You need to include a wee bit more info about what you want to do than that. Otherwise, the answer is simply "Make a SELECT to select the multiple data that you want and use it in one or more INSERTs to insert into multiple tables column."

    You might take a peek at the link in my signature and you might want to make you're own thread instead of piggy backing on someone else's post...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • After a new posting has been applied I am removing my request.

  • Please don't hijack old threads. If you have a new question, start a new thread.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • My apologies to the Business Intelligence Analyst with no tact.

    I didn't hijack the threat. I wasn't paying attention. My apologies to the Forum Moderators.

    I'll ask question elsewhere.

  • Hi,

    I need your help on a similar case

    I need to insert data from different tables into single table

    I've a table named costs

    and I've to fill it with datas from 3 tables: trademark,tariff and currency

    Iam little bit confused

    well the all thing Iam working on is while generating a template "word file"monitored by a stored procedure.

    this sp should include this insert code in order that the table will be filled automatically once a template is generated.

    I don't know if it sounds clear for you.

    I hope that someone knows how to deal with that

    and thank you anyway 🙂

  • zineb (6/23/2012)


    Hi,

    I need your help on a similar case

    ...

    Please open the new thread!

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 10 posts - 1 through 10 (of 10 total)

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