1

I give an example to show my problem. I create 2 table as the following:

CREATE TABLE a
(
    id INT
)

CREATE TABLE b
(
    name NVARCHAR(10),
    id INT
)

Then insert data into these tables

INSERT INTO a VALUES(1),(2)
INSERT INTO b VALUES('Kan',1),('Michael',2)

Next,I create indexed view that join these tables via id

CREATE VIEW a_b
WITH SCHEMABINDING
AS
(
SELECT a.id,b.name FROM a INNER JOIN b ON a.id=b.id
)

CREATE UNIQUE CLUSTERED INDEX ix_a_b
ON a_b(id)

INSERT INTO a_b VALUES (3,'Joe') will be wrong

As I know about view:

  • views do not store data,just saved queries

  • but indexed view that stored data physically like table in the database.So why I don't insert,delete from a_b?

And what I know about VIEW is right or wrong?Help me improve?

1 Answer 1

0

From Doc Online:

The view referenced by table_or_view_name must be updatable and reference exactly one base table in the FROM clause of the view. 
For example, an INSERT into a multi-table view must use a column_list that references only columns from one base table. 

https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-ver15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.