游标使用demo(循环,条件判断,自增长字段)
2010-11-23 09:35
316 查看
--数据
create proc ut_insert_data
as
begin tran
--声明临时表,
create table #temp
(
id int identity(1,1),--自动ID,仅作为循环使用
customer nvarchar(50)--存储mzzb的原来的ID
)
declare @cus nvarchar(50) --取得当前的要操作使用的mzzb的ID(原)
declare @n int --
declare @rows int
select @n=1
--查找要恢复的数据(包含有12-15号的数据)
insert #temp(customer) select id from orgsql.dbo.mz_sfzb b where b.sfsj between '2010-08-12 00:00:00.000' and '2010-08-15 23:59:59.999'
select @rows = @@rowcount
print '要恢复的数据个数:'+str(@rows)
while @n <= @rows
begin
select @cus=customer from #temp where id = @n
-- 在真实库中插入主表信息org是真实库的数据库名
-- orgsql为包含12-15号数据的数据库名
insert into org.dbo.mz_sfzb(a1,a2)
select b.b1,b.b2
from orgsql.dbo.mz_sfzb b where b.id=@cus
declare @Zb_ID int
Set @Zb_ID= @@IDENTITY
--在真实库中插入明细
insert into org.dbo.mz_sfmx(a1,a2)
select @Zb_ID,b.b1,b.bz
from orgsql.dbo.mz_sfmx b where b.zb_id=@cus
--移动当前@n
select @n = @n + 1
end
--输出当前id
print '---还原的数据个数:'+str(@n-1)
if @@error > 0
begin
if @@trancount>0
rollback tran
end
else
begin
if @@trancount>0
commit tran
end
create proc ut_insert_data
as
begin tran
--声明临时表,
create table #temp
(
id int identity(1,1),--自动ID,仅作为循环使用
customer nvarchar(50)--存储mzzb的原来的ID
)
declare @cus nvarchar(50) --取得当前的要操作使用的mzzb的ID(原)
declare @n int --
declare @rows int
select @n=1
--查找要恢复的数据(包含有12-15号的数据)
insert #temp(customer) select id from orgsql.dbo.mz_sfzb b where b.sfsj between '2010-08-12 00:00:00.000' and '2010-08-15 23:59:59.999'
select @rows = @@rowcount
print '要恢复的数据个数:'+str(@rows)
while @n <= @rows
begin
select @cus=customer from #temp where id = @n
-- 在真实库中插入主表信息org是真实库的数据库名
-- orgsql为包含12-15号数据的数据库名
insert into org.dbo.mz_sfzb(a1,a2)
select b.b1,b.b2
from orgsql.dbo.mz_sfzb b where b.id=@cus
declare @Zb_ID int
Set @Zb_ID= @@IDENTITY
--在真实库中插入明细
insert into org.dbo.mz_sfmx(a1,a2)
select @Zb_ID,b.b1,b.bz
from orgsql.dbo.mz_sfmx b where b.zb_id=@cus
--移动当前@n
select @n = @n + 1
end
--输出当前id
print '---还原的数据个数:'+str(@n-1)
if @@error > 0
begin
if @@trancount>0
rollback tran
end
else
begin
if @@trancount>0
commit tran
end
相关文章推荐
- [置顶] 实现1+2+3...+n,不能使用*、/、循环、条件判断、选择相关的关键字
- 【剑指offer】实现 1+2+3+...+n,要求不能使用乘除法,循环,条件判断,选择相关的关键字
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字
- mysql 存储过程示例,有参有返回值,使用到了判断,循环,游标,临时表,事务
- python web框架Django学习(五)模板中的循环,条件判断,常用的标签,过滤器的使用
- Oracle 存储过程中的循环,条件判断,存储过程,游标小结
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字(for、while、if、else、switch、case)。
- day01之合并两个有序链表+实现1+2+3...+n要求不能使用乘除法循环条件判断等
- ifthen宏包使用——条件判断与循环语句
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- Ansible管理任务计划、安装rpm包管理服务、 playbook的使用循环条件判断handles
- 全面掌握Java中的循环控制语句与条件判断语句的使用
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字。
- 无符号数(unsigned) 和带符号数(int)的循环变量 在for循环条件判断使用中的区别1例
- c语音中for循环使用两个判断条件的写法
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字。
- Django学习____模板中的循环,条件判断,常用的标签,过滤器的使用
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字
- 合并有序链表,实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字。
- 9-13 循环语句、条件判断的使用方法及其相关示例