Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP call a stored procedure

Status
Not open for further replies.

maswien

Technical User
Sep 24, 2003
1,286
CA

I have a ASP code that calls the oracle procedure, what's the correct way to write this stored procedure?

The ASP code is for registering a user on the web site, and the stored procedure should check if this user already exists in the database, if not add this user, if it does then prompt the user duplicate.

Should I use PL/SQL to write the procedure?

Thanks
 
Assume you have a procedure like this one below, and that it has been already created on the
Oracle database. This procedure doesn't return anything, but that doesn't change anything!
STEP #1:
/******STORED PROCEDURE ON ORACLE DATABASE************/
/*====================================================*/
create or replace procedure test_me
is
w_count integer;
begin
insert into TEST values ('Surya was here');
--commit it
commit;
end;
/*****END OF STORED PROCEDURE****/


STEP # 2:
+++++++++
I assume you have tested it from sql*plus by running the
following statements:

/************TEST THE STORED PROCEDURE FROM SQL*PLUS******/
SQL> execute test_me

PL/SQL procedure successfully completed.

SQL>
/***************END OF TESTING THE STORED PROC************/

STEP# 3:
++++++++
/*****CALLING A STORED PROCEDURE FROM ASP******************/

1. USING THE CONNECTION OBJECT

You can execute stored procedures which perform Oracle Server side tasks and return you a recordset. You can only use this method if
your stored procedure doesn't return any OUTPUT values.
<% Set Conn = Server.CreateObject("ADODB.Connection")

Conn.execute "test_me",-1,4
%>
Note that -1 means no count of total number of records is
required. If you want to get the count, substitute count
with some integer variable

Note that 4 means it is a stored procedure. By using the
actual number -1 and 4, you don't need the server side
include ADOVBS.INC ;-)

The above would do the job on the database and return
back to you without returning any recordsets.

Alternatively, you could:

<% Set rs = conn.execute("test_me",w_count,4) %>

W_count is the number of records affected. If your stored
procedure were to return a query result, it is returned
within your recordset (rs). This method is useful with Stored procs
which return results of an SQL query


2. USING THE COMMAND OBJECT

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Set Comm = Server.CreateObject("ADODB.Command")

Set comm.ActiveConnection = conn
comm.commandtype=4

'(or use adCmdStoredProc instead of 4, but then you would have to
'include the ADOVBS.INC. Its upto you

comm.commandtext = "test_me"

comm.execute
'or
Set rs = comm.execute()
%>

STEP# 4
+++++++++
/************PASSING INPUT/OUTPUT PARAMETERS**************************/
<%
'If your stored procedure accepts IN parameters and returns OUT parameters
'here's how to go about it

set param = comm.Parameters
param.append comm.createparameter("Input",3,1)
param.append comm.createparameter("Output",3,2)
'Note that 3 = adInteger for the datatype
'Note that 1=adParamInput and 2=adParamOutput for parameter direction
'Pass the input value
comm("Input") = "...."

OR

set param = comm.createparameter("InPut",3,1)
set param = comm.createparameter("OutPut",3,2)
comm.parameters.append param
'Pass the input value
comm("Input") = "...."

'Execute after setting the parameters
comm.execute()

'If your stored procedure returns OUT parameters, here's how to get it


Out_1 = comm("Output")
'and so on...

%>

Thats it!
 

Thanks a lot fmsousa!

Following is my stored procedure:

CREATE OR REPLACE PROCEDURE sp_adduser
(pid IN contact.person_id%TYPE,
email IN contact.e_mail%TYPE,
pass IN contact.PASSWORD%TYPE,
ret OUT NUMBER )
AS

cnt NUMBER;
procedure_error EXCEPTION;

BEGIN
SELECT COUNT(*) INTO cnt FROM contact;
IF cnt > 0 THEN
RAISE procedure_error;
ELSE
INSERT INTO contact (person_id, e_mail, PASSWORD) VALUES(pid, email,pass, SYSDATE);
ret:=0;
END IF;

EXCEPTION
WHEN procedure_error THEN
ret := 1;
WHEN OTHERS THEN
ret := 1;

END sp_adduser;


The parameter ret is used to know whether or not the user is added to the table, I can get this returned from oracle
using 'Out_1 = comm("Output")'? or use the method in STEP 3:

Set rs = conn.execute("test_me",w_count,4)

if w_count is 0 I know there is no insert to the table.

Which way is the approprieate way?

Thanks

Mark
 
The use of return codes within oracle is not really necessary. If there is a problem with the insert into the user table you will get an error from Oracle.

For example look at my rewriting of your code.

Code:
CREATE OR REPLACE PROCEDURE p_adduser
(
 pid IN  contact.person_id%TYPE,
 email IN contact.e_mail%TYPE,
 pass  IN contact.PASSWORD%TYPE
)
AS
BEGIN
    SELECT COUNT(*) INTO cnt FROM contact;
    IF cnt > 0 THEN
       raise_application_error(-20001,'No records in CONTACT Table');
    ELSE
       INSERT INTO contact (person_id, e_mail, PASSWORD) VALUES(pid, email,pass, SYSDATE);
       
    END IF;
END sp_adduser;

As you can see there is less code to maintain. If there is an error it will be returned back to the calling client, this should be used as the return code value you seek. In your code you should catch the sqlexception and do what you had intended to do if the return code was 1. Personally I don't think the when others exception should be ever handled as then the client has no idea what has gone on inside the procedure.

Now with the above code you can check the SQLCODE if there is an exception, decide ok, it was 20001 exception, that just means there was no users, let's ignore it, otherwise we should warn the user that something bad happened in the database.
 
hi,

use Out_1 = comm("Output") instead.

fmsousa
 

how can I can the SQLexception in VBscript?

I thought that's not easy so I want the stored procedure return a integer, is ther any way for me to know what's kind of error happens in database from VBscript?

Thanks
 
Sorry I don't know anything useful about vbscript. Only that if you can catch sqlexceptions in other languages I am 99% certain you can catch it in ASP.
 

fmsousa,


I tried following:

set param = comm.createparameter("pid",200,1,50,Request("hidentifier"))
set param = comm.createparameter("email",200,1, 50, (Request("hemail"))
set param = comm.createparameter("pass",200,1, 50, Request("hpassword"))
set param = comm.createparameter("ret",3,2, 4)

comm.parameters.append param

comm.execute()

When I ran it, I got error:

[Oracle][ODBC][Ora]ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'SP_ADDUSER' ORA-06550: line 1, column 7: PL/SQL: Statement ignored



How can I fix the ASP code?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top