村名 行政区域编号 成员流水号 户籍编号 户主姓名 与户主关系 成员姓名 出生年月
A村 520121004020820 01 704040001 司仕兵 户主 司仕兵 1970-9
A村 520121004020820 02 704040001 司仕兵 妻 刘兴菊 1965-9
A村 520121004020820 03 704040001 司仕兵 女 司会 1989-8
A村 520121004020820 04 704040001 司仕兵 女 司胜荣 1995-8
A村 520121004020820 05 704040001 司仕兵 子 司胜涛 1992-4
A村 520121004020821 01 704040002 胡明亮 户主 胡明亮 1971-8
A村 520121004020821 02 704040002 胡明亮 妻 陈菊 1971-4
A村 520121004020821 03 704040002 胡明亮 子 胡凯 1996-11
A村 520121004020821 04 704040002 胡明亮 女 胡馨媛 2000-12
B村 520121004050019 01 704010024 杨春富 户主 杨春富 1951-9
B村 520121004050019 02 704010024 杨春富 妻 何润兰 1952-7
B村 520121004050020 01 704010025 杨毓富 户主 杨毓富 1968-3
B村 520121004050020 02 704010025 杨毓富 妻 吴仕荣 1968-2
B村 520121004050020 03 704010025 杨毓富 子 杨涛 1991-8
B村 520121004050020 04 704010025 杨毓富 子 杨开 1992-10
B村 520121004050020 05 704010025 杨毓富 子 杨毅 1994-9
C村 520121004010177 01 704060007 杨毓伦 户主 杨毓伦 1963-7
C村 520121004010177 02 704060007 杨毓伦 妻 邓石荣 1965-11
C村 520121004010178 01 704060008 曾仁举 户主 曾仁举 1973-7
C村 520121004010178 02 704060008 曾仁举 妻 宿远仙 1986-4在上表中,每户人的行政区域编号、户籍编号、户主姓名这三项是唯一值(但在表中有重复项),成员流水号用来表示该成员在该户中按出生年月的排序(但户主必须为‘1’)。要求2个存储过程实现相关项的更新:
存储过程1实现删除或增加成员时成员流水号自动更新
存储过程2实现整户删除时行[政区域编号]及[户籍编号]自动更新注:各村的[政区域编号]及[户籍编号]规则不同,具体在表中体现了。[政区域编号]的后4位表示该户在该村的索引,[户籍编号]的后3位表示该户在该村的索引。

