Chapter 9 Stored Procedure

9.1_ Use of cursor

9.1.1_ Cursor introduction and usage process

Use the cursor (CURSOR) in the need to process line by line, the cursor is very useful. The cursor can open a result set (rows selected according to the specified criteria) and provide the function of processing row by row in the result set . Based on the type of cursor, it can be rolled back or forward.

Use Process:
① defined
② reading result data set by the Fetch
③ open cursors

 open cursor_name

④Close the cursor

close cursor_name

9.1.2_ declaration of cursor

Use the DECLARE statement to declare a cursor. There are two ways to specify a cursor.
SQL-92 syntax:

DECLARE cursor_name [ INSENSITIVE ] [ SCROLL ] CURSOR 
FOR select_statement 
[ FOR { READ ONLY | UPDATE [ OF column_name [ ,...n ] ] } ]
--举例:
DECLARE student_cursor CURSOR FOR SELECT sno,sname FROM student

Transact-SQL extended syntax:

DECLARE cursor_name CURSOR 
[ LOCAL | GLOBAL ] 
[ FORWARD_ONLY | SCROLL ] 
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] 
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] 
[ TYPE_WARNING ] 
FOR select_statement 
[ FOR UPDATE [ OF column_name [ ,...n ] ] ] 

9.1.3_ Use cursor to read data

In the process of reading data from the cursor, you can move and process back and forth on each row in the result set.
If the cursor definition becomes scrollable (using in the statement SCROLL keyword), then at any time may remove any row in the result set .
For non-scrolling cursors, only the next row of the current row can be fetched . The result set can be taken into local variables. The syntax of the Fetch command is as follows:

FETCH [NEXT | PRIOR| FIRST | LAST | ABSOLUTE {n | @nvar} | RELATIVE {n | @nvar}]
FROM [GLOBAL] cursor_name} | cursor_variable_name}
[INTO @variable_name ][,……n]]

When @@fetch_status! = 0, the end of reading

--举例
FETCH NEXT FROM student_cursor INTO @ID, @Name

9.1.4_ Examples

Routine 9.3: Define a cursor, return all the data in the student information table, open the cursor, and then traverse the student information table until the record with the student name "Zhang Sanfeng" is found, and the student ID and student name are printed.

--定义游标
DECLARE student_cursor CURSOR
FOR SELECT sno,sname FROM student
--定义变量
DECLARE @ID char(10), @Name char(30)
--打开游标
OPEN student_cursor
--指向集合的下一项
FETCH NEXT FROM student_cursor INTO @ID, @Name
--进入whlie循环当全局变量@@fetch_status = 0时进入循环
WHILE @@fetch_status = 0
BEGIN
IF @Name = '张三丰'
BEGIN
			PRINT '找到张三丰'
			PRINT @ID+@Name
			BREAK
END
--指向集合中下一项
FETCH NEXT FROM student_cursor INTO @ID, @Name
END 

9.2_ stored procedure

9.2.1_ Introduction to stored procedures

There are two main types of procedural SQL blocks , namely named blocks and anonymous blocks . The previous introduction was anonymous blocks. The anonymous block must be compiled every time it is executed , it cannot be stored in the database, nor can it be called from other procedural SQL blocks. Stored procedures and functions are named blocks . They are compiled and saved in the database, called persistent storage modules , which can be called repeatedly and run faster.
(In simple terms, the stored procedure can be called like a function, and this function can be stored in the database, which can be used directly in the future)

Stored procedures are divided into two categories: system stored procedures and user-defined stored procedures;

9.2.2_ stored procedure definition and execution

Stored procedure without output parameters:

USE  students--在student数据库上建立存储过程
GO
CREATE PROCEDURE procedure_name @xbbh varchar(4)--procedure_name 存储过程名,@xbbh varchar(4)传入参数及参数类型
AS
begin
--sql过程块
end
--上述中procedure可以简写为proc

Stored procedure with output parameters:

USE  students--在student数据库上建立存储过程
GO
CTEATE PROCEDURE proc_testOutput   
    ( 
    @p1 int , 
    @p2 int OUTPUT, --输出参数
    @p3 int  
    )    
AS 
BEGIN
--sql过程块
END

Execute a defined stored procedure syntax format through the EXEC command of the Transact-SQL statement:

    USE students
    GO
    EXEC proc_name
    GO

9.2.3_ Rewrite the stored procedure

USE  students--在student数据库上建立存储过程
GO
--把create改为alter即可
alter PROCEDURE procedure_name @xbbh varchar(4)--procedure_name 存储过程名,@xbbh varchar(4)传入参数及参数类型
AS
begin
--sql过程块
end

9.2.6_ Delete stored procedure

USE students
GO
DROP PROCEDURE proc_scorepass

9.2.5_ Examples

Supplement: #name is a temporary collection or temporary stored procedure

Count the top high schools with the largest number of new students (the number is given when querying): (using a stored procedure)

--定义游标
declare
find_cursor cursor for 
select middle_school from Student group by middle_school order by COUNT(*) desc
--存储过程
use test
go
create procedure find_procedure @number int
as
begin
--定义变量
declare @count int = 0
declare @school char(50)
--打开游标
open find_cursor
--读取游标所指集合的下一项内容
fetch next from find_cursor into @school
while @count < @number
begin
set @count = @count+1
print @count
print @number
print @school
fetch next from find_cursor into @school
end
--关闭游标
close find_cursor
end
--调用存储过程
exec find_procedure 5

Guess you like

Origin blog.csdn.net/qq_43907296/article/details/110481551