Tải bản đầy đủ (.ppt) (42 trang)

SQL Server - Bài 6

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (631.96 KB, 42 trang )

Stored Procedures &
User-Defined Functions
Vu Tuyet Trinh

Hanoi University of Technology
1
MicrosoftMicrosoft
Introduction to SQL Batch Processing
Batch
Individual SQL
commands
Grouped to form a
batch
Compiled
as single
execution
plan
MicrosoftMicrosoft
Example
Use Pubs
Select * from authors
Update authors
set phone= '890 451-7366‘
where au_lname= 'White'
Go
MicrosoftMicrosoft
Outline

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
4
MicrosoftMicrosoft
Assigning values to variables

SET statement
SET @local_variable name = value


SELECT statement
SELECT @local_variable name = value

Example
SET @CUST=’FRANK’
SELECT CUSTOMERID,COMPANYNAME
FROM CUSTOMERS

WHERE CUSTOMERID = @CUST
MicrosoftMicrosoft
Variables
SQL Server supports two types of variables in T-SQL
@@global_variable
@local_variable
MicrosoftMicrosoft
List 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
MicrosoftMicrosoft
List 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
@@VERSION Version information of SQL Server
MicrosoftMicrosoft
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
MicrosoftMicrosoft
Cursor 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
MicrosoftMicrosoft
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
MicrosoftMicrosoft
Control Statements
MicrosoftMicrosoft
Control Statements Contd…
MicrosoftMicrosoft
BEGIN..END
A set of TSQL statements to be executed can be enclosed in

BEGIN..END.
Syntax:
BEGIN
{ statement | statement_block}
END
MicrosoftMicrosoft
IF..ELSE
We can execute different
sets of SQL statements based on
specified conditions.
Syntax:
IF Boolean_expression

{ sql_statement |
statement_block }
[ ELSE
{ sql_statement |
statement_block } ]
MicrosoftMicrosoft
Example

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×