解决方案 »

  1.   


    看看对你有帮助 没有
    /*--自己做编号的示例:
    根据输入的RoleID,另一个表中查到一个部门ID(三位)
    然后用部门ID作为插入记录流水号的前三位,后面六位为自动增加的。
    --*/--测试环境--部门表
    create table 部门(部门id char(3),部门名称 varchar(10),RoleID int)
    insert 部门 
    select '001','A部门',1
    union all select '002','B部门',2
    union all select '003','c部门',3--A表
    create table A表(编号 char(9) primary key default '',RoleID int)
    go--处理的函数
    create function f_getid(@RoleID int)
    returns char(9)
    as
    begin
    declare @re char(9),@部门id char(3) select @部门id=部门id from 部门 where RoleID=@RoleID
    select @re=max(编号) from A表
    where 编号 like @部门id+'%'
    return(@部门id+case when @re is null then '000001' 
    else right('000000'+cast(cast(right(@re,6) as int)+1 as varchar),6) end)
    end
    go--创建触发器,自动生成编号
    create trigger t_insert on A表
    instead of insert
    as
    declare @部门编号 char(3),@id int,@RoleID int,@编号 char(9)select * into #t from inserted order by RoleIDupdate #t set 
    @编号=case RoleID when @RoleID then @编号 else dbo.f_getid(RoleID) end
    ,@部门编号=case RoleID when @RoleID then @部门编号 else left(@编号,3) end
    ,@id=case RoleID when @RoleID then @id+1 else right(@编号,6) end
    ,编号=@部门编号+right('000000'+cast(@id as varchar),6)
    ,@RoleID=RoleID
    insert into A表 select * from #t
    go--插入数据到A表
    insert A表(RoleID)
    select 1
    union all select 1
    union all select 2
    union all select 3
    union all select 2
    union all select 1
    union all select 2
    union all select 3
    union all select 3
    go--显示处理结果
    select * from A表
    go--删除测试环境
    drop table 部门,A表
    drop function f_getid/*--测试结果编号        RoleID      
    --------- ----------- 
    001000001 1
    001000002 1
    001000003 1
    002000001 2
    002000002 2
    002000003 2
    003000001 3
    003000002 3
    003000003 3(所影响的行数为 9 行)
    --*/
      

  2.   

    参考一下吧:
    第一个:
    --> (让你望见影子的墙)生成测试数据,时间:2009-02-16
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([村名] nvarchar(2),[行政区域编号] nvarchar(20),[成员流水号] int,[户籍编号] nvarchar(10),[户主姓名] nvarchar(3),[与户主关系] nvarchar(2),[成员姓名] nvarchar(3),[出生年月] nvarchar(7))
    Insert tb
    select N'A村',520121004020820,01,704040001,N'司仕兵',N'户主',N'司仕兵',N'1970-9' union all
    select N'A村',520121004020820,02,704040001,N'司仕兵',N'妻',N'刘兴菊',N'1965-9' union all
    select N'A村',520121004020820,03,704040001,N'司仕兵',N'女',N'司会',N'1989-8' union all
    select N'A村',520121004020820,04,704040001,N'司仕兵',N'女',N'司胜荣',N'1995-8' union all
    select N'A村',520121004020820,05,704040001,N'司仕兵',N'子',N'司胜涛',N'1992-4' union all
    select N'A村',520121004020821,01,704040002,N'胡明亮',N'户主',N'胡明亮',N'1971-8' union all
    select N'A村',520121004020821,02,704040002,N'胡明亮',N'妻',N'陈菊',N'1971-4' union all
    select N'A村',520121004020821,03,704040002,N'胡明亮',N'子',N'胡凯',N'1996-11' union all
    select N'A村',520121004020821,04,704040002,N'胡明亮',N'女',N'胡馨媛',N'2000-12' union all
    select N'B村',520121004050019,01,704010024,N'杨春富',N'户主',N'杨春富',N'1951-9' union all
    select N'B村',520121004050019,02,704010024,N'杨春富',N'妻',N'何润兰',N'1952-7' union all
    select N'B村',520121004050020,01,704010025,N'杨毓富',N'户主',N'杨毓富',N'1968-3' union all
    select N'B村',520121004050020,02,704010025,N'杨毓富',N'妻',N'吴仕荣',N'1968-2' union all
    select N'B村',520121004050020,03,704010025,N'杨毓富',N'子',N'杨涛',N'1991-8' union all
    select N'B村',520121004050020,04,704010025,N'杨毓富',N'子',N'杨开',N'1992-10' union all
    select N'B村',520121004050020,05,704010025,N'杨毓富',N'子',N'杨毅',N'1994-9' union all
    select N'C村',520121004010177,01,704060007,N'杨毓伦',N'户主',N'杨毓伦',N'1963-7' union all
    select N'C村',520121004010177,02,704060007,N'杨毓伦',N'妻',N'邓石荣',N'1965-11' union all
    select N'C村',520121004010178,01,704060008,N'曾仁举',N'户主',N'曾仁举',N'1973-7' union all
    select N'C村',520121004010178,02,704060008,N'曾仁举',N'妻',N'宿远仙',N'1986-4'
    Go
    Select * from tb
    if object_id('p_delete_chengyuan')<>0
    drop proc p_delete_chengyuan
    go
    create proc p_delete_chengyuan
    @cunming nvarchar(2),@xingzhengbianhao nvarchar(20),@chengyuanhao nvarchar(2),@huji nvarchar(10)
    as
    delete from tb
    where 村名=@cunming and 行政区域编号=@xingzhengbianhao and 成员流水号=@chengyuanhao and 户籍编号=@hujiupdate tb
    set 成员流水号=(select count(*) from tb  a  where 村名=@cunming and 行政区域编号=@xingzhengbianhao  and 户籍编号=@huji and 成员流水号<=tb.成员流水号)
    from tb p_delete_chengyuan 'A村','520121004020820',2,'704040001'select * from tb
    A村 520121004020820 1 704040001 司仕兵 户主 司仕兵 1970-9
    A村 520121004020820 2 704040001 司仕兵 女 司会 1989-8
    A村 520121004020820 3 704040001 司仕兵 女 司胜荣 1995-8
    A村 520121004020820 4 704040001 司仕兵 子 司胜涛 1992-4
    A村 520121004020821 1 704040002 胡明亮 户主 胡明亮 1971-8
    A村 520121004020821 1 704040002 胡明亮 妻 陈菊 1971-4
    A村 520121004020821 2 704040002 胡明亮 子 胡凯 1996-11
    A村 520121004020821 3 704040002 胡明亮 女 胡馨媛 2000-12
    B村 520121004050019 1 704010024 杨春富 户主 杨春富 1951-9
    B村 520121004050019 1 704010024 杨春富 妻 何润兰 1952-7
    B村 520121004050020 1 704010025 杨毓富 户主 杨毓富 1968-3
    B村 520121004050020 1 704010025 杨毓富 妻 吴仕荣 1968-2
    B村 520121004050020 2 704010025 杨毓富 子 杨涛 1991-8
    B村 520121004050020 3 704010025 杨毓富 子 杨开 1992-10
    B村 520121004050020 4 704010025 杨毓富 子 杨毅 1994-9
    C村 520121004010177 1 704060007 杨毓伦 户主 杨毓伦 1963-7
    C村 520121004010177 1 704060007 杨毓伦 妻 邓石荣 1965-11
    C村 520121004010178 1 704060008 曾仁举 户主 曾仁举 1973-7
    C村 520121004010178 1 704060008 曾仁举 妻 宿远仙 1986-4
    第二个参考着写吧