我在编写存储过程时遇到问题。如果select语句没有返回值,我想要返回-1的过程。但是,当select语句返回结果时,我想返回@@rowCount
。
当我运行存储过程时,它在没有结果时返回-1,但是当有结果时它返回1。
有关我需要更改的内容的任何建议吗?
-- Add the parameters for the stored procedure here
@LoginName VARCHAR(25),
@Password VARCHAR(10),
@rowsAffected INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @PasswordSalt VARCHAR(36)
SELECT @PasswordSalt = (SELECT PasswordSalt FROM Employee WHERE LoginName = @LoginName)
SELECT
Employee.ID, FirstName, LastName, LoginName, Email, StatusTypeID,
Description AS StatusDescription
FROM
Employee
JOIN
StatusType st ON Employee.StatusTypeID = st.ID
WHERE
LoginName = @LoginName
AND PasswordHash = HashBytes('SHA2_512', @Password + Cast(@PasswordSalt AS nvarchar(36)))
AND StatusTypeID = 1
IF @@ROWCOUNT = 0
BEGIN
SET @rowsAffected = -1
END
ELSE
BEGIN
SELECT @rowsAffected = @@RowCount
END
END
答案 0 :(得分:4)
您的if语句可能正在重置@@rowcount
。
首先存储该值,然后测试它:
select ...
from ...
set @rowsAffected = @@rowcount
if @rowsAffected = 0
set @rowsAffected = -1