Inserts one or more rows selected with a subquery : Insert « Insert Delete Update « SQL Server / T-SQL






Inserts one or more rows selected with a subquery


27>
28>
29> CREATE TABLE department(dept_no   CHAR(4) NOT NULL,
30>                         dept_name CHAR(25) NOT NULL,
31>                         location  CHAR(30) NULL)
32>
33> insert into department values ('d1', 'developer',   'Dallas')
34> insert into department values ('d2', 'tester',      'Seattle')
35> insert into department values ('d3', 'marketing',  'Dallas')
36>
37> select * from department
38> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d2      tester                    Seattle
d3      marketing                 Dallas

(3 rows affected)
1>
2> -- Inserts one or more rows selected with a subquery.
3>
4> CREATE TABLE dallas_dept
5>           (dept_no   CHAR(4)  NOT NULL,
6>            dept_name CHAR(20) NOT NULL)
7>
8> INSERT INTO dallas_dept (dept_no, dept_name)
9> SELECT dept_no, dept_name FROM department WHERE location = 'Dallas'
10>
11> select * from dallas_dept
12> Go

(2 rows affected)
dept_no dept_name
------- --------------------
d1      developer
d3      marketing

(2 rows affected)
1>
2> drop table department
3> drop table dallas_dept
4> GO
1>
2>
           
       








Related examples in the same category

1.The order of the columns in the insert statement
2.Leave column as NULL
3.Inserting Several Records in a Query Batch
4.Insert with default value