Home Database Mysql Tutorial Transact-SQL中游标名的作用域_MySQL

Transact-SQL中游标名的作用域_MySQL

Jun 01, 2016 pm 01:56 PM
Keywords cursor

Microsoft SQL Server 2000支持关键字GLOBAL和LOCAL用于DECLARE CURSOR 语句中游标名作用域的定义。GLOBAL指定游标名在一个连接中是全局的。LOCAL指定游标名在存储过程,触发器,包含DECLARE CURSOR的批处理语句中是局部的。


Microsoft SQL Server 7.0版本以前,在一个连接中,游标名都是全局的。你必须先执行一个用于创建游标的存储过程,然后再执行另外一个从该游标中读取记录的存储过程。如:
USE pubs
GO
CREATE PROCEDURE OpenCrsr AS

DECLARE SampleCrsr CURSOR FOR
SELECT au_lname
FROM authors
WHERE au_lname LIKE 'S%'

OPEN SampleCrsr
GO

CREATE PROCEDURE ReadCrsr AS
FETCH NEXT FROM SampleCrsr
WHILE (@@FETCH_STATUS -1)
BEGIN
FETCH NEXT FROM SampleCrsr
END
GO

EXEC OpenCrsr /* 声明并打开游标SampleCrsr. */
GO
EXEC ReadCrsr /* 从游标SampleCrsr中读取记录. */
GO
CLOSE SampleCrsr
GO
DEALLOCATE SampleCrsr
GO

局部游标在存储过程和触发器中对游标起着重要的保护作用。全局游标能在定义它们的存储过程或触发器外被访问。因此,它们可能在不经意的时候,在存储过程和触发器外被改变。局部游标显得更安全,因为它们不会在存储过程和触发器外被改变,除非特地的通过输出游标参数被传递给调用者。
因为全局游标可以在存储过程和触发器以外被引用,所以它们可能对其他语句有不可预期的影响。例如:一个存储过程创建一个全局游标xyz,过程运行结束后游标xyz仍然处于打开状态,如果程序其他部分又想声明一个全局游标并命名为xyz,这时,就会发生一个重复定义的错误。
全局和局部游标具有不同的命名空间,所以,在同一时间可以出现两个具有同样名字的全局和局部游标。Transact-SQL语法支持游标参数,同样也支持使用GLOBAL标识游标的作用域。如果一个游标名同时表示全局游标和局部游标,在没有指定为GLOBAL时,此游标名将引用局部游标。
数据库选项default to local cursor控制由未指定GLOBAL和LOCAL选项的DECLARE CURSOR语句创建的游标的默认作用域。如果default to local cursor 选项为真,则为局部游标,反之为全局。SQL Server 2000中,default to local cursors 选项默认为false,用于保持与先前版本一致。
声明并且打开局部游标的存储过程,可以将这些游标传出给调用它的存储过程,触发器和批处理语句。这可以通过一个定义为输出参数的CURSOR VARYING 数据类型实现。当存储过程执行结束时,游标必须打开,用以通过输出参数返回。我们可以用一个定义为CURSOR类型的局部变量引用它。
USE pubs
GO
/* Create a procedure with a cursor output parameter. */
CREATE PROCEDURE OpenCrsr @OutCrsr CURSOR VARYING OUTPUT AS

SET @OutCrsr = CURSOR FOR
SELECT au_lname
FROM authors
WHERE au_lname LIKE 'S%'

OPEN @OutCrsr
GO

/* 声明局部游标. */
DECLARE @CrsrVar CURSOR

/* 将先前的游标赋给局部变量. */
EXEC OpenCrsr @OutCrsr = @CrsrVar OUTPUT

/* 利用@CrsrVar去读取记录. */
FETCH NEXT FROM @CrsrVar
WHILE (@@FETCH_STATUS -1)
BEGIN
FETCH NEXT FROM @CrsrVar
END

CLOSE @CrsrVar

DEALLOCATE @CrsrVar
GO
数据库API并不支持输出游标参数的存储过程。一个包含输出游标参数的存储过程不能直接被数据库API执行。这些存储过程之可以被其他的存储过程,触发器,Transact-SQL 批处理,脚本调用执行。
全局游标在没有被显式释放或连接关闭前一直有效。局部游标默认将在存储过程,触发器,批处理结束时释放,除非它被作为输出参数输出。那个局部游标也将在引用它的存储工程结束时被释放。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

Recommend the best Windows 11 mouse cursor solution Recommend the best Windows 11 mouse cursor solution Apr 23, 2023 am 09:52 AM

Windows 11 has a seemingly unlimited amount of customization options, from default settings to every third-party app on the Internet. There are even apps that can change the appearance of your mouse cursor. Modifying the cursor is a great way to give your computer a unique look. You don't have to stick the same boring black and white pointer on every computer. But even so, you don't have to download software to change the look of your cursor. How to change the appearance of the cursor? Windows 11 offers a small amount of customization for the cursor. You can change the cursor by going into Control Panel and selecting Mouse Options there. A new window called "Mouse Properties" will appear. In the mouse properties you can change the color scheme, size and design. Your computer will naturally

Is go a keyword in C language? Detailed analysis Is go a keyword in C language? Detailed analysis Mar 16, 2024 am 10:30 AM

Title: Is go a keyword in C language? Detailed analysis In C language, "go" is not a keyword. Keywords in C language are specified by the C standard and are used to represent specific grammatical structures or functions. They have special meanings in the compiler and cannot be used as identifiers or variable names. For example, the keyword "int" represents an integer data type, "if" represents a conditional statement, and so on. If we want to verify whether "go" is a keyword in C language, we can write a simple program to test it. Here is an example: #inc

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

How many keywords are there in c language? How many keywords are there in c language? Nov 22, 2022 pm 03:39 PM

There are 32 keywords in C language. According to the function of keywords, they can be divided into four categories: data type keywords, control statement keywords, storage type keywords and other keywords. There are 12 data type keywords, including char, double, float, int, etc.; there are 12 control statement keywords, including for, break, if, else, do, etc.; there are 4 storage type keywords, including auto, static , extern, etc.; there are 4 other keywords, including const, sizeof, etc.

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

Complete list of go language keywords Complete list of go language keywords Apr 07, 2024 pm 02:15 PM

The keywords of Go language are: basic keywords: const, func, type, var, if, else, for, return Data type related keywords: bool, string, int, float64, interface{}, map, slice other keywords :break, continue, defer, go, select, range

Detailed explanation of the role and usage of the extends keyword in PHP Detailed explanation of the role and usage of the extends keyword in PHP Jun 28, 2023 pm 08:04 PM

Detailed explanation of the role and usage of the extends keyword in PHP In PHP programming, extends is a very important keyword, which is used to implement class inheritance. Through the extends keyword, we can create a new class that can inherit the properties and methods of one or more existing classes. Inheritance is an important concept in object-oriented programming, which makes code reuse and extension more convenient and flexible. This article will introduce in detail the function and use of the extends keyword. extends

See all articles