Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you insert into a view in SQL Server? [closed]

Tags:

sql

view

What is the benefit of inserting in a view vs. in tables directly? e.g. Scenario #1

What if the view has only a few columns from a table, are you still able to insert successfully? e.g. Scenario #2

Scenario #1:

CREATE TABLE dbo.tbl1 
(
    ID INT NOT NULL,
    NAME VARCHAR(32) NOT NULL  
);

CREATE VIEW dbo.vw_x 
AS
    SELECT ID, Name
    FROM dbo.tbl1
    WHERE ID = 2

INSERT INTO dbo.vw_x
SELECT 2, 'Name';

Scenario #2:

CREATE TABLE dbo.tbl1 
(
    ID INT NOT NULL,
    NAME VARCHAR(32) NOT NULL  
);

CREATE TABLE dbo.tbl2 
(
    ID INT NOT NULL,
    VALUE VARCHAR(32) NOT NULL  
);

CREATE VIEW dbo.vw_x 
AS
    SELECT t1.ID, t2.Value
    FROM dbo.tbl1 t1
    INNER JOIN dbo.tbl2 t2
        ON t1.ID = t2.ID
    WHERE t1.ID = 2

INSERT INTO dbo.vw_x
SELECT 2, 'Name';
like image 816
Tigerjz32 Avatar asked Dec 15 '15 00:12

Tigerjz32


People also ask

Why can't you insert into a view SQL?

You can insert rows into a view only if the view is modifiable and contains no derived columns. The reason for the second restriction is that an inserted row must provide values for all columns, but the database server cannot tell how to distribute an inserted value through an expression.

Can we insert data into a view in SQL Server?

You can insert data through a single-table view if you have the Insert privilege on the view. To do this, the defining SELECT statement can select from only one table, and it cannot contain any of the following components: DISTINCT keyword.

Can I insert into view?

So, Yes, we can insert data into view in SQL Server. But, remember that the actual data will be inserted into the underlying table, and a view will just return the data of that underlying table. Because a view in SQL Server is not a physical table and does not hold any data.

Can we insert and delete rows into view?

View is similar to a normal table and also have rows and columns. View may be combination of some rows and columns of two or more than two tables. You can use SQL function and WHERE clause with View in SQL statement. You can insert and delete rows into base table using view.


1 Answers

Typically users might insert into a view if they have not been granted permissions to the underlying tables.

Regarding "how inserting happens", this is from the MSDN article for the CREATE VIEW statement:

Updatable Views

You can modify the data of an underlying base table through a view, as long as the following conditions are true:

  • Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

  • The columns being modified in the view must directly reference the underlying data in the table columns. The columns cannot be derived in any other way, such as through the following:

    An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR, and VARP.

    A computation. The column cannot be computed from an expression that uses other columns. Columns that are formed by using the set operators UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also not updatable.

  • The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.

  • TOP is not used anywhere in the select_statement of the view together with the WITH CHECK OPTION clause.

The previous restrictions apply to any subqueries in the FROM clause of the view, just as they apply to the view itself. Generally, the Database Engine must be able to unambiguously trace modifications from the view definition to one base table.

like image 182
Nathan Griffiths Avatar answered Oct 09 '22 00:10

Nathan Griffiths