Friday, 5 October 2012

SQL Query: Insert Into statement - Select data from different table

Scenario:
I want to insert a record into table A that I will select from different table B if it is not already exist in table A.

Solution:

INSERT INTO tGymReg ( AdmissionNumber, Surname, Forename, Called, Induction )
     SELECT dbo_vwStudCYAll.AdmissionNumber, dbo_vwStudCYAll.Surname,
     dbo_vwStudCYAll.Forename, dbo_vwStudCYAll.Called, dbo_vwStudCYAll.Induction
     FROM dbo_vwStudCYAll
     LEFT JOIN tGymReg ON dbo_vwStudCYAll.AdmissionNumber = tGymReg.AdmissionNumber
WHERE (((tGymReg.AdmissionNumber) Is Null));






CREATE procedure [dbo].[ImportNewStudentToSTUDStudentCurrent]
as
insert into STUDStudentCurrent (STUD_AdmissionNumber,STUD_Surname,STUD_Forename,STUD_Called)
      select test.[Column 0], test.[Column 1], test.[Column 2], test.[Column 3]
      from test
      left join STUDStudentCurrent
            on test.[Column 0]=STUDStudentCurrent.STUD_AdmissionNumber
      where (STUDStudentCurrent.STUD_AdmissionNumber is null)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home