How to insert into table from two tables of data?

  • Thanks GSquared..

    Is it possible to give a script without UNION ALL keyword?

    Regards

    Sharma

  • Yes venkat .. Your way is correct.. I need a script without using UNION ALL

  • insert into Employee (pg)

    select pg

    from Qualification

    insert into Employee(street, city, state)

    select street, city, state

    from Address

    Does that do what you need?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks Squard..

    That way i need..

    But it insert as three rows.. I need combine this query & insert as single record in employee table..

    Regards

  • Hi Tables are restructrued by adding id in all the tables.

    ID in Address table is pk & ID in qualification table is FK

    Initailly Employee table is empty.

    Address tbl contains below values

    100

    car st,

    chennai,

    tamilnadu

    Qualification tbls contains below values

    100

    bca,

    msc.

    Finally Employee table will contain below records

    100,

    alex,

    msc,

    car st,

    chennai,

    tamilnadu

    Regards

  • sharma (5/8/2008)


    Hi Tables are restructrued by adding id in all the tables.

    ID in Address table is pk & ID in qualification table is FK

    Initailly Employee table is empty.

    Address tbl contains below values

    100

    car st,

    chennai,

    tamilnadu

    Qualification tbls contains below values

    100

    bca,

    msc.

    Finally Employee table will contain below records

    100,

    alex,

    msc,

    car st,

    chennai,

    tamilnadu

    Regards

    now update ADDRESS RECODS AND QUALIFICATION RECORDS TO EMPLOYEE TABLE

    UPDATE EMPLOYEE SET

    ADDRESS1 = ADD.ADDRESS1

    ,ADDRESS2 = ADD.ADDRESS2

    ,PG = PG.PG_NAME

    FROM

    ADDRESS ADD

    JOIN

    PG ON ADD.ID = PG.ID

    JOIN

    EMPLOYEE ON ADD.ID = EMPLOYEE.ID

  • Thanks shamshudheen ..

    It works.

    Is anyother way to do this using CTE or other way without using UPDATE, just do select & insert?

    Thanks for ur query..

    Regards

    Sharma

  • once you have records in EMPLOYEE table thru INSERT then there is only way is update.DONT CONFUSE YOURSELF.CTE IS FOR SOME OTHER PURPOSE.which will not a soulution for your problem

  • A better way of doing it without using CTE or Update statement is as below.

    insert into dbo.Employee(id,name,pg,street,city,state)

    select a.id,'Alex',a.pg,b.street,b.city,b.state

    from

    dbo.qualification a

    join

    dbo.address b

    on

    a.id = b.id

    This should be what you need..

    Hope this helps...

    The_SQL_DBA
    MCTS

    "Quality is never an accident; it is always the result of high intention, sincere effort, intelligent direction and skillful execution; it represents the wise choice of many alternatives."

Viewing 9 posts - 16 through 23 (of 23 total)

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