0% found this document useful (0 votes)
168 views19 pages

The Basic Syntax of The SELECT Statement

The INSERT statement is used to insert rows into a table. The basic syntax includes specifying the table name, column list (optional), and VALUES with expressions for each column. Columns without provided values will use the DEFAULT value or be NULL if nullable. Inserting into a subset of columns requires explicitly specifying the column names.

Uploaded by

lifeofname
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views19 pages

The Basic Syntax of The SELECT Statement

The INSERT statement is used to insert rows into a table. The basic syntax includes specifying the table name, column list (optional), and VALUES with expressions for each column. Columns without provided values will use the DEFAULT value or be NULL if nullable. Inserting into a subset of columns requires explicitly specifying the column names.

Uploaded by

lifeofname
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

The basic syntax of the SELECT statement

SELECT select_list
FROM table_source
[WHERE search_condition]
[ORDER BY order_by_list]
The four clauses of the SELECT statement
Clause
Description
SELECT
Describes the columns that will be included in the result set.
FROM
Names the table from which the query will retrieve the data.
WHERE
Specifies the conditions that must be met for a row to be include
d in the result set. This clause is optional.
ORDER BY Specifies how the rows in the result set will be sorted. This cla
use is optional.

20>
21> create table Billings (
22>
BankerID
INTEGER,
23>
BillingNumber
INTEGER,
24>
BillingDate
datetime,
25>
BillingTotal
INTEGER,
26>
TermsID
INTEGER,
27>
BillingDueDate
datetime ,
28>
PaymentTotal
INTEGER,
29>
CreditTotal
INTEGER
30>
31> );
32> GO
1>
2> INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-0422',123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-0222',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-0412',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-0417',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-04-

18',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-0419',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-0420',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-0421',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-0422',123,321);
2> GO
(1 rows affected)
1>
2> SELECT * FROM Billings
3> GO
BankerID
BillingNumber BillingDate
BillingTotal TermsID
BillingDueDate
PaymentTotal CreditTotal
----------- ------------- ----------------------- ------------ --------------------------------- ------------ ----------1
1 2005-0122 00:00:00.000
165
1 2005-0422 00:00:00.000
123
321
2
2 2001-0221 00:00:00.000
165
1 2002-0222 00:00:00.000
123
321
3
3 2003-0502 00:00:00.000
165
1 2005-0412 00:00:00.000
123
321
4
4 1999-0312 00:00:00.000
165
1 2005-0418 00:00:00.000
123
321
5
5 2000-0423 00:00:00.000
165
1 2005-0417 00:00:00.000
123
321
6
6 2001-0614 00:00:00.000
165
1 2005-0418 00:00:00.000
123
321
7
7 2002-0715 00:00:00.000
165
1 2005-0419 00:00:00.000
123
321
8
8 2003-0816 00:00:00.000
165
1 2005-0420 00:00:00.000
123
321
9
9 2004-0917 00:00:00.000
165
1 2005-0421 00:00:00.000
123
321
0
0 2005-1018 00:00:00.000
165
1 2005-0422 00:00:00.000
123
321

(10 rows affected)


1>
2> drop table Billings;
3> GO
1>

The operators you can use in a search condition


Operator
Description
!=
not equal
!>
not greater than
!<
not less than
<
less than
<=
less than or equal
<>
not being equal
=
equality between two expressions
>
greater than
>=
greater than or equal to
ALL
In subquery, if all retrieved values satisfy the search condi
tion, the rows will be retrieved.
ANY
In subquery, if any retrieved values satisfy the search condi
tion, the rows will be retrieved.
BETWEEN
Designates an inclusive range of values. Used with the AND cl
ause between the beginning and ending values.
CONTAINS
Does a search for words and phrases.
ESCAPE
Escape from the pattern.
EXISTS
When used with a subquery, EXISTS tests for the existence of
rows in the subquery.
FREETEXT
Searches characterbased data for words using meaning, rather than literal values.
IN
Provides an inclusive list of values for the search condition
.
IS NOT NULL
if the value is NOT null.
IS NULL
whether the value is null.
LIKE
pattern matching.
NOT BETWEEN
Specifies a range of values NOT to include.
NOT IN
Provides a list of values for which NOT to return rows for.
NOT LIKE
Tests character string, excluding those with pattern matches.
SOME
If any retrieved values satisfy the search condition, the row
s will be retrieved.

