MSSQL示例(四)定义服务器游标VS游标变量
2015-02-28 17:30
155 查看
-- =============================================
-- Author: tomtom
-- Create date: 2015.2.28
-- Description:定义服务器游标VS游标变量
-- =============================================
----1.示例数据准备
create table t1
(
id int
)
go
create table t2
(
name int
)
go
insert into t1
select 1
insert into t2
select 21 union all
select 11
select * from t1
select * from t2
----2.1定义服务器游标DECLARE my_cur CURSOR
--定义时必须加FOR子句了,并且不能作为变量被赋值如set cur_t1 =
declare cur_t1 CURSOR FOR select * from t1
open cur_t1
declare @name1 int
fetch next from cur_t1 into @name1
select @name1
select @name1=-1
fetch next from cur_t1 into @name1 --没有行了,@name1还是上次的值
select @name1
close cur_t1
deallocate cur_t1
----2.2游标变量示例DECLARE @my_cur CURSOR
--重新设置后,不重新打开将提示“游标未打开。”
declare @cur_t2 CURSOR
set @cur_t2 = CURSOR FOR select * from t2
open @cur_t2
declare @name int
fetch next from @cur_t2 into @name
select @name
--re set cursor
set @cur_t2 = CURSOR FOR select * from t1
open @cur_t2
fetch next from @cur_t2 into @name
select @name
close @cur_t2
deallocate @cur_t2
-- Author: tomtom
-- Create date: 2015.2.28
-- Description:定义服务器游标VS游标变量
-- =============================================
----1.示例数据准备
create table t1
(
id int
)
go
create table t2
(
name int
)
go
insert into t1
select 1
insert into t2
select 21 union all
select 11
select * from t1
select * from t2
----2.1定义服务器游标DECLARE my_cur CURSOR
--定义时必须加FOR子句了,并且不能作为变量被赋值如set cur_t1 =
declare cur_t1 CURSOR FOR select * from t1
open cur_t1
declare @name1 int
fetch next from cur_t1 into @name1
select @name1
select @name1=-1
fetch next from cur_t1 into @name1 --没有行了,@name1还是上次的值
select @name1
close cur_t1
deallocate cur_t1
----2.2游标变量示例DECLARE @my_cur CURSOR
--重新设置后,不重新打开将提示“游标未打开。”
declare @cur_t2 CURSOR
set @cur_t2 = CURSOR FOR select * from t2
open @cur_t2
declare @name int
fetch next from @cur_t2 into @name
select @name
--re set cursor
set @cur_t2 = CURSOR FOR select * from t1
open @cur_t2
fetch next from @cur_t2 into @name
select @name
close @cur_t2
deallocate @cur_t2
相关文章推荐
- PHP快速入门教程:服务器和浏览器变量获取示例
- oracle游标中可以定义变量
- 输出所有PHP服务器定义过的所有变量
- 定义变量的位置 @interface VS @implementation
- oracle 定义游标的时候使用变量作为表名
- 定义服务器游标的属性 将远程表上的数据复制到本地
- Go 语言变量的定义、用法、示例
- 说明定义游标cursor时的select语句里可以用变量
- 游标、获取本地本地多个文件、Excel数据导入、跨服务器数据拷贝、行转列示例
- 使用VS Code开发TypeScript--定义变量推荐使用let
- [MSSQL] 如何自己定义函数(返回多变量)
- MSSQL游标的原理及示例
- 游标cursor,游标循环和记录变量的定义
- PL/SQL——编程——变量定义(标量、复合变量、游标变量)
- 服务器预定义变量详解
- PHP快速入门教程:服务器和浏览器变量获取示例
- 服务器的iis、vs、mssql的安装顺序
- PHP快速入门教程:服务器和浏览器变量获取示例
- 【debug】vs2013中定义的全局变量count在使用时提示:“不明确的符号”
- MSSQL 定义变量及条件语句,循环语句,全局变量的使用