您的位置:首页 > 其它

游标使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