Use local variable in a select statement


3> create table Billings (
4>
BankerID
INTEGER,
5>
BillingNumber
INTEGER,
6>
BillingDate
datetime,
7>
BillingTotal
INTEGER,
8>
TermsID
INTEGER,
9>
BillingDueDate
datetime ,
10>
PaymentTotal
INTEGER,
11>
CreditTotal
INTEGER
12>
13> );
14> GO

1>
2> INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-0422',123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-0222',123,321.);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-0412',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-0417',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-0419',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-0420',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-0421',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-0422',123,321);
2> GO
(1 rows affected)
1>
2> DECLARE @intvar int
3> SET @intvar = 1
4>
5> SELECT TermsID, BillingNumber 'Category name',
6>
@intvar 'Local variable'
7> FROM Billings
8> GO
TermsID
Category name Local variable
----------- ------------- --------------

1
1
1
1
1
1
1
1
1
1

1
2
3
4
5
6
7
8
9
0

1
1
1
1
1
1
1
1
1
1

(10 rows affected)


1>
2>
3> drop table Billings;
4> GO

Using SELECT to Create a Script


4>
5>
6> CREATE TABLE employee(
7>
id
INTEGER NOT NULL PRIMARY KEY,
8>
first_name VARCHAR(10),
9>
last_name
VARCHAR(10),
10>
salary
DECIMAL(10,2),
11>
start_Date DATETIME,
12>
region
VARCHAR(10),
13>
city
VARCHAR(20)
14> );
15> GO
1> INSERT INTO employee VALUES (1, 'Jason' , 'Martin', 5890,'2005-0322','North','Vancouver');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, 'Alison',
21','South','Utown');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, 'James' ,
01','North','Paris');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, 'Celia' ,
03','South','London');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, 'Robert',
02','East','Newton');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, 'Linda' ,
19','East','Calgary');

'Mathews',4789,'2003-07-

'Smith',

6678,'2001-12-

'Rice',

5567,'2006-03-

'Black',

4467,'2004-07-

'Green' , 6456,'2002-05-

2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, 'David' ,
18','West','New York');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (8, 'James' ,
17','West','Regina');
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, 'Joan'
16','North','Toronto');
2> GO

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

'Larry',

5345,'2008-03-

'Cat',

4234,'2007-07-

'Act',

6123,'2001-04-

start_Date

regi

----------------------- ---2005-032003-072001-122006-032004-072002-052008-032007-072001-04-

rows affected)

SELECT column_name + ' IS NULL AND '


FROM INFORMATION_SCHEMA.columns
WHERE table_name = 'Employee'
ORDER BY ORDINAL_POSITION
GO

------------------------------------------------------------------------------------------------------------------------------------------id IS NULL AND


first_name IS NULL AND

last_name IS NULL AND


salary IS NULL AND
start_Date IS NULL AND
region IS NULL AND
city IS NULL AND
(7 rows affected)
1>
2>
3> drop table employee;
4> GO

The syntax of the INSERT statement for inserting a single row


INSERT [INTO] table_name [(column_list)]
[DEFAULT] VALUES (expression_1 [, expression_2]...)

INSERT Statement
If you only need to provide a value for this column
10>
11> CREATE TABLE Product (
12>
Name nchar (5) NOT NULL
13> )
14> GO
1>
2> INSERT INTO Product (Name) SELECT 'Wid'
3> GO
(1 rows affected)
1>
2> select * from Product;
3> GO
Name
----Wid
(1 rows affected)
1>
2> drop table Product;
3> GO

Inserting Data for a Subset of Table Columns


