A表中有一个ID字段并且为主键,值为ziy00000001,ziy00000002,ziy00000003.......,再加上其它字段等等,现在有一个需求,需要把另外一个数据库中的某个表的数据插入到A表中,当然这两个表不太一样,但是其中大部分的字段可以插入的,但是A表中的这个ID字段,我不知道该怎么写SQL语句让他生成 ziy开头的这个格式的字符串,又不能不生成,苦恼中

解决方案 »

  1.   

    --自动编号的例子.材料编号=类别编号+流水号--创建自定义函数,得到新的ID
    create function f_getid(
    @类别编号 varchar(3))
    returns int
    as
    begin
    declare @re int
    select @re=right(id,4) from(
    select id=max(材料编号) from tb where 类别编号=@类别编号
    ) a
    set @re=isnull(@re,0)+1
    return(@re)
    end
    go--创建测试表
    create table tb(材料编号 varchar(7) primary key default '',类别编号 varchar(3),材料名称 varchar(10))
    go--创建触发器,自动生成材料编号
    create trigger t_insert on tb
    instead of insert
    as
    select * into #t from inserted order by 类别编号
    declare @类别编号 varchar(3),@id int
    update #t set @id=case when @类别编号=类别编号 then @id+1 else dbo.f_getid(类别编号) end
    ,材料编号=类别编号+right('0000'+cast(@id as varchar),4)
    ,@类别编号=类别编号
    insert into tb select * from #t
    go--插入数据测试
    insert into tb(类别编号,材料名称)
    select '101','A材料'
    union all select '101','B材料'
    union all select '302','C材料'--显示结果
    select * from tb order by 材料编号go
    --删除测试环境
    drop table tb
    drop function f_getid
      

  2.   

    /*--自己做编号的示例:
    根据输入的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 行)
    --*/
      

  3.   

    if object_id('tempdb.dbo.#A') is not null drop table #A
    create table #A (intID int identity, ID as 'ziy'+right('0000000'+ltrim(intID),8), other_col int)
    insert #A select 10
    insert #A select 5select * from #A/*
    intID       ID                  other_col
    ----------- ------------------- -----------
    1           ziy00000001         10
    2           ziy00000002         5
    */
      

  4.   

    如上面
    ID创建为计算列,另加个自增字段,你根本无需维护这个ID