Sqlserver存储过程及游标的使用
程序员文章站
2022-03-04 20:30:52
...
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
drop PROCEDURE remove_repeated_msisdn
go
-- =============================================
-- 处理重复号
-- =============================================
CREATE PROCEDURE [dcms_zhq].remove_repeated_msisdn
AS
BEGIN
declare @num varchar(30)
declare @count bigint
declare @id bigint
declare @big_box varchar(30)
declare @little_box varchar(30)
declare @zha varchar(30)
declare @status bigint
declare c1 cursor local for
select msisdn_,count(id_) amount_ from dcms_zhq.SIM_CARD_RESOURCE_
where is_locked_='N' and unit_type_id_=400 and msisdn_='15119880662'
group by msisdn_
having count(id_)>1
open c1
fetch next from c1 into @num,@count
while (@@fetch_status=0)
begin
--#####################
declare c2 cursor local for
select id_,big_box_id_,little_box_id_,zha_id_,status_
from dcms_zhq.SIM_CARD_RESOURCE_ where [email protected] order by input_date_time_ asc
declare @count2 bigint
set @count2 = 0
open c2
fetch next from c2 into @id,@big_box,@little_box,@zha,@status
while (@@fetch_status=0)
begin
set @count2 = @count2 + 1
if @count2<@count
begin
BEGIN TRANSACTION
update dcms_zhq.SIM_CARD_RESOURCE_ set
packer_=packer_+'_20100420_'+ltrim(rtrim(str(@count2))),
msisdn_=msisdn_+'_20100420_'+ltrim(rtrim(str(@count2))),
life_status_=102
where [email protected]
if @status=2 or @status=16
begin
if(len(@zha)>0) update dcms_zhq.SIM_CARD_RESOURCE_ set amount_=amount_-1 where [email protected]
if(len(@little_box)>0) update dcms_zhq.SIM_CARD_RESOURCE_ set amount_=amount_-1 where [email protected]_box
if(len(@big_box)>0) update dcms_zhq.SIM_CARD_RESOURCE_ set amount_=amount_-1 where [email protected]_box
end
IF @@ERROR>0
ROLLBACK TRANSACTION
else
COMMIT TRANSACTION
end
fetch next from c2 into @id,@big_box,@little_box,@zha,@status
end
close c2
deallocate c2
--#####################
fetch next from c1 into @num,@count
end
close c1
deallocate c1
END
GO
上一篇: 数据库系统概念课程笔记
下一篇: vscode+PyQt5安装详解步骤
推荐阅读
-
sqlserver 存储过程中的top+变量使用分析(downmoon)
-
C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息示例
-
navicat中创建存储过程、触发器和使用游标的简单实例(图文)
-
一些SQLServer存储过程参数及举例
-
sqlserver数据库使用存储过程和dbmail实现定时发送邮件
-
使用sqlserver存储过程sp_send_dbmail发送邮件配置方法(图文)
-
C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息示例
-
navicat中创建存储过程、触发器和使用游标的简单实例(图文)
-
sqlserver数据库使用存储过程和dbmail实现定时发送邮件
-
SQLSERVER查询锁表,阻塞,使用表名查询存储过程,行数,表字段类型等常规查询实例讲解