4>
5>

6> CREATE TABLE T (


7>
int1 int,
8>
bit1 bit,
9>
varchar1 varchar(3),
10>
dec1 dec(5,2),
11>
cmp1 AS (int1 + bit1)
12> )
13> GO
1>
2> INSERT T (int1, bit1) VALUES (1,
3> INSERT T (int1, varchar1) VALUES
4> INSERT T (int1, dec1) VALUES (3,
5> INSERT T (bit1, dec1) VALUES (1,
6> GO

0)
(2, 'abc')
5.25)
9.75)

(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
1>
2> drop table t;
3> GO

The insertion of values into some (but not all) of a table's columns usually requires the
explicit specification of the corresponding columns.
The omitted columns must be either nullable or have a DEFAULT value.
6> CREATE TABLE employee(
7>
id
INTEGER NOT NULL PRIMARY KEY,
8>
first_name VARCHAR(10),
9>
last_name
VARCHAR(10),
10>
salary
DECIMAL(10,2),
11>
start_Date DATETIME,
12>
region
VARCHAR(10),
13>
city
VARCHAR(20),
14>
managerid
INTEGER
15> );
16> GO
1> INSERT INTO employee (id) VALUES (3);
2> GO
(1 rows affected)
1>
2> select * from employee;
3> GO
id
first_name last_name salary
start_Date
regi
on
city
managerid
----------- ---------- ---------- ------------ ----------------------- --------- -------------------- ----------3 NULL
NULL
NULL
NULL NULL
NULL
NULL
(1 rows affected)
1>

2>
3>
4>
5> drop table employee;
6> GO
1>

The order of column names in the VALUE clause of the INSERT statement can be different
from the original order of those columns.
6> CREATE TABLE employee(
7>
id
INTEGER NOT NULL PRIMARY KEY,
8>
first_name VARCHAR(10),
9>
last_name
VARCHAR(10),
10>
salary
DECIMAL(10,2),
11>
start_Date DATETIME,
12>
region
VARCHAR(10),
13>
city
VARCHAR(20),
14>
managerid
INTEGER
15> );
16> GO
1> INSERT INTO employee (id) VALUES (3);
2> GO
(1 rows affected)
1>
2> select * from employee;
3> GO
id
first_name last_name salary
start_Date
regi
on
city
managerid
----------- ---------- ---------- ------------ ----------------------- --------- -------------------- ----------3 NULL
NULL
NULL
NULL NULL
NULL
NULL
(1 rows affected)
1>
2>
3> INSERT INTO employee (first_name, id) VALUES (1,'Davis')
4>
5>
6>
7>
8> drop table employee;
9> GO
Msg 245, Level 16, State 1, Server J\SQLEXPRESS, Line 3
Conversion failed when converting the varchar value 'Davis' to data type in
t.

An INSERT statement that adds the new row using a column list
5>
6>
7> create table Billings (
8>
BankerID
INTEGER,

9>
BillingNumber
INTEGER,
10>
BillingDate
datetime,
11>
BillingTotal
INTEGER,
12>
TermsID
INTEGER,
13>
BillingDueDate
datetime ,
14>
PaymentTotal
INTEGER,
15>
CreditTotal
INTEGER
16>
17> );
18> GO
1>
2> INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-0422',123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-0222',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-0412',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-0417',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-0419',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-0420',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-0421',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-0422',123,321);
2> GO
(1 rows affected)

1>
2>
3> INSERT INTO Billings
4>
(BankerID, BillingNumber, BillingTotal, PaymentTotal, CreditTotal,
5>
TermsID, BillingDate, BillingDueDate)
6> VALUES
7>
(97, '456789', 8344.50, 0, 0, 1, '2002-08-01', '2002-08-31')
8> GO
(1 rows affected)
1>
2>
3> drop table Billings;
4> GO

INSERT statement with a column list


