2

I am creating a stored procedure to insert data into a table from another table. However, all of the data that I want inserted into the new record is not in the other table and at the present time it is not in any table. I am calling this "static" data. I have two tables: one called Resident and one called Lease. If I have a record in the Lease table, I also want an associated record in the Resident table for that Lease.

These are the fields that I need to populate in the Resident table:

    Comp_Name = SMITHJ1234
    Comp_NameLong = SMITH; JOHN
    Comp_Type = Resident
    Comp_Status = Active
    Comp_Source = Import
    Comp_CreatedBy = 1
    Comp_UpdatedBy = 1

These are the fields that will come from the Lease table:

  Lease_primaryresident
  Lease_name

So this is what I have right now for stored procedure:

 INSERT INTO Resident (Comp_Name,Comp_NameLong)
    SELECT Lease_primaryresident,
       Lease_name
    FROM Lease
    WHERE NOT EXISTS (SELECT * FROM Resident
    WHERE Lease.Lease_primaryresident = Resident.comp_name);

How do I get the other items to populate in the Resident record (i.e. comp_type, comp_status, etc.) during this insert?

Any assistance would be greatly appreciated.

Thanks!

1 Answer 1

1

You can add literal values to the select, obviously:

INSERT INTO Resident (Comp_Name,Comp_NameLong, Comp_Type, Comp_Status)
SELECT 
   Lease_primaryresident,
   Lease_name,
   'Resident',
   'Active'
FROM Lease
WHERE NOT EXISTS (SELECT * FROM Resident
WHERE Lease.Lease_primaryresident = Resident.comp_name);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.