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.
| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-08-30 : 00:34:21
|
| How can I return an output to my ASP from my SP. I havent done this before but this is what I have. Can somebody help me out here or point me to a good article, I can't seem to find any.I have a couple lines commented out where I would like to end the SP and spit out some info to my ASP. They are when the emailaddress or the nameonline is already taken, I want to notify my ASP thats the case. Also, if everything runs ok I am going to add one more select statement that returns the GUID. Could somebody please give me some direction??Thanks alot!MIke123CREATE PROCEDURE [insert_newUser_TESTING] ( @NameOnline [varchar](15), @EmailAddress [varchar](50), @Response1 [varchar](250) = NULL, @Response2 [varchar](250) = NULL, @Response3 [varchar](250) = NULL, @Response4 [varchar](250) = NULL, @Response5 [varchar](250) = NULL, @Response6 [varchar](250) = NULL )AS SET NOCOUNT ONdeclare @userID as int--CHECK TO SEE IF THE NAMEONLINE IS TAKENif isnumeric(right(@nameonline,1)) =1 IF Exists (SELECT userID FROM tblUserDetails WHERE nameOnline = substring(@nameOnline,1,len(@nameOnline)-1)) BEGIN --SET @???? Return ENDelse IF Exists (SELECT userID FROM tblUserDetails WHERE nameOnline = @nameOnline) BEGIN --SET @???? Return ENDIF Exists (SELECT userID FROM tblUserDetails WHERE emailAddress = @emailAddress) BEGIN --SET@??? Return END--Start a transactionBEGIN TRANSACTION--First ActionINSERT INTO [tblUserDetails] ( [NameOnline], [EmailAddress], ) VALUES ( @NameOnline, @EmailAddress, )SELECT @userID = @@IDENTITY--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert New Member tblUserDetails',16,1) RETURN(@@ERROR)END--Second ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES (@userID, 1, @response1)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 1 into tblQuestions',16,1) RETURN(@@ERROR)END--Third ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES ('Identity', 2, @response2)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 2 into tblQuestions',16,1) RETURN(@@ERROR)END--Fourth ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES ('Identity', 3, @response3)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 3 into tblQuestions',16,1) RETURN(@@ERROR)END--Fifth ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES ('Identity', 4, @response4)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 4 into tblQuestions',16,1) RETURN(@@ERROR)END--Sixth ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES ('Identity', 5, @response5)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 5 into tblQuestions',16,1) RETURN(@@ERROR)END--Seventh ActionINSERT INTO [tblQuestions] ( [userID], [questionID], [response]) VALUES ('Identity', 6, @response6)--Some checkingIF NOT @@ROWCOUNT > 0BEGIN ROLLBACK TRANSACTION RAISERROR('Could not Insert Question 6 into tblQuestions',16,1) RETURN(@@ERROR)ENDELSE COMMIT TRANSACTIONGO |
|
|
Nico
Starting Member
18 Posts |
Posted - 2002-08-30 : 09:46:19
|
| If you're talking about ASP.NET then:CREATE PROCEDURE sp_whateverASIF EXISTS( ... ) RETURN 0ELSE RETURN 1when calling your SP:SqlConnection con = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["DSN"] );SqlParameter prm = null;SqlCommand cmd = new SqlCommand( "sp_whatever", con );cmd.CommandType = CommandType.StoredProcedure;prm = cmd.Parameters.Add( "RETURN_VALUE", SqlDbType.Int );prm.Direction = ParameterDirection.ReturnValue;con.Open();cmd.ExecuteNonQuery();result = Convert.ToInt32( cmd.Parameters["RETURN_VALUE"].Value );con.Close();If you're talking about basic ASP then:try www.google.comsearch for "stored procedure asp return" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-08-30 : 20:32:28
|
| is it not possible to return a string value??Thanksmike123Edited by - mike123 on 08/30/2002 20:32:45 |
 |
|
|
Onamuji
Aged Yak Warrior
504 Posts |
Posted - 2002-08-30 : 20:55:04
|
sure, use an output parameter ... its limited to 8000 characters or 4000 characters if you are using unicode... i'm hoping you know how to use output parameters |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-08-31 : 19:36:23
|
| No, I dont know how to use output parameters :)That is actually what I was trying to get at in my first post, since Im going to need to return the GUID as an output, or return "EMAILTAKEN" or "NAMEONLINETAKEN" depending on what happens.Any suggestions on how to take this on?Thanks alotmike123 |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-09-01 : 08:50:48
|
| SQL Server only returns int parameters ( Int32 in the .NET world ).The closest equivalent is a char/varchar output parameter ( a ref string argument to a method in .NET ).You define the procedure as follows:create procedure OutputParmProc@input1 int,@input2 int,@output varchar(255) outas...{some DML to SET @output = {something}}You then add an output parameter to a SqlCommand object as follows:...prm = cmd.Parameters.Add( "@output", SqlDbType.VarChar ); prm.Direction = ParameterDirection.Output;prm.ExecuteNonQuery();then the parameter value can be recovered withprm.Value();Jonathan{0} |
 |
|
|
|
|
|
|
|