Search code examples
sqlsql-servert-sqlstored-procedures

stored procedure returns nothing


This stored procedure doesn't work. It just returns 0 rows. I've checked the SQL and it returns the correct value when parsed directly to the DB. 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

Solution

  •  @eMail nvarchar
    

    Will truncate the passed in email to one character. You need to put in a length. e.g.

      @eMail nvarchar(50)
    

    This should match the datatype of the relevant column in 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?

    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'[email protected]', @salt OUTPUT
    SELECT @salt