How to create updatable views in TSQL

How to make an updated look in TSQL. Please provide simple examples on which I can substantiate my decision.

Recently, I was helping my friend with TSQL, and he asked me about updated views. I thought it would be a good idea to leave my solution here for the future as a quick search.

For a good reference check out msdn .

+5
source share
1 answer

I provide a very simple working example.

--Here is our base table
CREATE TABLE  [test_table]
(
   [a]   int primary key, 
   [b]   varchar(40),
   [c]   bigint,
   [d]   varchar(20),
)
GO

--Here is our view to the table
CREATE VIEW [test_view]     As SELECT b,a,d FROM test_table
GO

--As an example insert few entries
INSERT INTO test_table VALUES (0, 'abc', 2, '2011-10-13')
INSERT INTO test_table VALUES (1, 'abc', 2, '2011-10-13')
INSERT INTO test_table VALUES (3, 'abc', 2, '2011-10-13')

--Check if everything works fine
SELECT * FROM test_table 
SELECT * FROM test_view
GO

As the table and view populate the msdn ( search ) queries , I can do the following on the view:

UPDATE test_view SET b = 'xyz', d = '2011-10-14' where a = 0
INSERT test_view VALUES ('xyz', 2, '2011-10-14')
DELETE test_view where a = 3
GO

SELECT * FROM test_table 
SELECT * FROM test_view
GO

Important: Bulk insert is also allowed.

Good luck with your programming.

+5
source

All Articles