我有两个独立的存储过程,一个用于向数据库添加新雇员,另一个用于从数据库中获取雇员。
我正在使用SHA2_256
,并且在插入数据时看起来不错,但是当使用相同的技术来获取员工时,有些东西不起作用。
这是用于添加员工的SP。
USE [db11]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[io_sp_admin_add_emp]
@Id BIGINT, @Lname VARCHAR(20), @Fname VARCHAR(15),@Gender TINYINT,@Bday DATETIME,@LoggedInUser VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
DECLARE @HashId varbinary(50) = HashBytes('SHA2_256', cast(@Id as varbinary(50)))
INSERT INTO io_t_employees(
lname,
fname,
gender,
bday,
[user_name],
hash_id
)
VALUES(
LTRIM(RTRIM(@Lname)),
LTRIM(RTRIM(@Fname)),
@Gender,
@Bday,
@loggedInUser,
@HashId,
)
SELECT CAST(1 as BIT) as 'Status', 'Succeeded' as 'ReturnMessage'
END TRY
BEGIN CATCH
END
然后,我想根据先前存储的UniqueId
获取用户的HashId
。
USE [db11]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[io_sp_admin_emp_helper]
-- Add the parameters for the stored procedure here
@id INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT UniqueId
FROM io_t_employees
WHERE hash_id = HashBytes('SHA2_256', cast(@id as varbinary(50)))
END
不幸的是,第二个过程的查询结果没有数据。 我感觉在第一步(添加员工)中做错了事。