SQL Server/T-SQL Tutorial/Transact SQL/Insert Statement

Материал из SQL эксперт
Перейти к: навигация, поиск

Creation Script for the usp_EmployeeInsert Stored Procedure

5>
6>
7> CREATE TABLE Employees
8> (orderid int NOT NULL,
9>  customerid int NULL,
10>  empname varchar(25) NOT NULL,
11>  orderdate Datetime,
12>  salary money NOT NULL);
13> GO
1>
2> CREATE PROC dbo.usp_EmployeeInsert
3>   @orderid    int,
4>   @customerid char(5),
5>   @orderdate  datetime
6> AS
7> INSERT INTO Employees(orderid, customerid, orderdate)
8>   VALUES(@orderid, @customerid, @orderdate)
9> GO
1>
2>
3> drop proc dbo.usp_EmployeeInsert;
4> GO
1>
2> drop table Employees;
3> GO


Pass value into a procedure and insert it to a table

5> create table Billings (
6>     BankerID           INTEGER,
7>     BillingNumber      INTEGER,
8>     BillingDate        datetime,
9>     BillingTotal       INTEGER,
10>     TermsID            INTEGER,
11>      BillingDueDate     datetime ,
12>      PaymentTotal       INTEGER,
13>      CreditTotal        INTEGER
14>
15>  );
16>  GO
1>
2>
3>
4> CREATE PROC usp_AddBillings
5> @ClassIDval int
6> AS
7> INSERT Billings (BankerID)VALUES (@ClassIDval)
8> GO
1>
2> EXEC usp_AddBillings 2
3> GO
(1 rows affected)
1>
2>
3> drop PROC usp_AddBillings;
4> GO
1> drop table Billings;
2> GO


Procedure for bulk insert

4> CREATE PROC usp_BULKINSERT
5> AS
6> BULK INSERT Billings FROM "c:\NewClasses.txt"
7> GO
1>
2> EXEC usp_BULKINSERT
3> GO
1>


use a stored procedure to insert data to a table

7> CREATE TABLE employee(
8>    id          INTEGER NOT NULL PRIMARY KEY,
9>    first_name  VARCHAR(10),
10>    last_name   VARCHAR(10),
11>    salary      DECIMAL(10,2),
12>    start_Date  DATETIME,
13>    region      VARCHAR(10),
14>    city        VARCHAR(20),
15>    managerid   INTEGER
16> );
17> 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> CREATE PROCEDURE spIns_Employee
4> @FirstName    nVarChar(50),
5> @LastName  nVarChar(25),
6> @Salary   Money
7> AS
8> INSERT INTO Employee (ID, First_name, last_Name, Salary)
9> SELECT 10, @FirstName, @LastName, @Salary
10> GO
1> spIns_Employee "Gadget", "Bond", 49.95
2> GO
(1 rows affected)
1> drop procedure spIns_Employee;
2> GO
1> drop table employee;
2> GO
1>
2>


Using the INSERT DEFAULT VALUES Statement

6> CREATE TABLE MyTable(
7>   ID           int          NULL IDENTITY (1, 1),
8>   EntryDate    datetime     NULL DEFAULT (GETDATE ()),
9>   Description  varchar (20) NULL
10> )
11> GO
Msg 8147, Level 16, State 1, Server J\SQLEXPRESS, Line 6
Could not create IDENTITY attribute on nullable column "ID", table "MyTable".
1> INSERT MyTable DEFAULT VALUES
2>
3> drop table MyTable
4> GO
Msg 208, Level 16, State 1, Server J\SQLEXPRESS, Line 1
Invalid object name "MyTable".
1>