日期:2014-05-17 浏览次数:20558 次
update a
set no = (select ck + '-' + kw from b where b.bindid = a.bindid) + '-' + convert(varchar(12),getdate(),112) + '-' + 编号
--在insert的时候可以做触发器,将前面部门截取后,按你的规则计算出编号,用变量存储,然后将前面部门根据相应的条件拼接,最后拼接上编号即可,要注意数字和字符拼接的时候,先将数字转成字符
update 表a
set [NO] = tb2.CK+'-'+tb2.KW+'-'+CONVERT(varchar(12), GETDATE(),112)+CONVERT(varchar(6), REPLACE(CONVERT(TIme, GETDATE()),':', ''))+'-'+RIGHT('000'+CONVERT(nvarchar(3),ROW_NUMBER() OVER(Partition by tb.bindid order by tb.bindid)),3)
from 表a
inner join 表b on 表a.bindid = 表b.bindid
--drop table a,b
--go
create table a(bindid int, NO varchar(50))
create table b(CK varchar(20), KW varchar(20), bindid int)
insert into b
select 'AA', '3J01', 1008 union all
select 'BB', '5501', 1010
go
create trigger dbo.trigger_a_insert
on dbo.a
for insert
as
begin
;with t
as
(
select a.bindid,
a.NO,
row_number() OVER(Partition by a.bindid order by getdate()) as rownum
from a
inner join (select distinct bindid from inserted) i
on a.bindid = i.bindid
)
update t
set [NO] = b.CK+'-'+b.KW+'-'+
&nb