Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored procedure returns no value and no error

Author  Topic 

SEFL
Starting Member

8 Posts

Posted - 2011-03-22 : 13:00:13
Hi there,

After 11 years of using Access and SQL Server in various capacities, I've finally gotten around to trying to teach myself stored procedures for the first time. What I'm trying to do is create a procedure to check a username to make sure there are no duplicates (to avoid any errors from the .NET page that uses the username).

Here's the stored procedure to check for the duplicate username:

ALTER PROCEDURE [dbo].[Check_Username]
-- Add the parameters for the stored procedure here
@Username nvarchar (255),
@User_ID bigint = 0
AS
set Nocount on
Declare @Check_User_ID bigint = 0
SELECT @Check_User_ID = Users.User_ID from Users where Username = @Username and Users.User_ID <> @User_ID
return @Check_User_ID

Now, when I run Execute Stored Procedure and check it with a username that already exists, the value that is returned is the user ID associated with the username, which is what I was expecting.

DECLARE @return_value int

EXEC @return_value = [dbo].[Check_Username]
@Username = N'SEFL',
@User_ID = 0

SELECT 'Return Value' = @return_value

GO


When that runs, I get the user ID associated with SEFL. That's what I want to see.

But if I run it like this, which is how I run it on the .NET page as well as within SQL Server, nothing at all gets returned. No error, but no result either. I don't even get a return value of 0.

exec Check_Username 'SEFL', 0

Within SQL Server, it says "Command(s) completed successfully."

The question I have is...why don't I see anything the second way when I see something the first way?

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-22 : 13:10:37
Not tested...but try this..also...do have enterprise manager?

And...I'm not so sure of the logic here



IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Check_Username]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_Check_Username]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[usp_Check_Username]
@Username nvarchar (255)
, @User_ID bigint = 0
, @rc int OUTPUT
AS

SET NOCOUNT ON

SELECT @rc = COUNT(*)
FROM [Users]
WHERE [UserName] = @UserName
AND [UserId] <> @User_ID

-- Please add appropriate Error Handling & Housekeeping

usp_Check_Username_Exit:

Return

usp_Check_Username_Err:

GOTO usp_Check_Username_Exit
GO

DECLARE @rc int
EXEC usp_Check_Username
'X002548' -- @Username nvarchar (255)
, null -- @User_ID bigint = 0
,@rc OUTPUT

SELECT @rc AS [RC]
GO



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

SEFL
Starting Member

8 Posts

Posted - 2011-03-22 : 14:07:05
Thanks, Brett.

I tried what you suggested, and it did work within SQL Server Management Studio. The problem is that the output would be sent to an ASP.net page, not Management Studio. Within ASP.net, nothing happens when I execute the query. So it didn't work. :(
Go to Top of Page

SEFL
Starting Member

8 Posts

Posted - 2011-03-22 : 14:21:47
Okay, after some playing around, I did manage to get it to work, but I really don't like the solution.

Basically, what I did is took your idea of executing the procedure inside of Management Studio and created a stored procedure that did nothing but that.

ALTER PROCEDURE [dbo].[Check_Username]
-- Add the parameters for the stored procedure here
@Username nvarchar (255) = '',
@User_ID bigint = 0
AS
Declare @RC bigint = -1
EXECUTE @RC = Check_Username_Sub @Username, @User_ID -- I renamed the original procedure Check_Username_Sub.
Select @RC as [RC]
return @RC

I needed both the select @RC line and the return @RC line to output the correct value to my .NET page, which it now does.

But here's what I don't understand now...

1) Why do I need to execute a stored procedure from another stored procedure to get the correct output in the first place?

2) Why do I need to repeat the output and create two result sets for one single value on an ASP.net page?
Go to Top of Page

SEFL
Starting Member

8 Posts

Posted - 2011-03-22 : 14:22:19
By the way, is there any way to "like" or "thank" someone for trying to help them on this board?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-22 : 14:24:23
Most of us like , you can add one using [beer] in your reply.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-22 : 14:38:55
quote:
Originally posted by robvolk

Most of us like , you can add one using [beer] in your reply.



I prefer



[ chug]



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-22 : 14:41:53
I would also suggest that you leave Return alone.

SQL Server Can overwrite whatever you pass with what it wants in certain instance (I'm not sure in 2k8), making it unreliable.

A result set or an output variable is the way to go



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

SEFL
Starting Member

8 Posts

Posted - 2011-03-24 : 12:11:41
Okay...

Tried removing the 'Return' line from the sub in question and just used the Select @RC as [RC] line, which did work, so ultimately I got what I wanted, which is great.

The only thing I still don't understand is what the whole point of "return" is in the SQL Server context. If it's going to throw things off, then why is it there in the first place? I'm used to it from the VB and Javascript contexts where it returns a value.

x 24 to X002548 at any rate!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-24 : 12:27:50
Is the question why you are allowed to pass an int with return?

I...do not know

It's like giving a monkey a gun..you can do it...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -