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';
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With