6> create table Billings (
7>
BankerID
INTEGER,
8>
BillingNumber
INTEGER,
9>
BillingDate
datetime,
10>
BillingTotal
INTEGER,
11>
TermsID
INTEGER,
12>
BillingDueDate
datetime ,
13>
PaymentTotal
INTEGER,
14>
CreditTotal
INTEGER
15>
16> );
17> GO
1>
2> INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-0422',123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-0222',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-0412',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-0418',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-0417',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-0418',123,321);
2> GO

(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-0419',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-0420',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-0421',123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-0422',123,321);
2> GO
(1 rows affected)
1>
2>
3> create table BillingArchive (
4>
BankerID
INTEGER,
5>
BillingNumber
INTEGER,
6>
BillingDate
datetime,
7>
BillingTotal
INTEGER,
8>
TermsID
INTEGER,
9>
BillingDueDate
datetime ,
10>
PaymentTotal
INTEGER,
11>
CreditTotal
INTEGER
12>
13> );
14> GO
1>
2>
3> INSERT INTO BillingArchive
4>
(BankerID, BillingNumber, BillingTotal, CreditTotal,
5>
PaymentTotal, TermsID, BillingDate, BillingDueDate)
6> SELECT
7>
BankerID, BillingNumber, BillingTotal, CreditTotal,
8>
PaymentTotal, TermsID, BillingDate, BillingDueDate
9> FROM Billings
10> WHERE BillingTotal - PaymentTotal - CreditTotal = 0
11> GO
(0
1>
2>
3>
4>
5>

rows affected)
drop table BillingArchive;
drop table Billings;
GO

Use declared variables in insert statement


2>

3>
4> IF EXISTS(SELECT name FROM sys.tables
5>
WHERE name = 'T')
6>
DROP TABLE T
7> GO
1>
2> CREATE TABLE T (
3>
c1 int,
4>
c2 varchar(8000)
5> )
6> GO
1>
2> DECLARE @v1 varchar(max)
3>
4> SET @v1 = REPLICATE('A',7999) + 'B'
5> INSERT T VALUES (1, @v1)
6> SELECT RIGHT(c2,2) 'Right 2 of c2' FROM T
7>
8> SET @v1 = @v1 + 'B'
9> INSERT T VALUES (2, @v1)
10> SELECT RIGHT(c2,2) 'Right 2 of c2' FROM T
11>
12> GO
(1 rows affected)
Right 2 of c2
------------AB
Msg 8152, Level 16, State 10, Server J\SQLEXPRESS, Line 9
String or binary data would be truncated.
The statement has been terminated.
(1 rows affected)
Right 2 of c2
------------AB
(1 rows affected)
1>
2> select * from t;
3> GO
c1
c2

eliminate the optional column list and allow SQL Server to assume we're providing
something for every column
3>
4> CREATE TABLE stores(
5>
stor_id
char(4)
6>
stor_name
varchar(40)
7>
stor_address
varchar(40)
8>
city
varchar(20)
9>
state
char(2)
10>
zip
char(5)
11> )
12> GO
1>
2>
3>
INSERT INTO stores

NOT NULL,
NULL,
NULL,
NULL,
NULL,
NULL

