Insert all statements in a batch or to roll back the entire statement group if an error occurs : Insert Data « Transact SQL « SQL Server / T-SQL

Home
SQL Server / T-SQL
1.Aggregate Functions
2.Analytical Functions
3.Constraints
4.Cursor
5.Data Set
6.Data Type
7.Database
8.Date Timezone
9.Index
10.Insert Delete Update
11.Math Functions
12.Select Query
13.Sequence
14.Store Procedure Function
15.String Functions
16.Subquery
17.System
18.Table
19.Table Joins
20.Transact SQL
21.Transaction
22.Trigger
23.View
24.XML
SQL Server / T-SQL » Transact SQL » Insert Data 
Insert all statements in a batch or to roll back the entire statement group if an error occurs


20>
21>
22CREATE TABLE employee  (emp_no    INTEGER NOT NULL,
23>                         emp_fname CHAR(20NOT NULL,
24>                         emp_lname CHAR(20NOT NULL,
25>                         dept_no   CHAR(4NULL)
26>
27insert into employee values(1,  'Matthew', 'Smith',    'd3')
28insert into employee values(2,  'Ann',     'Jones',    'd3')
29insert into employee values(3,  'John',    'Barrimore','d1')
30insert into employee values(4,  'James',   'James',    'd2')
31insert into employee values(5,  'Elsa',    'Bertoni',  'd2')
32insert into employee values(6,  'Elke',    'Hansel',   'd2')
33insert into employee values(7,  'Sybill',  'Moser',    'd1')
34>
35select from employee
36> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
emp_no      emp_fname            emp_lname            dept_no
----------- -------------------- -------------------- -------
          Matthew              Smith                d3
          Ann                  Jones                d3
          John                 Barrimore            d1
          James                James                d2
          Elsa                 Bertoni              d2
          Elke                 Hansel               d2
          Sybill               Moser                d1

(rows affected)
1>
2> -- Use exception handling to insert all statements in a batch or to roll back the entire statement group if an error occurs.
3>
4BEGIN TRY
5>        BEGIN TRANSACTION
6>        insert into employee values(11111'Ann', 'Smith','d2')
7>        insert into employee values(22222'Matthew', 'Jones','d4')
8>        insert into employee values(33333'John', 'Barrimore', 'd2000000000000')
9>        COMMIT TRANSACTION
10>        PRINT 'Transaction committed'
11>       END TRY
12>       BEGIN CATCH
13>        ROLLBACK
14>        PRINT 'Transaction rolled back'
15>       END CATCH
16> GO

(rows affected)

(rows affected)

(rows affected)
Transaction rolled back
1select from employee
2> GO
emp_no      emp_fname            emp_lname            dept_no
----------- -------------------- -------------------- -------
          Matthew              Smith                d3
          Ann                  Jones                d3
          John                 Barrimore            d1
          James                James                d2
          Elsa                 Bertoni              d2
          Elke                 Hansel               d2
          Sybill               Moser                d1

(rows affected)
1> drop table employee
2> GO
1>
           
       
Related examples in the same category
1.Insert data to a table in the procedure
2.Checks for existing Product record: update or insert
3.Insert command and variables
4.Stored procedure: accepts a ID, name and city and inserts them as new row
5.Managing Inserts Using Stored Procedures
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.