?
Solved

Stored Procedure ? marks less than the actual variables

Posted on 2002-04-10
11
Medium Priority
?
208 Views
Last Modified: 2010-03-31
Here is the code, which has 8 question marks(?), and actual variables mapped are 9. So how the 9th variable are mapped?

What is the relation between question mark(?) and set method variables.


Code is....

sp = conn.prepareCall(" {?= call " sp_name(?,?,?,?,?,?,?,?)}");

            sp.registerOutParameter( 1, Types.INTEGER );

            sp.setInt(2, var1);
            sp.setInt(3, var2);
            sp.setInt(4, var3);
            sp.setInt(5, var4);

            sp.setInt(6, var5);

            sp.setInt(7, var6);
            sp.setInt(8, var7);

            sp.registerOutParameter (9, Types.VARCHAR );

            ResultSet rs = sp.executeQuery();

end....
0
Comment
Question by:virendra032700
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6930982
The escape syntax of the SQL you posted includes a result parameter. In other words, the stored procedure is a "function" that takes returns a result. The returned result is parameter number 1. The stored procedure also accepts 8 arguments. The first seven are input parameters -- they procedure expects you to provide a value when you call it. The last parameter is registered as an output parameter so the stored procedure expects to be able to set it and you  retrieve the parameter value after executing the call. So there really are 9 parameter markers which must be bound for this call -- one result parameter and eight formal arguments to the stored procedure.

Best regards,
Jim Cakalic
0
 

Author Comment

by:virendra032700
ID: 6931062
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
 

Author Comment

by:virendra032700
ID: 6931071
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:virendra032700
ID: 6931085
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931160
I hope this is what you are asking for ...

sp = conn.prepareCall(" {?= call sp_name(?,?,?,?,?,?,?,?)}");

// indicates return value of procedure is an int
sp.registerOutParameter(1, Types.INTEGER);

// next 7 set IN parameters to the procedure (all ints)
sp.setInt(2, var1);
sp.setInt(3, var2);
sp.setInt(4, var3);
sp.setInt(5, var4);
sp.setInt(6, var5);
sp.setInt(7, var6);
sp.setInt(8, var7);

// this tells the driver what kind of OUT parameter to expect
// corresponds to last actual parameter of stored procedure call
sp.registerOutParameter(9, Types.VARCHAR);

// execute the call
sp.execute();

// get the value of the OUT parameters
int returnValue = sp.getInt(1);
String outString = sp.getString(9);

Jim
0
 

Author Comment

by:virendra032700
ID: 6931195
sp = conn.prepareCall(" {?= call sp_name

is this ? mark pointing to sp.registerOutParameter( 1, Types.INTEGER ); in the code
or
the actual parameters in function ().
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
ID: 6931219
The 1st question mark is parameter #1. The parameter indexes are the same for registerOutParameter, the setXXX methods, and the getXXX methods. When you call registerOutParameter(1, ...), you are telling the driver that the 1st parameter marker (question mark) that it encounters in the SQL statement you just prepared is an OUT parameter of the specified type. So you count _all_ the parameter markers, not just those that match the formal arguments declared by the stored procedure.

To summarize, the '?' that appears before the '=' is parameter #1. It is the 'target' of the registerOutParameter(1, Types.INTEGER) call.

Jim
0
 

Author Comment

by:virendra032700
ID: 6931230
Jim,

Thanks again. If there are more out parameters can we put more than one ? marks before '=' sign.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931247
There can only be one return value from a stored procedure -- just like there can only be one return value from a Java method. So you can't have multiple parameter markers preceding the '=' sign. However, even in your example you have more than one OUT parameter -- the first is the return value of the procedure and the other is a formal parameter that the procedure sets as part of its execution. You bound it as

    sp.registerOutParameter(9, Types.VARCHAR);

For multiple values, a stored procedure could also return a ResultSet. If it does it in the traditional manner, then you would use the executeQuery method to execute the call so that you could get a ResultSet object. A less-traditional manner is for the stored procedure to return the ResultSet as an OUT parameter. That tends to get a bit nasty and vendor-specific but it can be done.
0
 

Author Comment

by:virendra032700
ID: 6931330
Jim,

Thank you very much for your help as I was looking for the help desperately. Let me churn some of the points u told. If there is something I will write again.

Warm Regards,

Virendra
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931343
You're welcome. Glad I could help.
Jim
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question