背景:
原本数据库中已经存在 Unit (部门表),仅有一些常用字段(部门ID、部门名称、父部门ID、部门级别之类的)。现在需要两个新的字段,存储部门的编号,如父部门:001,子部门:001001,子子部门:001001001 …
问题分析:
(1)、部门编号以三个字符(001)为基础,迭代增加。需要已知部门序号基础上,自动凑齐三个字符,不足补0。(参照zhou同学)
-- New Function: PadLeft
-- Purpose: if the length of original-string is less than the specified length, fill the specified character in the left.
if Object_ID('FN_PadLeft', 'FN') is not null
Drop Function FN_PadLeft
go
Create Function FN_PadLeft(@original_str nvarchar(500), @total_len int, @char_tofilled nvarchar(10))
Returns nvarchar(500)
as
Begin
if @char_tofilled is null or