我有一个生成员工ID的现有存储过程。员工ID的格式为EPXXXX,EP则为4个数值。我希望缩短存储过程。
给出上面的表(tblEmployee)。以下是使用新员工编号插入新员工的存储过程。过程是我必须得到最后一个员工ID,获取最后4位数(这是数字),将其转换为整数,将1加到增量,检查数字是否小于10,100或1000或等于/大于超过1000,在将新记录插入表之前添加前缀。
create procedure NewEmployee
@EmployeeName VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
DECLARE @lastEmpID as VARCHAR(6)
SET @lastEmpID =
(
SELECT TOP 1 Employee_ID
FROM tblEmployee
ORDER BY Employee_ID DESC
)
DECLARE @empID as VARCHAR(4)
SET @empID =
(
SELECT RIGHT(@lastEmpID, 4)
)
DECLARE @numEmpID as INT
@numEmpID =
(
SELECT CONVERT(INT, @empID) + 1
)
DECLARE @NewEmployeeID as VARCHAR(6)
IF @numEmp < 10
SET @NewEmployee = SELECT 'EP000' + CONVERT(@EmpID)
IF @numEmp < 100
SET @NewEmployee = SELECT 'EP00' + CONVERT(@EmpID)
IF @numEmp < 1000
SET @NewEmployee = SELECT 'EP0' + CONVERT(@EmpID)
IF @numEmp >= 1000
SET @NewEmployee = SELECT 'EP' + CONVERT(@EmpID)
INSERT INTO tblEmployee(Employee_ID, Name)
VALUES (@NewEmployeeID, @EmployeeName)
END
答案 0 :(得分:2)
我不是在暗示你现在有什么,但我会这样做。这是我在我的应用程序中实现的方式。哪个我要给你。希望你喜欢这个。这是完全动态的,适用于您可能拥有的所有交易。
我有一张表格,其中包含文件编号:
CREATE TABLE INV_DOC_FORMAT(
DOC_CODE VARCHAR(10),
DOC_NAME VARCHAR(100),
PREFIX VARCHAR(10),
SUFFIX VARCHAR(10),
[LENGTH] INT,
[CURRENT] INT
)
哪个会保存数据:
INSERT INTO INV_DOC_FORMAT(DOC_CODE,DOC_NAME,PREFIX,SUFFIX,[LENGTH],[CURRENT])
VALUES('01','INV_UNIT','U','',5,0)
INSERT INTO INV_DOC_FORMAT(DOC_CODE,DOC_NAME,PREFIX,SUFFIX,[LENGTH],[CURRENT])
VALUES('02','INV_UNIT_GROUP','UG','',5,0)
并且,我有一个功能或程序但是,我在这里有一个函数,它将生成文档编号。
CREATE FUNCTION GET_DOC_FORMAT(@DOC_CODE VARCHAR(100))RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @PRE VARCHAR(10)
DECLARE @SUF VARCHAR(10)
DECLARE @LENTH INT
DECLARE @CURRENT INT
DECLARE @FORMAT VARCHAR(100)
DECLARE @REPEAT VARCHAR(10)
IF NOT EXISTS(SELECT DOC_CODE FROM INV_DOC_FORMAT WHERE DOC_CODE=@DOC_CODE)
RETURN ''
SELECT @PRE= PREFIX FROM INV_DOC_FORMAT WHERE DOC_CODE=@DOC_CODE
SELECT @SUF= SUFFIX FROM INV_DOC_FORMAT WHERE DOC_CODE=@DOC_CODE
SELECT @LENTH= [LENGTH] FROM INV_DOC_FORMAT WHERE DOC_CODE=@DOC_CODE
SELECT @CURRENT= [CURRENT] FROM INV_DOC_FORMAT WHERE DOC_CODE=@DOC_CODE
SET @REPEAT=REPLICATE('0',(@LENTH-LEN(CONVERT(VARCHAR, @CURRENT))))
SET @FORMAT=@PRE + @REPEAT +CONVERT(VARCHAR, @CURRENT+1) + @SUF
RETURN @FORMAT
END
您可以使用以下功能:
INSERT INTO INV_UNIT(UNIT_CODE,UNIT_NAME,UNIT_ALIAS,APPROVED,APPROVED_USER_ID,APPROVED_DATE)
VALUES(DBO.GET_DOC_FORMAT('01'),@Unit_Name,@Unit_Alias,@APPROVED,@APPROVED_USER_ID,@APPROVED_DATE)
--After Transaction Successfully complete, You can
UPDATE INV_DOC_FORMAT SET [CURRENT]=[CURRENT]+1 WHERE DOC_CODE='01'
或者,你可以创建一个单独的程序来处理所有事情。
希望你顺道......
因此, 看着你的方式,你正在犯错误。 你来了 SET @lastEmpID = ( SELECT TOP 1 Employee_ID 来自tblEmployee ORDER BY Employee_ID DESC )
上一个员工ID,然后您正在操纵ID的其余部分。这将创建或重用先前生成的ID,但现在已删除。 假设EMP0010在那里。有一天,EMP被删除了。所以,当你下次再次创建一个Employeee时,你将拥有你以前为anohter Employe所拥有的相同的Emp ID,但是不会有更多的退出。我认为这不是一个好主意。
而且,而不是:
DECLARE @NewEmployeeID as VARCHAR(6)
IF @numEmp < 10
SET @NewEmployee = SELECT 'EP000' + CONVERT(@EmpID)
IF @numEmp < 100
SET @NewEmployee = SELECT 'EP00' + CONVERT(@EmpID)
IF @numEmp < 1000
SET @NewEmployee = SELECT 'EP0' + CONVERT(@EmpID)
IF @numEmp >= 1000
SET @NewEmployee = SELECT 'EP' + CONVERT(@EmpID)
您曾经重复过零。您将使用SQL的 Replicate Function()。如上所述,我的实例。
SET @REPEAT=REPLICATE('0',(@LENTH-LEN(CONVERT(VARCHAR, @CURRENT))))
答案 1 :(得分:1)
试试这个 -
CREATE PROCEDURE dbo.NewEmployee
@EmployeeName VARCHAR(50)
AS BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.tblEmployee(Employee_ID, Name)
SELECT
'EP' + RIGHT('0000' + CAST(Employee_ID + 1 AS VARCHAR(4)), 4)
, @EmployeeName
FROM (
SELECT TOP 1 Employee_ID = CAST(RIGHT(Employee_ID, 4) AS INT)
FROM dbo.tblEmployee
ORDER BY Employee_ID DESC
) t
END
答案 2 :(得分:1)
我认为您不需要Stored Procedure
,请尝试使用 Ranking Functions
select
'EP'+RIGHT('000000'+ CAST(ROW_NUMBER() OVER (ORDER BY Name) AS VARCHAR(6)), 4)
AS [emp_code]
,
Name
FROM emp1 WITH(NOLOCK)
<强> SQL Fiddle 强>
修改强>
select
'EP'+RIGHT('000000'+ CAST((ROW_NUMBER() OVER (ORDER BY Name)+10) AS VARCHAR(6)), 4)
AS [emp_code] --^Add the last Emp no.
,
Name
FROM emp1 WITH(NOLOCK)
<强> SQL Fiddle 强>
答案 3 :(得分:0)
当然接受的答案工作正常,但如果我们在之前的值中有numm,它就无法正常工作。所以修改如下,希望这也有助于其他人
CREATE PROCEDURE dbo.NewEmployee
@EmployeeName VARCHAR(50)
AS BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.tblEmployee(Employee_ID, Name)
SELECT
'EP' + RIGHT('0000' + CAST(Employee_ID + 1 AS VARCHAR(4)), 4)
, @EmployeeName
FROM (
SELECT TOP 1 Employee_ID = CAST(RIGHT(Employee_ID, 4) AS INT)
FROM dbo.tblEmployee
ORDER BY Employee_ID DESC
) t
END