Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / SQL

How to Insert Data Using SQL Views Created Using Multiple Tables

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
5 Aug 2011CPOL1 min read 230.2K   6   5
This post will show you how to insert data using SQL views created using multiple tables.

A view can be defined as a virtual table or a stored query and the data accessible through a view is not stored in the database as a distinct object. Only the select statement is stored on the database instead.

However, views can be used and perform DML operations (Insert, Update & Delete) also.

Consider the following two tables:

SQL
CREATE TABLE STUDENT(
    STD_ID        INT,
    STD_FNAME    VARCHAR(20),
    STD_LNAME    VARCHAR(20)
)

CREATE TABLE STUDENT_PAYMENT(
    STD_ID        INT,
    PAY_AMT        MONEY,
    PAY_DATE    DATETIME
)

Now create the following views:

SQL
CREATE VIEW VW_STUDENT
AS
SELECT 
    STD_ID, 
    STD_FNAME, 
    STD_LNAME
FROM 
    STUDENT

CREATE VIEW VW_STUDENT_PAYMENT
AS
SELECT 
    STD_ID, 
    PAY_AMT, 
    PAY_DATE
FROM
    STUDENT_PAYMENT

You can insert data to the above tables using the views we have just created. And it is the same syntax that we use to insert data to tables.

SQL
INSERT INTO VW_STUDENT
SELECT 1,'Peter','Parker' UNION
SELECT 2,'James', 'Watson'
SQL
INSERT INTO VW_STUDENT_PAYMENT
SELECT 1,1000,'01/01/2011' UNION
SELECT 1,1100,'01/02/2011' UNION
SELECT 1,1200,'01/03/2011' UNION
SELECT 1,1250,'01/04/2011' UNION
SELECT 1,1375,'01/05/2011' UNION
SELECT 2,750,'01/03/2011' UNION
SELECT 2,850,'01/04/2011' UNION
SELECT 2,950,'01/05/2011' 

And if you query the tables, you can see that the records have inserted correctly.

Image 1

Now we will create the following view. This time, we will join two tables and create a somewhat complex query.

SQL
CREATE VIEW VW_LAST_PAYMENT_DETAILS AS
    WITH CTE_STD (STD_ID,MAX_PAYDATE) AS (
        SELECT SP.STD_ID, MAX(SP.PAY_DATE) AS MAX_PAYDATE
        FROM STUDENT_PAYMENT AS SP
        GROUP BY SP.STD_ID
    )
    SELECT S.STD_ID,S.STD_FNAME,S.STD_LNAME, P.PAY_AMT,P.PAY_DATE
    FROM STUDENT AS S
    JOIN STUDENT_PAYMENT AS P ON S.STD_ID = P.STD_ID 
    JOIN CTE_STD AS Q ON P.STD_ID = Q.STD_ID AND P.PAY_DATE = Q.MAX_PAYDATE
    GROUP BY S.STD_ID,S.STD_FNAME,S.STD_LNAME, P.PAY_AMT,P.PAY_DATE

Using the above created view, we can list the last payment details of each student.

Image 2

So if we are required to insert the last payment details using this view, how shall we do it? If you use the simple insert statements similar to the ones we used earlier, you have could ended up with the following error:

SQL
INSERT INTO VW_LAST_PAYMENT_DETAILS (STD_ID,PAY_AMT,PAY_DATE)
SELECT 1,4440,GETDATE()

img_scr_007

In order to insert (update & delete) data to views created using multiple tables, you need to use an ‘Instead of trigger’.

**Please note that ‘After Triggers’ cannot be created for views.

Let’s create an instead of trigger using the following syntax:

SQL
CREATE TRIGGER TRGI_VW_PAYMENT ON VW_LAST_PAYMENT_DETAILS
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO STUDENT_PAYMENT
    SELECT STD_ID,PAY_AMT,PAY_DATE
    FROM INSERTED
END

Now using the above insert syntax, you can insert data without getting any error. If you inspect the ‘STUDENT_PAYMENT’ table, you can see that the data has been inserted successfully.

img_scr_010

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My current role involves architecting and building a variety of data solutions, providing database maintenance and administration support, building the organization’s data practice, and training and mentoring peers.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science.


Specialities: SQL Server, T-SQL Development, SQL Server Administration, SSRS, SSIS, C#, ASP.Net, Crystal Reports

Comments and Discussions

 
QuestionThank you so much Pin
Member 1519977614-May-21 6:45
Member 1519977614-May-21 6:45 
Questioncan you please clear these doubts Pin
ajay ruhela31-Jul-19 0:16
ajay ruhela31-Jul-19 0:16 
QuestionCreate Trigger WIth Dynamic Field Pin
Satyabrat Mohanty6-Oct-15 15:46
Satyabrat Mohanty6-Oct-15 15:46 
GeneralGood Article Pin
Member 1045809714-Nov-14 8:05
Member 1045809714-Nov-14 8:05 
GeneralMy vote of 5 Pin
Omar Gameel Salem8-May-14 14:22
professionalOmar Gameel Salem8-May-14 14:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.