views:

58

answers:

2

This stored procedure doesn't work. I've checked the SQL and it returns the correct value when parsed directly to the DB. It's really strange! It just returns 0 rows.

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
+4  A: 
 @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
Martin Smith
Oh that simple? Thanks, I'm lucky I bumped in to that this early on in the project.
Phil
And to answer your question; it is correct that I was looking for the output parameter. Is it better practice to always have an output paramter even though the command is fairly simple?
Phil
@Phil - Yes. I believe an `OUTPUT` parameter is more light weight than a scalar select.
Martin Smith
+1  A: 

You do not need to assign the salt to a variable.

CREATE PROCEDURE dbo.GetSaltOfUser
(
    @eMail nvarchar    
)
AS
BEGIN
    SELECT salt
    FROM UserSet
    WHERE eMail = @eMail
END
Dustin Laine
As it seems, I CAN'T return a var since it has to be an integer? Does this only apply to the "RETURN" command?
Phil
It will return `salt` in whatever datatype it is. You could cast that as anything that you want, with in scope obviously. `CAST(salt AS nvarchar(50)) AS salt`
Dustin Laine