Inserting from one table to another one

  • Hi all,

    Is there a way to write a sql script to do the following tasks?

    If the Subj_Num and Scan_date in table IMG_WHOLE_BRAIN matches with Subj_Num and Scan_date in the table mrisession, then insert the value from Magnet_strength_in_gauss in mrisession table into Magnet_strength_in_gauss in IMG_WHOLE_BRAIN

    Tables information:

    Table Name: IMG_WHOLE_BRAIN

    Fields name:

    Subj_Num data type: int

    Scan_date data type: datetime

    Magnet_strength_in_gauss data type: decimal

    #########################################

    Table name: mrisession

    Fields name:

    Magnet_strength_in_gauss data type: decimal

    Exam_DATE_stamp data type: datetime

    subj_num data type: int

    ////////////

    We have already around 1000 records in IMG_WHOLE_BRAIN table with no value in the dbo.IMG_WHOLE_BRAIN.Magnet_strength_in_gauss field.

    I tried the following script, but it did not work:

    SELECT dbo.mrisession.Magnet_strength_in_gauss

    from dbo.mrisession , dbo.IMG_WHOLE_BRAIN

    where dbo.IMG_WHOLE_BRAIN.SUBJ_NUM = dbo.mrisession.subj_num

    AND dbo.IMG_WHOLE_BRAIN.scan_date = dbo.mrisession.Exam_DATE_stamp

    INSERT INTO dbo.IMG_WHOLE_BRAIN.Magnet_strength_in_gauss

    //////////////////////////////////////////////////

    Thanks for any help.

    Abrahim

  • This was removed by the editor as SPAM

  • The following update query should do what you want:

    update dbo.IMG_WHOLE_BRAIN

       set Scan_Date = MRI.Exam_DATE_stamp

       from dbo.IMG_WHOLE_BRAIN as IWB

       join dbo.mrisession      as MRI

         on IWB.Subj_Num = MRI.Subj_Num   and

            IWB.Magnet_strength_in_gauss = MRI.Magnet_strength_in_gauss

     

         Rick Phillips

     

  • Thanks Rick ,

    I'll give it try.

    Regards,

    Abrahim

Viewing 4 posts - 1 through 3 (of 3 total)

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