0

I have not found anywhere on how to call an Oracle stored procedure using ADODB from Excel where the stored procedure has no input parameters.

Fake example to illustrate lack of input parameters:

CREATE OR REPLACE PROCEDURE Get_Data
(OUTPUT OUT SYS_REFCURSOR) IS

******************************************************************************/
BEGIN

OPEN OUTPUT FOR 

SELECT DISTINCT
       B.ITEM_ID
       B.ITEM_DESC
    FROM ITEM_FILE B
WHERE ITEM_ID IS NOT NULL
 ORDER BY B.ITEM_ID
; 

END Get_Data;
/

Oh, and the stored procedure is required because we don't want to give users SQL access to create whatever SQL they want.

Is this even possible? And if so, what kind of code would it take to call it?

Thanks, Dan

1
  • That's no problem. Do you have an example with IN parameter? btw, you may prefer to write a FUNCTION rather than a PROCEDURE. Commented May 30, 2018 at 17:11

1 Answer 1

0

Transform your procedure to a function:

CREATE OR REPLACE FUNCTION Get_Data RETURN SYS_REFCURSOR IS
res SYS_REFCURSOR;

BEGIN

OPEN OUTPUT FOR 
SELECT DISTINCT
       B.ITEM_ID
       B.ITEM_DESC
    FROM ITEM_FILE B
WHERE ITEM_ID IS NOT NULL
ORDER BY B.ITEM_ID;  
RETURN res;

END Get_Data;
/

Call in VBA would be as this:

cmd.CommandText = "{CALL Get_Data()}"
cmd.Properties("PLSQLRSet") = True
Set Rst1 = cmd.Execute 
cmd.Properties("PLSQLRSet") = False

Note, by default an OUT parameters are used like this:

cmd.Parameters.Append cmd.CreateParameter("OUTPUT", adVarChar, adParamOutput, 100)

However, for RefCursor parameters you must not declare them with cmd.Parameters.Append.

Have a look at Provider for OLE DB Developer's Guide, it contains several examples. Chapter Stored Procedures and Functions Returning Rowsets should be the most relevant for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I figured it out. I had forgotten the prefix/alias in my stored procedure name when calling it Prefix.stored_proc_name and once the prefix was added it worked fine. The VBA code I use to call the stored procedure, coincidentally is almost exactly as the code in your post. Thank you so much for your reply. Hopefully, this code will help another. Also, thanks for the links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.