Solved

Stored Procedure ? marks less than the actual variables

Posted on 2002-04-10
11
195 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 50 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now