4>
VALUES ('TEST', 'Test Store', '1234 Anywhere Street', 'Here', 'NY', '
00319')
5> GO
(1 rows affected)
1>
2>
INSERT INTO stores (stor_id, stor_name, city, state, zip)
3>
VALUES ('TST2', 'Test Store', 'Here', 'NY', '00319')
4> GO
(1 rows affected)
1>
2>
SELECT * FROM stores WHERE stor_id = 'TST2'
3> GO
stor_id stor_name
stor_address
city
state zip
------- ---------------------------------------- --------------------------------------- -------------------- ----- ----TST2
Test Store
NULL
Here
NY
00319
(1 rows affected)
1>
2>
3> drop table stores;

How to add rows to a table based on the output of a stored procedure


The syntax for inserting data from a stored procedure is as follows:
INSERT
[ INTO]
table_or_view_name
[ ( column_list ) ]
EXEC stored_procedure_name
12> CREATE TABLE employee(
13>
id
INTEGER,
14>
first_name VARCHAR(10),
15>
last_name
VARCHAR(10),
16>
salary
DECIMAL(10,2),
17>
start_Date DATETIME,
18>
region
VARCHAR(10),
19>
city
VARCHAR(20),
20>
managerid
INTEGER
21> );
22> GO
1> INSERT INTO employee VALUES (1, 'Jason' ,
22','North','Vancouver',3);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, 'Alison',
21','South','Utown',4);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, 'James' ,
01','North','Paris',5);

'Martin', 5890,'2005-03-

'Mathews',4789,'2003-07-

'Smith',

6678,'2001-12-

2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, 'Celia' ,
03','South','London',6);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, 'Robert',
02','East','Newton',7);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, 'Linda' ,
19','East','Calgary',8);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, 'David' ,
18','West','New York',9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (8, 'James' ,
17','West','Regina',9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, 'Joan'
16','North','Toronto',10);
2> GO

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

'Rice',

5567,'2006-03-

'Black',

4467,'2004-07-

'Green' , 6456,'2002-05-

'Larry',

5345,'2008-03-

'Cat',

4234,'2007-07-

'Act',

6123,'2001-04-

start_Date

regi

----------------------- ---2005-033
2003-074
2001-125
2006-036
2004-077
2002-058
2008-039
2007-079
2001-0410

2>
3>
4> CREATE PROCEDURE myProc
5> @StartDT datetime
6> AS
7> SELECT ID, first_name, last_name
8> FROM employee
9> WHERE start_Date > @StartDT
10> GO
1>
2> EXEC myProc '6/2/04'
3>
4> INSERT employee(ID, first_name, last_name)
5> EXEC myProc '6/2/04'
6>
7> select * from employee;
8> GO
ID
first_name last_name
----------- ---------- ---------1 Jason
Martin
4 Celia
Rice
5 Robert
Black
7 David
Larry
8 James
Cat
(5 rows affected)
id
first_name last_name salary
on
city
managerid
----------- ---------- ---------- ----------------- -------------------- ----------1 Jason
Martin
5890.00
22 00:00:00.000 North
Vancouver
2 Alison
Mathews
4789.00
21 00:00:00.000 South
Utown
3 James
Smith
6678.00
01 00:00:00.000 North
Paris
4 Celia
Rice
5567.00
03 00:00:00.000 South
London
5 Robert
Black
4467.00
02 00:00:00.000 East
Newton
6 Linda
Green
6456.00
19 00:00:00.000 East
Calgary
7 David
Larry
5345.00
18 00:00:00.000 West
New York
8 James
Cat
4234.00
17 00:00:00.000 West
Regina
9 Joan
Act
6123.00
16 00:00:00.000 North
Toronto
1 Jason
Martin
NULL
NULL
NULL
4 Celia
Rice
NULL
NULL
NULL
5 Robert
Black
NULL
NULL
NULL
7 David
Larry
NULL
NULL
NULL
8 James
Cat
NULL
NULL
NULL
(14 rows affected)
1>

start_Date

regi

----------------------- ---2005-033
2003-074
2001-125
2006-036
2004-077
2002-058
2008-039
2007-079
2001-0410
NULL NULL
NULL NULL
NULL NULL
NULL NULL
NULL NULL

2> drop procedure myProc;


3> drop table employee;
4> GO
1>

How to add rows to a table based on the output of a stored procedure


The syntax for inserting data from a stored procedure is as follows:
INSERT
[ INTO]
table_or_view_name
[ ( column_list ) ]
EXEC stored_procedure_name
12> CREATE TABLE employee(
13>
id
INTEGER,
14>
first_name VARCHAR(10),
15>
last_name
VARCHAR(10),
16>
salary
DECIMAL(10,2),
17>
start_Date DATETIME,
18>
region
VARCHAR(10),
19>
city
VARCHAR(20),
20>
managerid
INTEGER
21> );
22> GO
1> INSERT INTO employee VALUES (1, 'Jason' ,
22','North','Vancouver',3);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, 'Alison',
21','South','Utown',4);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, 'James' ,
01','North','Paris',5);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, 'Celia' ,
03','South','London',6);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, 'Robert',
02','East','Newton',7);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, 'Linda' ,
19','East','Calgary',8);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, 'David' ,
18','West','New York',9);
2> GO

'Martin', 5890,'2005-03-

'Mathews',4789,'2003-07-

'Smith',

6678,'2001-12-

'Rice',

5567,'2006-03-

'Black',

4467,'2004-07-

'Green' , 6456,'2002-05-

'Larry',

5345,'2008-03-

(1 rows affected)
1> INSERT INTO employee VALUES (8, 'James' ,
17','West','Regina',9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, 'Joan'
16','North','Toronto',10);
2> GO

(1 rows affected)
1>
2> select * from employee;
3> GO
id
first_name last_name salary
on
city
managerid
----------- ---------- ---------- ----------------- -------------------- ----------1 Jason
Martin
5890.00
22 00:00:00.000 North
Vancouver
2 Alison
Mathews
4789.00
21 00:00:00.000 South
Utown
3 James
Smith
6678.00
01 00:00:00.000 North
Paris
4 Celia
Rice
5567.00
03 00:00:00.000 South
London
5 Robert
Black
4467.00
02 00:00:00.000 East
Newton
6 Linda
Green
6456.00
19 00:00:00.000 East
Calgary
7 David
Larry
5345.00
18 00:00:00.000 West
New York
8 James
Cat
4234.00
17 00:00:00.000 West
Regina
9 Joan
Act
6123.00
16 00:00:00.000 North
Toronto
(9 rows affected)
1>
2>
3>
4> CREATE PROCEDURE myProc
5> @StartDT datetime
6> AS
7> SELECT ID, first_name, last_name
8> FROM employee
9> WHERE start_Date > @StartDT
10> GO
1>
2> EXEC myProc '6/2/04'
3>
4> INSERT employee(ID, first_name, last_name)
5> EXEC myProc '6/2/04'
6>
7> select * from employee;
8> GO
ID
first_name last_name
----------- ---------- ---------1 Jason
Martin
4 Celia
Rice

'Cat',

4234,'2007-07-

'Act',

6123,'2001-04-

start_Date

regi

----------------------- ---2005-033
2003-074
2001-125
2006-036
2004-077
2002-058
2008-039
2007-079
2001-0410

5 Robert
7 David
8 James

Black
Larry
Cat

(5 rows affected)
id
first_name last_name salary
on
city
managerid
----------- ---------- ---------- ----------------- -------------------- ----------1 Jason
Martin
5890.00
22 00:00:00.000 North
Vancouver
2 Alison
Mathews
4789.00
21 00:00:00.000 South
Utown
3 James
Smith
6678.00
01 00:00:00.000 North
Paris
4 Celia
Rice
5567.00
03 00:00:00.000 South
London
5 Robert
Black
4467.00
02 00:00:00.000 East
Newton
6 Linda
Green
6456.00
19 00:00:00.000 East
Calgary
7 David
Larry
5345.00
18 00:00:00.000 West
New York
8 James
Cat
4234.00
17 00:00:00.000 West
Regina
9 Joan
Act
6123.00
16 00:00:00.000 North
Toronto
1 Jason
Martin
NULL
NULL
NULL
4 Celia
Rice
NULL
NULL
NULL
5 Robert
Black
NULL
NULL
NULL
7 David
Larry
NULL
NULL
NULL
8 James
Cat
NULL
NULL
NULL
(14 rows affected)
1>
2> drop procedure myProc;
3> drop table employee;
4> GO
1>

start_Date

regi

----------------------- ---2005-033
2003-074
2001-125
2006-036
2004-077
2002-058
2008-039
2007-079
2001-0410
NULL NULL
NULL NULL
NULL NULL
NULL NULL
NULL NULL

You might also like