Solved

Stored Procedure ? marks less than the actual variables

Posted on 2002-04-10
11
196 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
Java SE 8u111  Lot of stuff broke 11 52
fibonacci ten numbers 4 23
table example 4 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 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

13 Experts available now in Live!

Get 1:1 Help Now