存储过程建立临时表,通过游标增加数据
2009-12-11 17:16
309 查看
if exists(select * from sysobjects where name='pro_table')
drop proc pro_table --判断存储过程是否存在,如存在则删除
go
create proc pro_table ---创建存储过程
@sid int
as
begin
declare @sql nvarchar(200)
if(object_id('temptable') is not null)----创建临时表
begin
drop table temptable
end
create table temptable(sid int ,sname nvarchar(200),descriptions nvarchar(200))
---通过游标读取数据并将数据存入临时表中
declare tabl_cursor cursor for
select top 50 id,name from stest where sid=@sid ---声明游标
open tabl_cursor ----打开游标
declare @id int,@name varchar(200),@des nvarchar(200)
fetch next from tabl_cursor into @id,@name ----循环游标
while (@@fetch_status=0) ---判断游标是否到了尾端
begin
insert into temptable(sid,sname,descriptions)values(@id,substring(@name,1,20),convert(nvarchar(20),@id)+@name) ---将数据提交到临时表中
fetch next from tabl_cursor into @id,@name ----循环游标
end
close tabl_cursor ---关闭游标
deallocate tabl_cursor ---销毁游标
set @sql=N'select * from temptable'
exec sp_executesql @sql --执行查询临时表中的数据
drop table temptable --销毁临时表
end
GO
exec pro_table 2 --执行存储过程
drop proc pro_table --判断存储过程是否存在,如存在则删除
go
create proc pro_table ---创建存储过程
@sid int
as
begin
declare @sql nvarchar(200)
if(object_id('temptable') is not null)----创建临时表
begin
drop table temptable
end
create table temptable(sid int ,sname nvarchar(200),descriptions nvarchar(200))
---通过游标读取数据并将数据存入临时表中
declare tabl_cursor cursor for
select top 50 id,name from stest where sid=@sid ---声明游标
open tabl_cursor ----打开游标
declare @id int,@name varchar(200),@des nvarchar(200)
fetch next from tabl_cursor into @id,@name ----循环游标
while (@@fetch_status=0) ---判断游标是否到了尾端
begin
insert into temptable(sid,sname,descriptions)values(@id,substring(@name,1,20),convert(nvarchar(20),@id)+@name) ---将数据提交到临时表中
fetch next from tabl_cursor into @id,@name ----循环游标
end
close tabl_cursor ---关闭游标
deallocate tabl_cursor ---销毁游标
set @sql=N'select * from temptable'
exec sp_executesql @sql --执行查询临时表中的数据
drop table temptable --销毁临时表
end
GO
exec pro_table 2 --执行存储过程
相关文章推荐
- Java调用oracle存储过程通过游标返回临时表数据
- Java调用oracle存储过程通过游标返回临时表数据
- Java调用oracle存储过程通过游标返回临时表数据
- Mysql存储过程中临时表的建立及游标遍历
- proc 存储过程,通过存储过程往数据库的某个表中插入指定数据量的纪录,表中有primary key
- 通过存储过程的方式生成数据脚本
- c++通过存储过程访问数据库库(ado)
- 通过游标在SQL存储过程中循环
- [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器
- 通过存储过程建立灵活的SQL计划任务
- sql 脚本 向临时表中插入数据, 存储过程结果集插入到临时表中
- 给数据增加测试数据的存储过程
- MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程
- SQL的存储过程、动态语句、临时表、游标的综合应用
- 通过存储过程来实现删除所有表的数据(清库)
- mysql存储过程之游标遍历数据表
- C#中读取存储过程中临时表中的数据的写法
- 通过名字查找数据库名的通用存储过程
- 利用存储过程把两个表的数据插入到一个新表中(存储过程中写两个游标)
- spring jdbctemplate或jdbc调用返回游标或复杂数据类型的存储过程