根据一定规则为数据表新增记录自动编号的存储过程
2009-12-17 07:26
387 查看
CREATE PROCEDURE dbo.AutoGenCustomerSN
AS
--自动生成单据客户编号
declare @客户ID int,@类别编号 varchar(30),@客户编号 varchar(30),@客户名称 varchar(50),@客户简称 varchar(30),@所属市县 varchar(50)
declare @WFNum integer, @BH varchar(20),@count int
--先清除所属市县与编号不匹配的客户的编号
update 客户资料表 set 客户编号='' where substring(所属市县,1,6)<> substring(客户编号,1,6)
declare Master_cursor cursor for
SELECT distinct 所属市县 from 客户资料表 order by 所属市县
open Master_cursor
fetch next from Master_cursor into @所属市县
while (@@fetch_status = 0)
begin
--select @count=convert(int ,substring(max(客户编号) ,7,3) ) from 客户资料表 where 所属市县=@所属市县 and isnull(客户编号,'')<>''
-- select @count=count(*) from 客户资料表 where 所属市县=@所属市县 and isnull(客户编号,'')<>''
--下述算法正常工作的前提是, 客户编号必须是6位编号加3位流水号, 而且6位编号必须是所属市县的前六位, 切记, 切记
select @count=convert(int ,substring(max(客户编号) ,7,3) ) from 客户资料表 where 所属市县=@所属市县 and 客户编号 like substring(@所属市县,1,6)+'%'
select @WFNum=isnull(@count,0)+100001
declare detail_cursor cursor for
SELECT 客户ID,类别编号,客户编号,客户名称,客户简称
from 客户资料表 where isnull(客户编号,'')='' and 所属市县=@所属市县 order by 客户名称
open detail_cursor
fetch next from detail_cursor into @客户ID,@类别编号,@客户编号,@客户名称,@客户简称
while (@@fetch_status = 0)
begin
select @BH=substring(@所属市县,1,6)+Substring(Convert(varchar(6),@WFNum),4,3)
update 客户资料表 set 客户编号=@BH where 客户ID=@客户ID
select @WFNum=@WFNum+1
fetch next from detail_cursor into @客户ID,@类别编号,@客户编号,@客户名称,@客户简称
end
close detail_cursor
deallocate detail_cursor
fetch next from Master_cursor into @所属市县
end
close Master_cursor
deallocate Master_cursor
GO
上面的存储过程在触发器中调用即可.
AS
--自动生成单据客户编号
declare @客户ID int,@类别编号 varchar(30),@客户编号 varchar(30),@客户名称 varchar(50),@客户简称 varchar(30),@所属市县 varchar(50)
declare @WFNum integer, @BH varchar(20),@count int
--先清除所属市县与编号不匹配的客户的编号
update 客户资料表 set 客户编号='' where substring(所属市县,1,6)<> substring(客户编号,1,6)
declare Master_cursor cursor for
SELECT distinct 所属市县 from 客户资料表 order by 所属市县
open Master_cursor
fetch next from Master_cursor into @所属市县
while (@@fetch_status = 0)
begin
--select @count=convert(int ,substring(max(客户编号) ,7,3) ) from 客户资料表 where 所属市县=@所属市县 and isnull(客户编号,'')<>''
-- select @count=count(*) from 客户资料表 where 所属市县=@所属市县 and isnull(客户编号,'')<>''
--下述算法正常工作的前提是, 客户编号必须是6位编号加3位流水号, 而且6位编号必须是所属市县的前六位, 切记, 切记
select @count=convert(int ,substring(max(客户编号) ,7,3) ) from 客户资料表 where 所属市县=@所属市县 and 客户编号 like substring(@所属市县,1,6)+'%'
select @WFNum=isnull(@count,0)+100001
declare detail_cursor cursor for
SELECT 客户ID,类别编号,客户编号,客户名称,客户简称
from 客户资料表 where isnull(客户编号,'')='' and 所属市县=@所属市县 order by 客户名称
open detail_cursor
fetch next from detail_cursor into @客户ID,@类别编号,@客户编号,@客户名称,@客户简称
while (@@fetch_status = 0)
begin
select @BH=substring(@所属市县,1,6)+Substring(Convert(varchar(6),@WFNum),4,3)
update 客户资料表 set 客户编号=@BH where 客户ID=@客户ID
select @WFNum=@WFNum+1
fetch next from detail_cursor into @客户ID,@类别编号,@客户编号,@客户名称,@客户简称
end
close detail_cursor
deallocate detail_cursor
fetch next from Master_cursor into @所属市县
end
close Master_cursor
deallocate Master_cursor
GO
上面的存储过程在触发器中调用即可.
相关文章推荐
- C#运用存储过程新增一条记录并返回自动生成的ID
- C#运用存储过程新增一条记录并返回自动生成的ID
- 根据表中数据生成insert语句的存储过程
- Oracle根据存储过程的业务逻辑造数据,是的存储过程中的QUERY逻辑有结果
- sql根据传入参数确定选择前几条数据的存储过程
- oracle一卡通消费数据分区表建立及自动建立分区存储过程
- 存储过程 删除两表关联的数据记录
- 在SQL Server 2000中insert的时候返回自动编号的id 存储过程示例[原]
- 配置导出MOSS2010列表数据到Excel并根据列表记录自动刷新数据
- 将表中的数据自动生成INSERT语句的存储过程,自己收藏一下
- [导入]创建一个存储过程,根据系部编号查出男生、女生人数
- 好久没来了。共享一个自动生成编号的存储过程
- php根据数据id自动生成编号的实现方法
- 存储过程中得到新增数据的ID
- 自动生成表的更新数据的存储过程
- .Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete【原创】
- MSSQL通过存储过程自动创建自定义数据表
- 自动生成订单编号存储过程
- 记录一个SQL Server 2005的存储过程,循环修改数据例子
- (转)根据表中数据生成insert语句的存储过程(另一种方法)