SQL Server - Bài
Trang 1Stored Procedures &
Trang 2Introduction to SQL Batch Processing
Batch
Individual SQL
commands
Grouped to form a batch
Compiled
as single execution plan
Trang 3Use PubsSelect * from authorsUpdate authors
set phone= '890 451-7366‘where au_lname= 'White'Go
Trang 4 Understanding the concepts of batch and batch
processing
T-SQL Programming
Define and assign variables
Cursors for data retrieval
Control statements
Write SQL statements using SQL Server basic functions
Use basic functions in a query
Implementing Stored Procedures
Implementing User-Defined Functions
Trang 5Assigning values to variables
SET statement
SET @local_variable name = value
SELECT statement
SELECT @local_variable name = value
SELECT CUSTOMERID,COMPANYNAME FROM CUSTOMERS
Trang 6SQL Server supports two types of variables in T-SQL
@@global_variable
@local_variable
Trang 7List of Global variables
@@CONNECTIONS Number of connections made to the server
since it was last started
@@CPU_BUSY Number of milliseconds the system has been
processing since SQL Server was started
@@CURSOR_ROWS Number of rows in the most recently opened
cursor
@@ERROR Error number of the last T-SQL error
@@FETCH_STATUS 0 if the last fetch status was successful
-1 if there was an error
Trang 8List of Global variables (2)
@@IDENTITY Last inserted identity value
@@LANGUAGE Name of the language currently in use
@@MAX_CONNECTIONS Maximum number of concurrent
connections that can be made
@@ROWCOUNT Number of rows affected by most
recent SQL Statement
@@SERVERNAME Name of local server
@@SERVICENAME Name of the SQL Service on this
computer
@@TIMETICKS Number of microseconds per tick on
the current computer
@@TRANSCOUNT Number of transaction open on the
current connection
Trang 9Cursors 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
Trang 10Cursor Implementations
Transact-SQL Server Cursors
Used in scripts, stored procedures, and triggers
Implemented on the server
API Server Cursors
Implemented on the server but managed by API cursor functions (OLE DB, ODBC, DB-Library)
Client Cursors
Entire result set is cached on client and all cursor
operations are performed against this cached set
Trang 11Working 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
Trang 12Control Statements
Trang 13Control Statements Contd…
Trang 15We can execute different
sets of SQL statements based on
Trang 16Example
Trang 17WHILE construct
We can execute a SQL statement or a
block of statements based on some condition
Trang 18SET price = price * 2
SELECT MAX(price) FROM titles
IF (SELECT MAX(price) FROM titles) > $50
Trang 19GOTO 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
Trang 20RETURN: We can use RETURN at any point to exit from a block, procedure Statements after the RETURN statement are not executed
Syntax:
RETURN [ integer_expression ]
Trang 21WHEN expression1 THEN expression1
[[WHEN expression2 THEN expression2] […]]
[ELSE expression]
END
Example:
SELECT au_fname, au_lname,CASE state
WHEN 'OR' THEN 'Oregon'
Trang 22Multi column updates using CASE
be updated using one command in this manner
Trang 27 Understanding the concepts of batch and batch
processing
T-SQL Programming
Define and assign variables
Cursors for data retrieval
Control statements
Write SQL statements using SQL Server basic functions
Use basic functions in a query
Implementing Stored Procedures
Implementing User-Defined Functions
Trang 28Stored 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
Trang 29Creating a Stored Procedure
Trang 30Modifying a Stored Procedure
ALTER PROCEDURE GetSubjects AS
SELECT *
Trang 31Deleting a Stored Procedure
DROP PROCEDURE dbo.GetSubjects
Trang 32Executing a Stored Procedure
Local stored procedures
Trang 33 Understanding the concepts of batch and batch
processing
T-SQL Programming
Define and assign variables
Cursors for data retrieval
Control statements
Write SQL statements using SQL Server basic functions
Use basic functions in a query
Implementing User-Defined Functions
Trang 34 embed complex logic within a query.
create new functions for complex expressions
Three distinct types
Scalar functions that return a single value
Updateable inline table functions similar to views
Multi-statement table functions that build a result set with code
Trang 35Creating a Scalar Function
Trang 36Creating a Inline Table Function
Trang 37Creating a Multi-statement table function
Trang 38Modifying a Scalar Function
Trang 39Modifying a Inline Table Functions
Trang 40Modifying a Multi-statement Table Function
Trang 41Summary