Inserting Rows from Another Table : Insert into select « Insert Delete Update « SQL Server / T-SQL Tutorial






4>
5> CREATE TABLE Customers (
6>      CustomerID nchar (5) NOT NULL ,
7>      CompanyName nvarchar (40) NOT NULL ,
8>      ContactName nvarchar (30) NULL ,
9>      ContactTitle nvarchar (30) NULL ,
10>     Address nvarchar (60) NULL ,
11>     City nvarchar (15) NULL ,
12>     Region nvarchar (15) NULL ,
13>     PostalCode nvarchar (10) NULL ,
14>     Country nvarchar (15) NULL ,
15>     Phone nvarchar (24) NULL ,
16>     Fax nvarchar (24) NULL
17> )
18> GO
1>
2> CREATE TABLE employee(
3>    id          INTEGER NOT NULL PRIMARY KEY,
4>    first_name  VARCHAR(10),
5>    last_name   VARCHAR(10),
6>    salary      DECIMAL(10,2),
7>    start_Date  DATETIME,
8>    region      VARCHAR(10),
9>    city        VARCHAR(20),
10>    managerid   INTEGER
11> );
12> GO
1> INSERT INTO employee VALUES (1, 'Jason' ,  'Martin', 5890,'2005-03-22','North','Vancouver',3);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (2, 'Alison',  'Mathews',4789,'2003-07-21','South','Utown',4);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (3, 'James' ,  'Smith',  6678,'2001-12-01','North','Paris',5);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (4, 'Celia' ,  'Rice',   5567,'2006-03-03','South','London',6);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (5, 'Robert',  'Black',  4467,'2004-07-02','East','Newton',7);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (6, 'Linda' ,  'Green' , 6456,'2002-05-19','East','Calgary',8);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (7, 'David' ,  'Larry',  5345,'2008-03-18','West','New York',9);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (8, 'James' ,  'Cat',    4234,'2007-07-17','West','Regina',9);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (9, 'Joan'  ,  'Act',    6123,'2001-04-16','North','Toronto',10);
2> GO

(1 rows affected)
1>
2> select * from employee;
3> GO
id          first_name last_name  salary       start_Date              region     city                 managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
          1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
          2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
          3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
          4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
          5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
          6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
          7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
          8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
          9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected)
1>
2>
3>
4>
5>
6> INSERT INTO Customers
7> (CompanyName)
8> SELECT First_Name
9> FROM Employee
10> GO
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 6
Cannot insert the value NULL into column 'CustomerID', table 'master.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.
1>
2> select * from Customers;
3> GO
CustomerID CompanyName                              ContactName                    ContactTitle                   Address                                                      City            Region
       PostalCode Country         Phone                    Fax
---------- ---------------------------------------- ------------------------------ ------------------------------ ------------------------------------------------------------ --------------- ---------
------ ---------- --------------- ------------------------ ------------------------

(0 rows affected)
1>
2> drop table Customers;
3> drop table employee;
4> GO
1>








2.2.Insert into select
2.2.1.The syntax of the INSERT statement for inserting rows selected from another table
2.2.2.INSERT. . . SELECT constants
2.2.3.INSERT statement with a column list and select clause
2.2.4.Use insert...into...select in a stored procedure
2.2.5.Inserting Rows from Another Table