| 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 = 0ASset Nocount onDeclare @Check_User_ID bigint = 0SELECT @Check_User_ID = Users.User_ID from Users where Username = @Username and Users.User_ID <> @User_IDreturn @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 intEXEC @return_value = [dbo].[Check_Username] @Username = N'SEFL', @User_ID = 0SELECT 'Return Value' = @return_valueGO 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 hereIF 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]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER OFFGOCREATE PROCEDURE [dbo].[usp_Check_Username] @Username nvarchar (255) , @User_ID bigint = 0 , @rc int OUTPUTASSET NOCOUNT ONSELECT @rc = COUNT(*) FROM [Users] WHERE [UserName] = @UserName AND [UserId] <> @User_ID-- Please add appropriate Error Handling & Housekeepingusp_Check_Username_Exit:Returnusp_Check_Username_Err:GOTO usp_Check_Username_ExitGODECLARE @rc intEXEC usp_Check_Username 'X002548' -- @Username nvarchar (255) , null -- @User_ID bigint = 0 ,@rc OUTPUT SELECT @rc AS [RC]GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
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. :( |
 |
|
|
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 = 0ASDeclare @RC bigint = -1EXECUTE @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? |
 |
|
|
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? |
 |
|
|
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. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|