游标与临时表
2014-04-28 16:56
106 查看
游标一般格式:
DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段3,... FROM 表名 WHERE ...
OPEN 游标名称
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
WHILE @@FETCH_STATUS=0
BEGIN
SQL语句执行过程... ...
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
END
CLOSE 游标名称
DEALLOCATE 游标名称 (删除游标)
--drop proc Execproc
create proc Execproc
@timeint int
as
set @timeint=-@timeint
DECLARE @ID UNIQUEIDENTIFIER
begin
DECLARE Execproc_Cursor CURSOR FOR select TOP 1 ID from tUnitItem where StatusCODE='FAIL'
AND DateTime>DATEADD(HH,@timeint,GETDATE())
OPEN Execproc_Cursor
FETCH NEXT FROM Execproc_Cursor into @ID
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE tUnitItem SET SerialNumber=SerialNumber+'_Lock' where ID=@ID AND
charindex('_Lock',SerialNumber)< 1
FETCH NEXT FROM Execproc_Cursor into @ID
END
CLOSE Execproc_Cursor
deallocate Execproc_Cursor
end
exec Execproc 1128
------------------------------------------------------------------------------
/*
功能:数据库表格
tbl_users
数据
deptid userid username
1
100
a
1
101
b
2
102
c
要求用一个
sql
语句输出下面结果
deptid username
1
ab
2
c
*/
create
table
#Temp1
(
deptid
int
,
userid
int
,
username
varchar
(
20
))
--
待测
试的数据表
create
table
#Temp2
(
deptid
int
,
username
varchar
(
20
))
--
结果表
--
先把一些待测试的数据插入到待测试表
#Temp1
中
insert
into
#Temp1
select
1
,
100
,
'a'
union
all
select
1
,
101
,
'b'
union
all
select
1
,
131
,
'd'
union
all
select
1
,
201
,
'f'
union
all
select
2
,
302
,
'c'
union
all
select
2
,
202
,
'a'
union
all
select
2
,
221
,
'e'
union
all
select
3
,
102
,
'y'
union
all
select
3
,
302
,
'e'
union
all
select
3
,
121
,
't'
--
declare
@deptid
int
,
@username
varchar
(
20
)
--
定义游标
declare
Select_cursor
cursor
for
select
deptid
,
username
from
#Temp1
open
Select_cursor
fetch
next
from
Select_cursor
into
@deptid
,
@username
--
提取操作的列数
据放到局部变量中
while
@@fetch_status
=
0
--
返回被
FETCH
语句执行的最后游标的状态
/*
@@FETCH_STATUS =0
FETCH
语句成功
@@FETCH_STATUS =-1 FETCH
语句失败或此行不在结果集中
@@FETCH_STATUS =-2
被提取的行不存在
*/
begin
--
当表
#Temp2
列
deptid
存在相同的数据时,就直接在列
username
上追
加
@username
值
if
(exists(
select
*
from
#Temp2
where
deptid
=
@deptid
))
update
#Temp2
set
username
=
username
+
@username
where
deptid
=
@deptid
else
--
插入新数据
insert
into
#Temp2
select
@deptid
,
@username
fetch
next
from
Select_cursor
into
@deptid
,
@username
end
close
Select_cursor
deallocate
Select_cursor
select
*
from
#Temp2
--
测试结果
Drop
table
#Temp1
,
#Temp2
SELECT TOP 1 * FROM tUnitItem WHERE charindex('_Lock',SerialNumber)>0
DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段3,... FROM 表名 WHERE ...
OPEN 游标名称
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
WHILE @@FETCH_STATUS=0
BEGIN
SQL语句执行过程... ...
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
END
CLOSE 游标名称
DEALLOCATE 游标名称 (删除游标)
--drop proc Execproc
create proc Execproc
@timeint int
as
set @timeint=-@timeint
DECLARE @ID UNIQUEIDENTIFIER
begin
DECLARE Execproc_Cursor CURSOR FOR select TOP 1 ID from tUnitItem where StatusCODE='FAIL'
AND DateTime>DATEADD(HH,@timeint,GETDATE())
OPEN Execproc_Cursor
FETCH NEXT FROM Execproc_Cursor into @ID
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE tUnitItem SET SerialNumber=SerialNumber+'_Lock' where ID=@ID AND
charindex('_Lock',SerialNumber)< 1
FETCH NEXT FROM Execproc_Cursor into @ID
END
CLOSE Execproc_Cursor
deallocate Execproc_Cursor
end
exec Execproc 1128
------------------------------------------------------------------------------
/*
功能:数据库表格
tbl_users
数据
deptid userid username
1
100
a
1
101
b
2
102
c
要求用一个
sql
语句输出下面结果
deptid username
1
ab
2
c
*/
create
table
#Temp1
(
deptid
int
,
userid
int
,
username
varchar
(
20
))
--
待测
试的数据表
create
table
#Temp2
(
deptid
int
,
username
varchar
(
20
))
--
结果表
--
先把一些待测试的数据插入到待测试表
#Temp1
中
insert
into
#Temp1
select
1
,
100
,
'a'
union
all
select
1
,
101
,
'b'
union
all
select
1
,
131
,
'd'
union
all
select
1
,
201
,
'f'
union
all
select
2
,
302
,
'c'
union
all
select
2
,
202
,
'a'
union
all
select
2
,
221
,
'e'
union
all
select
3
,
102
,
'y'
union
all
select
3
,
302
,
'e'
union
all
select
3
,
121
,
't'
--
declare
@deptid
int
,
@username
varchar
(
20
)
--
定义游标
declare
Select_cursor
cursor
for
select
deptid
,
username
from
#Temp1
open
Select_cursor
fetch
next
from
Select_cursor
into
@deptid
,
@username
--
提取操作的列数
据放到局部变量中
while
@@fetch_status
=
0
--
返回被
FETCH
语句执行的最后游标的状态
/*
@@FETCH_STATUS =0
FETCH
语句成功
@@FETCH_STATUS =-1 FETCH
语句失败或此行不在结果集中
@@FETCH_STATUS =-2
被提取的行不存在
*/
begin
--
当表
#Temp2
列
deptid
存在相同的数据时,就直接在列
username
上追
加
@username
值
if
(exists(
select
*
from
#Temp2
where
deptid
=
@deptid
))
update
#Temp2
set
username
=
username
+
@username
where
deptid
=
@deptid
else
--
插入新数据
insert
into
#Temp2
select
@deptid
,
@username
fetch
next
from
Select_cursor
into
@deptid
,
@username
end
close
Select_cursor
deallocate
Select_cursor
select
*
from
#Temp2
--
测试结果
Drop
table
#Temp1
,
#Temp2
SELECT TOP 1 * FROM tUnitItem WHERE charindex('_Lock',SerialNumber)>0
相关文章推荐
- SQL表变量与临时表区别 + 临时表遍历(非游标)
- SQL大全(查询分析器快捷鍵,Sysproperties系统表,syscolumns系统表,union,表结构修改,临时表,加一个自动增长列,函数,游标,关联其它表修改,直接调用存储过程,T远程连接专用sql)
- sql server 语句 循环 、游标、临时表
- 临时表和游标区别
- MS Sql Server临时表和游标的使用小总结
- oracle之存储过程,临时表,游标示例
- mysql游标和临时表
- 一个蛮复杂的存储过程,游标,循环,临时表
- 临时表和游标,选择哪个?
- 海洋工作室——网站建设专家:MS Sql Server临时表和游标的使用小总结【转】
- 使用临时表替换游标:提高效率
- Mysql存储过程优化——使用临时表代替游标
- 用临时表代替游标实现多条数据的动态更新
- sql 存储过程 临时表和游标的使用
- SQLSERVER存储过程不用游标循环临时表结果集
- SQL 临时表或表变量替代游标
- 这么多年第一次自己去用游标和临时表
- 游标的结果集放入临时表
- 存储过程,游标,循环,临时表
- MS Sql Server临时表和游标的使用小总结