简体   繁体   English

存储过程什么都不返回

[英]stored procedure returns nothing

This stored procedure doesn't work. 此存储过程不起作用。 I've checked the SQL and it returns the correct value when parsed directly to the DB. 我检查了SQL,并在直接解析到DB时返回正确的值。 It's really strange! 真奇怪啊! It just returns 0 rows. 它只返回0行。

What could be wrong? 可能有什么不对?

ALTER PROCEDURE dbo.GetSaltOfUser

 (
 @eMail nvarchar

 )

AS
DECLARE @result nvarchar
 /* SET NOCOUNT ON */
BEGIN
   SELECT @result = salt
   FROM UserSet
   WHERE eMail = @eMail
   RETURN @result
END
 @eMail nvarchar

Will truncate the passed in email to one character. 将传入的电子邮件截断为一个字符。 You need to put in a length. 你需要加长。 eg 例如

  @eMail nvarchar(50)

This should match the datatype of the relevant column in UserSet 这应该与UserSet相关列的数据类型匹配

Also do you really want to be using the return code for this or do you want to use an output parameter perhaps - or just a scalar select? 你真的想要使用返回代码,或者你想使用output parameter - 或者只是标量选择?

ALTER PROCEDURE dbo.GetSaltOfUser

    (
    @eMail nvarchar (50),      /*Should match datatype of UserSet.eMail*/
    @salt nvarchar (50) OUTPUT /*Should match datatype of UserSet.salt*/
  )

AS
BEGIN
    SET NOCOUNT ON

    SELECT @result = salt
    FROM UserSet
    WHERE eMail = @eMail
END

And to call it 并称之为

DECLARE @salt nvarchar(50)
EXECUTE dbo.GetSaltOfUser N'abc@example.com', @salt OUTPUT
SELECT @salt

You do not need to assign the salt to a variable. 您不需要将salt分配给变量。

CREATE PROCEDURE dbo.GetSaltOfUser
(
    @eMail nvarchar    
)
AS
BEGIN
    SELECT salt
    FROM UserSet
    WHERE eMail = @eMail
END

Two Cents from my end 我最后两分钱

  • Good you are using SET NOCOUNT ON 好你正在使用SET NOCOUNT ON
  • Always use RETURN to return status code, Example 0- success, 1 - failure 始终使用RETURN返回状态代码,示例0-成功,1 - 失败
  • Use SELECT to return the ROWS 使用SELECT返回ROWS
  • Use Try Catch to handle Error Conditions 使用Try Catch处理错误条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM