Download presentation
Presentation is loading. Please wait.
Published byAnnabella Lindsey Modified over 6 years ago
1
SQL Transactions 4/23/2019 See scm-intranet
2
Objective To develop an understanding of the role of SQL transactions
To study mechanisms available for encapsulating SQL transactions 4/23/2019 See scm-intranet
3
SQL Routines A routine is a number of SQL statements defined as a compound statement which is stored by the DBMS and executed as a single action. Standardised through SQL/PSM 1996 (Persistent Stored Modules). Two kinds-functions and procedures. 4/23/2019 See scm-intranet
4
Functions e.g. Create Function greater(a integer, b integer)
Create Function greater(a integer, b integer) Returns integer Begin If a>b then Return a; Else Return b; End if End 4/23/2019 See scm-intranet
5
Example Create function city_pop(my_city varchar(24))
Returns decimal(6,1) Begin Declare my_pop decimal (6,1); Select population Into my_pop From city Where name=my_city; Return my_pop; End Used like functions such as sum, count etc.. e.g. select city_pop(‘london’); 4/23/2019 See scm-intranet
6
Procedures Create procedure proc_pop (in my_city varchar(24),
Create procedure proc_pop (in my_city varchar(24), out my_pop decimal(6,1)) Begin Select population Into my_pop From city Where name=my_city; End Call proc_pop('Paris', paris_pop) 4/23/2019 See scm-intranet
7
Routines can be given access privileges
Grant execute on city_pop to admin; Execute privilege gives a user access to a database table without any other privileges. So, access for very specific purposes can defined. 4/23/2019 See scm-intranet
8
SQL*Server-Stored Procedures
These are persistent re-useable programs written in an extended form of SQL (extended to include flow-of-control statements). Create procedure name parameter_list As sql_statements; Simple example (no parameters)- Create procedure all_employees As select * from employee; Procedure is invoked by- Execute all_employees; (may work by simply executing the procedure name) 4/23/2019 See scm-intranet
9
Example using parameters- Create procedure work_proc
int) as Insert into worker Values @ indicates a parameter (which can be used in the same manner as variables in other programming languages). Cannot use text or image data types for parameters/variables. This can be invoked thus- Work_proc ('Bob Smith','Sales',3333); This inserts a single row of data into the worker table. 4/23/2019 See scm-intranet
10
Global variables are referenced using @@.
Only local variables can be declared. Global variables are defined and made available by SQL*Server. Lists of global variables in Transact*SQL Help under ‘globals’. Used mainly for system and status information. 4/23/2019 See scm-intranet
11
sp_helptext work_proc Stored procedures are removed using
Stored procedures sp_help and sp_helptext can be used to gain information on your stored procedures. e.g. sp_helptext work_proc Stored procedures are removed using Drop procedure work_proc 4/23/2019 See scm-intranet
12
Using Flow-of-Control
The various components are illustrated in the following simple examples- int {Declaring variables} int Select values} Begin Print 'x still less than 5' Select ……….{incrementing variable values} Select Print 'y is 2 so break out of loop' Break …….{jumping out of loop....} End Print 'out of while loop' 4/23/2019 See scm-intranet
13
Next example- Begin tran …{begin transaction}
While (select avg(price) from titles)<30 Begin Select title_id, price From title Where price>20 Update title set price=price*2 End End tran 4/23/2019 See scm-intranet
14
Returning Values Procedures can call other procedures.
The only value that can be returned is an integer. This may be a status value from SQL*Sever indicating success or otherwise of the procedure. Or it might be a value provided by the called procedure. Values 0, -1 to -14 are system status values. 4/23/2019 See scm-intranet
15
Values received using- Execute @return_status=procedure_name
Select 4/23/2019 See scm-intranet
16
Summary SQL operations can be encapsulated into transactions
These can be in the form of persistent functions or procedures SQL is extended to include parameter passing and flow-of-control statements 4/23/2019 See scm-intranet
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.