Transaction in a procedure : Create Procedure « Store Procedure Function « SQL Server / T-SQL

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Store Procedure Function » Create Procedure 
Transaction in a procedure

 

3>
4CREATE TABLE Employees(
5>   empid   int         NOT NULL,
6>   mgrid   int         NULL,
7>   empname varchar(25NOT NULL,
8>   salary  money       NOT NULL
9)
10> GO
1INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(1, NULL, 'Nancy', $10000.00)
2INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(21'Andrew', $5000.00)
3INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(31'Janet', $5000.00)
4INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(41'Margaret', $5000.00)
5INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(52'Steven', $2500.00)
6INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(62'Michael', $2500.00)
7INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(73'Robert', $2500.00)
8INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(83'Laura', $2500.00)
9INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(93'Ann', $2500.00)
10INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(104'Ina', $2500.00)
11INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(117'David', $2000.00)
12INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(127'Ron', $2000.00)
13INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(137'Dan', $2000.00)
14INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(1411'James', $1500.00)
15INSERT INTO Employees(empid, mgrid, empname, salary)  VALUES(1512'Sean', $1500.00)
16> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3CREATE PROC RemoveEmployeeMoveSubs
4(
5>   @empid  int,
6>   @newmgr int
7)
8> AS
9BEGIN TRAN
10UPDATE E
11>   SET mgrid = @newmgr
12FROM
13>     Employees AS E
14>   JOIN
15>     Employees AS M ON E.mgrid = M.empid
16WHERE
17>   M.empid = @empid
18DELETE FROM Employees
19WHERE
20>   empid = @empid
21> COMMIT TRAN
22> GO
1> --Testing the RemoveEmployeeMoveSubs Stored Procedure
2> EXEC RemoveEmployeeMoveSubs
3>   @empid  = 3,
4>   @newmgr = 4
5>
6> drop table Employees;
7> GO

(rows affected)

(rows affected)

 
Related examples in the same category
1. Performing Data Manipulation: Adding a New Row
2. Returning a Single Result Set from a procedure
3. Check to see if a procedure exists
4. Define procedure to insert or update
5. Store Procedure: Returns the Customer record given a parameter of the ID
6. Store procedure: pre-check for the existence of the foreign key (RegionID) before attempting the insert
7. Processing Return Status Values
8. Return only one value from the procedure: without the use of an output parameter
9. Using Parameters
10. Stored Procedures as Parameterized Views
11. RECOMPILE(ing) a Stored Procedure Each Time It Is Executed
12. Stored procedure can be executed with the parameter and assigned value
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.