Solved

Stored Procedure ? marks less than the actual variables

Posted on 2002-04-10
11
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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