Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert into a table using a view

Tags:

sql

I have some data in a view which I want to insert into a table (same table schema) what's the easiest, cleanest way to do it.

like image 763
Jack Mills Avatar asked Jun 18 '10 09:06

Jack Mills


People also ask

Can we insert into table from view?

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. GROUP BY clause.

How do I insert data into a SQL view table?

To insert data through view in multiple tables, we need to use the INSTEAD OF TRIGGER in SQL Server. An INSTEAD OF TRIGGER in SQL Server allows executing other statements specified in the trigger instead of an INSERT, DELETE, or UPDATE statement to a table or view.


3 Answers

if you want to insert the entire column then you can do like

   insert into table_name
   select * from view_name
like image 147
Ashim Sinha Avatar answered Oct 16 '22 03:10

Ashim Sinha


Insert Into dbo.MyTable (Col1, Col2,...)
Select Col1, Col2, ...
From dbo.MyView
like image 23
codingbadger Avatar answered Oct 16 '22 01:10

codingbadger


insert into mytable(c1, c2, c3, ...)
select c1, c2, c3, ... from myview
like image 3
Willis Blackburn Avatar answered Oct 16 '22 03:10

Willis Blackburn