AS
if exists(select productname from tblProducts where productname = @productname)
return 55555
else
insert into tblproducts (productname, price)
values (@productname, @price)
return @@error
A simple sample stored procedure that checks to see if the product already exists. If so, it returns 55555, otherwise it inserts the product and returns the error code (which will 0 if successful). Now, on the ASP side, I use the command object to send the form contents to the stored procedure. The stored procedure's return value is always the first item in the parameters collection of the command object (cmd.parameters(0)) after the command object's Execute method has been called in this case.
<%
option explicit
response.buffer = true
response.exires = -1441
dim connect, cmd, returnvalue
set connect = server.createobject("adodb.connection")
set cmd = server.createobject("adodb.command")
connect.open YourConnectionString
set cmd.activeconnection = connect
cmd.commandtype = 4 'sp (I know, magic numbers, but add a comment and there you go)
cmd.commandtext = "Proc_InsertProduct"
cmd.parameters(1) = request.form("productname")
cmd.parameters(2) = request.form("price")
cmd.execute
%>
At this point the stored procedure ha
displays a confirmation message of the database action just performed.
<%
returnvalue = cmd.parameters(0)
'Did an error occur?
if returnvalue <> 0
'Some sort of error occurred
response.write MyError(returnvalue)
else
'No errors...
response.redirect ("Whereever.ASP")
end if
set cmd = nothing
connect.close
set connect = nothing
%gt;
The MyError subroutine now needs to use some clIEnt-side JavaScript code to send the user back a pop-up message; once this popup message is read and the OK button is clicked, it should take the user back to Page1.ASP.
<%
function MyError(errorcode)
dim title, message
select case errorcode
case 55555
title = "Error Adding Product"
message = "The product is already in the catalog. Please check your entry."
case else
title = "Error Adding Product"
message = "There was an unspecifIEd error while adding the product to the catalog. Please try again."
end select
MyError = "" & chr(13) & chr(10)
MyError = MyError & "" & chr(13) & chr(10)
MyError = MyError & "" & chr(13) & chr(10)
MyError = MyError & "" & chr(13) & chr(10)
MyError= MyError & "" & chr(13) & chr(10)
end function
%>
MyError essentially sends back a small Html page that pops a message and then goes back a page when the user clicks the 'OK' button on the pop-up. The users entries are left intact when going back to the previous page when using this method. Maybe the case is they mistakenly entered some value and now they can edit it instead of having to retype everything. Furthermore, you don't have to write any code to re-populate their form entrIEs.
Some of the situations where I've used this are when trying to delete records that may be parent records to existing children (I don't like cascading deletes), when trying to edit records that have been flagged for d
您正在看的SQLserver教程是:User Tips: Using Return Values from a SQL Server Stored Procedure to Customize Error Messages_QQGB.c。eletion, etc. It's useful anytime your SQL statement should fail or be aborted and you want to notify the user and make it easy for them to go back to where they were. You can include this into all of your pages that run "action querIEs" and create your own set of custom messages. Just add a new case to your case statement for each possible return code.
If you have any feedback or questions about this technique, please email me at [email protected].
Happy Programming!