查询工作,但存储过程给我一个格式错误

时间:2018-02-04 15:54:54

标签: sql stored-procedures formatting

查询完全返回我想要的内容但是当我将相同的结构插入到我的存储过程中时,我收到格式化错误。

Msg 8116,Level 16,State 1,Procedure RegisterUser,Line 21 参数数据类型varchar对于格式函数的参数1无效。

这是我的疑问。

DECLARE @Department int
SET @Department = (SELECT DepartmentLink FROM DepartmentMaster WHERE Name = 
'Inventory')

DECLARE @Position int
SET @Position = (SELECT TypeLink FROM EmployeeTypes Where Title = 'Clerk')

DECLARE @UID int
set @UID = (SELECT ID FROM EmployeeMaster WHERE FirstName = 'John' and 
LastName = 'Doe')

(SELECT convert(varchar,FORMAT (@Department,'00#'))+'-'+ 
convert(varchar,FORMAT(@Position, '0#'))+'-
'+convert(varchar,FORMAT(@UID,'000#')) as EmployeeID)

这是我的存储过程..

CREATE Procedure RegisterUser
(
@FirstName varchar(255),
@LastName varchar(255),
@Department varchar(255),
@Email varchar(255),
@Password varchar(255),
@UserType varchar(255),
@EmployeeID varchar(255),
@DepartmentLink int,
@Position int,
@UID int
)
AS

SET @Department = (SELECT DepartmentLink FROM DepartmentMaster WHERE Name = 
@Department)

SET @Position = (SELECT TypeLink FROM EmployeeTypes Where Title = @UserType)

set @UID = (SELECT ID FROM EmployeeMaster WHERE FirstName = @FirstName and 
LastName = @LastName)

SET @EmployeeID = (SELECT convert(varchar,FORMAT (@Department,'00#'))+'-'+ 
convert(varchar,FORMAT(@Position, '0#'))+'-
'+convert(varchar,FORMAT(@UID,'000#')) as EmployeeID)

INSERT INTO EmployeeMaster(FirstName, LastName, Department, 
Email,Password,CreationDate, EmployeeID, Active)
VALUES(@FirstName, @LastName, (SELECT DepartmentLink FROM DepartmentMaster 
WHERE Name = @Department), @Email, @Password, GETDATE(),
@EmployeeID
,1)

1 个答案:

答案 0 :(得分:0)

我建议将其写成:

CREATE Procedure RegisterUser (
    @FirstName varchar(255),
    @LastName varchar(255),
    @Department varchar(255),
    @Email varchar(255),
    @Password varchar(255),
    @UserType varchar(255),
    @EmployeeID varchar(255),
    @DepartmentLink int,
    @Position int,
    @UID int
) AS
BEGIN
    DECLARE @DepartmentLink varchar(255);
    DECLARE @PositionLink int;
    DECLARE @Uid_new int;
    DECLARE @EmployeeId_new varchar(255);

    SELECT @DepartmentLink = DepartmentLink
    FROM DepartmentMaster
    WHERE Name = @Department;

    SELECT @PostiionLink = TypeLink
    FROM EmployeeTypes 
    WHERE Title = @UserType;

    SELECT @Uid_new = ID
    FROM EmployeeMaster
    WHERE FirstName = @FirstName and LastName = @LastName;

    SELECT @EmployeeID_new = (FORMAT(@Department, '00#') + '-' + 
                              FORMAT(@Position, '0#')) + '-' + 
                              FORMAT(@UID, '000#')
                             );

    INSERT INTO EmployeeMaster(FirstName, LastName, Department, Email, Password, CreationDate, EmployeeID, Active)
        VALUES(@FirstName, @LastName,
               @DepartmentLink, @Email, @Password, GETDATE(),
               @EmployeeID_new, 1
              );
END;  -- RegisterUser

你可以进一步简化这一点。

注意:

  • 您的代码需要BEGIN / END
  • 不要为参数赋值,除非它们是输出参数。相反,请使用局部变量。
  • FORMAT()已经返回一个字符串 - 无需进行其他转换。
  • 当您使用SET进行作业时,带有子查询的
  • SELECT对我来说很尴尬。
  • 我认为你不需要所有的论点,但这是另一回事。