Pull data from another table

  • I have 2 tables; tbl1 and tbl2.

    Information:

    tbl1 has the following columns;

    PID, PNUM, PDISC

    tbl2 has the following columns;

    PID, PNUMR, PDISCR. COlumn 'PID' has NULL values all the way across.

    I need help with TSQL that will populate Column 'PID'in tbl2 from Column 'PID' in tbl1. Only thing that

    matches between two tables are columns 'PNUM' and 'PNUMR'. That is column PNUM in tbl1 = column PNUMR in tbl2.

    I am new to TSQL. Pleaes help! Thanks!!

  • Hi,

    You’ll need an update statement, something like:

    UPDATE tbl2

    SET PID = tbl1.PID

    FROM tbl1

    WHERE PNUMR = tbl1.PNUM

    Allister

  • Thank you SSC Rookie...that worked like a charm!!

  • Could yu guide me on how I could do the above automated, every time, a data entry goes into oen tabe, how can I get it to move into the other one?

  • Create an AFTER INSERT trigger and add that query into it.

    ======================================
    Blog: www.irohitable.com

  • Grasshopper,

    Could you please elaborate a little more, since I am relatively new to this.

    Thanks in advance

  • A trigger is similar to stored procedures with a little difference. Trigger is attached to a table and the code gets executed automatically when row is inserted, updated or deleted from the table.

    There are very good tutorials available. Below are few links:

    An Introduction to Triggers

    How To: SQL Server Trigger 101[/url]

    ======================================
    Blog: www.irohitable.com

  • Thankyou very much I shall go through them and get back if I get stuk again. Thanks a lot

    So, if I want to move all messages coming from Service ID = 200 from Table_MessagesOut to Table_MessagesIn Service ID 202, the sql command would be the same as above right?

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

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