Stored Procedures & User-Defined Functions: Hanoi University of Technology
Stored Procedures & User-Defined Functions: Hanoi University of Technology
User-Defined Functions
Vu Tuyet Trinh
[email protected]
Hanoi University of Technology
1
Introduction to SQL Batch Processing
Individual SQL
commands
Grouped to form a
batch
Batch
Compiled
as single
execution
plan
Microsoft
Example
Use Pubs
Select * from authors
Update authors
set phone= '890 451-7366‘
where au_lname= 'White'
Go
Microsoft
Outline
Microsoft 4
Assigning values to variables
SET statement
SET @local_variable name = value
SELECT statement
SELECT @local_variable name = value
SELECT CUSTOMERID,COMPANYNAME
FROM CUSTOMERS
WHERE CUSTOMERID = @CUST
Microsoft
Variables
@local_variable
Microsoft
List of Global variables
Microsoft
List of Global variables (2)
Microsoft
Cursors to Retrieve Data
Allowing positioning at specific rows of the result set
Retrieving one row or block of rows from the current
position in the result set
Supporting data modifications to the rows at the current
position in the result set
Supporting different levels of visibility for changes made
by other users to the data in the result set
Providing access to the data in a result set for T-SQL
statements in scripts, stored procedures, and triggers
Microsoft
Cursor Implementations
Transact-SQL Server Cursors
Used in scripts, stored procedures, and triggers
Implemented on the server
Client Cursors
Entire result set is cached on client and all cursor
operations are performed against this cached set
Microsoft
Working with T-SQL Server Cursors
Declare the cursor
Populate the cursor
Retrieve (fetch) the result set
First, Next, Prior, Last, Absolute n, Relative n
Optional: update or delete a row
Close the cursor
Free resources allocated to the cursor
Microsoft
Control Statements
Microsoft
Control Statements Contd…
Microsoft
BEGIN..END
Syntax:
BEGIN
{ statement | statement_block}
END
Microsoft
IF..ELSE
Syntax:
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement |
statement_block } ]
Microsoft
Example
Microsoft
WHILE construct
Syntax:
WHILE Boolean_expression
{ statement | statement_block }
[ BREAK ]
{ statement | statement_block
}
[ CONTINUE ]
Microsoft
Example
USE pubs
GO
WHILE (SELECT AVG(price) FROM titles) < $30
BEGIN
UPDATE titles
SET price = price * 2
SELECT MAX(price) FROM titles
IF (SELECT MAX(price) FROM titles) > $50
BREAK
ELSE
CONTINUE
END
PRINT 'Too much for the market to bear'
Microsoft
GOTO keyword
GOTO:
We can change the flow of execution to a specified location
(label). The statements after GOTO keyword are skipped and
the execution process continues at the specified label in the
GOTO statement.
Syntax:
GOTO label
Microsoft
RETURN
Syntax:
RETURN [ integer_expression ]
Microsoft
CASE statement
Allowing to return a value based on whether an
expression is true.
Can be used wherever an expression is allowed.
Syntax:
CASE expression
WHEN expression1 THEN expression1
[[WHEN expression2 THEN expression2] […]]
[ELSE expression]
END
Example:
Microsoft
Math Functions
AVG()
MIN()
MAX()
SUM()
COUNT()
SQUARE()
SQRT()
ROUND()
Microsoft
String Functions
ASCII()
CHAR()
UPPER()
LOWER()
LEN()
LTRIM()
RTRIM()
LEFT()
RIGHT()
Microsoft
Time Functions
GETDATE()
DATEPART(YY,getdate())
DATEDIFF(X,Y,Z)
DAY()
MONTH()
YEAR()
Microsoft
System Functions
DB_ID([‘database_name’])
DB_NAME([database_id])
HOST_ID()
HOST_NAME()
ISNULL(expr,value)
OBJECT_ID(‘obj_name’)
OBJECT_NAME(object_id)
SUSER_SID([‘login_name’])
SUSER_ID([‘login_name’])
SUSER_SNAME([server_user_id])
SUSER_NAME([server_user_id])
USER_ID([‘user_name’])
USER_NAME([user_id])
Microsoft
Outline
Microsoft 27
Stored Procedures
Main ideas
move the processing as close to the data as possible.
Move into a batch that has been stored with a name so it can be
pre-compiled
Advantages
better performance
higher productivity
ease of use
increased scalability.
Microsoft
Creating a Stored Procedure
USE SQLProject;
go
CREATE PROCEDURE GetSubjects
AS
SELECT SubjectCode, Name, Semester, CreditPoint
FROM dbo.Subject;
RETURN;
Microsoft
Modifying a Stored Procedure
Microsoft
Executing a Stored Procedure
Example
Execute dbo.GetSubjects
Microsoft
Outline
Microsoft 33
User-Defined Function
Microsoft
Creating a Scalar Function
Microsoft
Creating a Inline Table Function
Microsoft
Creating a Multi-statement table function
Microsoft
Modifying a Scalar Function
Microsoft
Modifying a Inline Table Functions
Microsoft
Modifying a Multi-statement Table Function
Microsoft
Summary
Microsoft 41
Microsoft