SQL-按自定义格式进行编号的SQL自定义函数
生成格式如:DT.EMP.0000000001的自增emp_id, 加入EmpBaseINfo表中。
--生成格式如DT.EMP.0000000001 【Vegas Add】
ALTERFUNCTION\[dbo\].\[Get\_EmpBaseInfo\_AccountID\](@RowIDasint)
RETURNSnvarchar(50) as
begin
declare@oidnvarchar(50)
declare@headStrnvarchar(50)
set@headStr='DT.EMP.'
----------------------------RowID的计算--------------------------------------
ifexists(select1from empbaseinfo)
--如empbaseinfo存在数据,count(*)后直接加RowID
begin
select@oid=count(1) from empbaseinfo
set@oid=@oid+@RowID
end
else
set@oid=@RowID
----------------------------补全十个数------------------------------
declare@strnvarchar(50) --临时编号
set@str=convert(nvarchar,(convert(int,right(@oid,10))))
while (10-len(@str)>0)
begin
set@str='0'+@str
end
set@oid=@headStr+@str
---------------------------返回值---------------------------------------------------------
RETURN@oid
end
调用:
select dbo.Get\_EmpBaseInfo\_AccountID(ROW_NUMBER() OVER(ORDERBY hbdwno)) as id,
hbdwno,hbdbno,hbdenm,hbdcnm,
(case hbdgdr when1then'M'when0then'F'end),
(case hbdidl when1then'IDL'when0then'DL'end),
hbdwdt,hbdbir,hbdmds,'DT'as domain
from hrmsdt.hrms.dbo.hrshhbd
orderby hbdwno
喜欢请赞赏一下啦^_^