create function f_nextBH(@t1 varchar(20),@t2 varchar(20),@t3 varchar(20))
returns varchar(50)
as
begin
declare @i int,@sql varchar(50)
if exists(select * from t where 年月+建设单位+施工单位=@t1+@t2+@t3)
begin
select @i=cast(right(max(合同编号),5) as int) from t where 年月+建设单位+施工单位=@t1+@t2+@t3
select @sql=@t1+@t2+@t3+'- '+right(1000000+(@i+1),5)
end
else
select @i=1,
@sql=@t1+@t2+@t3+'- '+right(1000000+@i,5)
return(@sql)
end
go
create table t
(年月 varchar(20),建设单位 varchar(20),施工单位 varchar(20),合同编号 varchar(50))insert into t
select '200501','A01','B01',dbo.f_nextBH('200501','A01','B01') union all
select '200501','A01','B01',dbo.f_nextBH('200501','A01','B01') union all
select '200501','A02','B01',dbo.f_nextBH('200501','A02','B01') union all
select '200501','A02','B01',dbo.f_nextBH('200501','A02','B01') union all
select '200502','A01','B02',dbo.f_nextBH('200502','A01','B02') union all
select '200502','A01','B02',dbo.f_nextBH('200502','A01','B02')select * from t
drop table t
drop function f_nextBH
年月 建设单位 施工单位 合同编号
-------------------- -------------------- -------------------- --------------------------------------------------
200501 A01 B01 200501A01B01- 00001
200501 A01 B01 200501A01B01- 00002
200501 A02 B01 200501A02B01- 00001
200501 A02 B01 200501A02B01- 00002
200502 A01 B02 200502A01B02- 00001
200502 A01 B02 200502A01B02- 00002(所影响的行数为 6 行)
returns varchar(50)
as
begin
declare @i int,@sql varchar(50)
if exists(select * from t where 年月+建设单位+施工单位=@t1+@t2+@t3)
begin
select @i=cast(right(max(合同编号),5) as int) from t where 年月+建设单位+施工单位=@t1+@t2+@t3
select @sql=@t1+@t2+@t3+'- '+right(1000000+(@i+1),5)
end
else
select @i=1,
@sql=@t1+@t2+@t3+'- '+right(1000000+@i,5)
return(@sql)
end
go
create table t
(年月 varchar(20),建设单位 varchar(20),施工单位 varchar(20),合同编号 varchar(50))insert into t
select '200501','A01','B01',dbo.f_nextBH('200501','A01','B01') union all
select '200501','A01','B01',dbo.f_nextBH('200501','A01','B01') union all
select '200501','A02','B01',dbo.f_nextBH('200501','A02','B01') union all
select '200501','A02','B01',dbo.f_nextBH('200501','A02','B01') union all
select '200502','A01','B02',dbo.f_nextBH('200502','A01','B02') union all
select '200502','A01','B02',dbo.f_nextBH('200502','A01','B02')select * from t
drop table t
drop function f_nextBH
年月 建设单位 施工单位 合同编号
-------------------- -------------------- -------------------- --------------------------------------------------
200501 A01 B01 200501A01B01- 00001
200501 A01 B01 200501A01B01- 00002
200501 A02 B01 200501A02B01- 00001
200501 A02 B01 200501A02B01- 00002
200502 A01 B02 200502A01B02- 00001
200502 A01 B02 200502A01B02- 00002(所影响的行数为 6 行)
Create trigger Tig_ID on 表名
for insert
as--取出最大的合同号
update 表名 set 合同编号=a.年月+a.建设单位+a.施工单位+right('00000'+cast(cast(right(max(合同编号),3) as int)+1 as varchar),5) from 表名 a
inner join inserted b on b.年月=a.年月
and a.建设单位=b.建设单位 and a.施工单位=b.施工单位
where not a.合同编号 is null
(
年月 varchar(10),
建设单位 varchar(10),
施工单位 varchar(10),
合同编号 varchar(100)
)drop Function T_Fun
create Function T_Fun(@t1 varchar(20),@t2 varchar(20),@t3 varchar(20))
returns varchar(100)
begin
declare @T_ID varchar(100)
select @T_ID=@t1+@t2+@t3 + '-' + right(cast(100000+isnull(count(*),0) as varchar),5) from A where 年月+建设单位+施工单位=@t1+@t2+@t3
return (@T_ID)
enddrop TRIGGER TEMP_SC
CREATE TRIGGER TEMP_SC on A
AFTER INSERT
as
insert into A select A.年月,A.建设单位,A.施工单位,dbo.T_Fun(A.年月,A.建设单位,A.施工单位) from inserted A
delete A from A where 合同编号=''
有点错误
情况:当按照次序生成合同号时,由于某种原因(虽然可能性不大)删除了其中的一条合同,而且删除的合同不是最后一条,所以在重新添加记录时会生成相同的记录数(年月 建设单位 施 工单位的都相同)
就不会出现这种情况