1. Trang chủ
  2. » Công Nghệ Thông Tin

SQL Server - Bài 6

42 490 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Stored procedures & user-defined functions
Tác giả Vu Tuyet Trinh
Trường học Hanoi University of Technology
Thể loại bài
Thành phố Hanoi
Định dạng
Số trang 42
Dung lượng 640,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

SQL Server - Bài

Trang 1

Stored Procedures &

Trang 2

Introduction to SQL Batch Processing

Batch

Individual SQL

commands

Grouped to form a batch

Compiled

as single execution plan

Trang 3

Use 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 5

Assigning values to variables

 SET statement

SET @local_variable name = value

 SELECT statement

SELECT @local_variable name = value

SELECT CUSTOMERID,COMPANYNAME FROM CUSTOMERS

Trang 6

SQL Server supports two types of variables in T-SQL

@@global_variable

@local_variable

Trang 7

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

Trang 8

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

Trang 9

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

Trang 10

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

Trang 11

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

Trang 12

Control Statements

Trang 13

Control Statements Contd…

Trang 15

We can execute different

sets of SQL statements based on

Trang 16

Example

Trang 17

WHILE construct

We can execute a SQL statement or a

block of statements based on some condition

Trang 18

SET price = price * 2

SELECT MAX(price) FROM titles

IF (SELECT MAX(price) FROM titles) > $50

Trang 19

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

Trang 20

RETURN: 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 21

WHEN expression1 THEN expression1

[[WHEN expression2 THEN expression2] […]]

[ELSE expression]

END

 Example:

SELECT au_fname, au_lname,CASE state

WHEN 'OR' THEN 'Oregon'

Trang 22

Multi 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 28

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

Trang 29

Creating a Stored Procedure

Trang 30

Modifying a Stored Procedure

ALTER PROCEDURE GetSubjects AS

SELECT *

Trang 31

Deleting a Stored Procedure

DROP PROCEDURE dbo.GetSubjects

Trang 32

Executing 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 35

Creating a Scalar Function

Trang 36

Creating a Inline Table Function

Trang 37

Creating a Multi-statement table function

Trang 38

Modifying a Scalar Function

Trang 39

Modifying a Inline Table Functions

Trang 40

Modifying a Multi-statement Table Function

Trang 41

Summary

Ngày đăng: 15/11/2012, 10:59

TỪ KHÓA LIÊN QUAN