473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing variable parameters to stored procedures

Hi all,

Another novice question.

I want to insert multiple records into my DB2 database using stored
procedure but I do not know how many I would like to insert at any
given time. Each record has 6 columns (fields).

Here's an example of a stored procedure that accepts 6 parameters and
inserts one record. I would like to modify this stored procedure such
that it accepts 6, 12 or 18 or more parameters and inserts 1, 2 3 ..or
more rows into the DB - depending on how many parameters it receives.
So, as an example I want to be able to execute something like:

- Call db2.sp_add_log ("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10","11", "12" ) and
have this call insert 2 records into the DB.

At other times I might want to pass 60 parameters and insert maybe 10
records at a time. Is there a way to decide at run-time the number of
parameters I want to pass to a stored procedure.

=============================================
create PROCEDURE db2.sp_add_log
(
IN PLAYEROID_in CHAR(36),
IN PLAYTIME_in CHAR(23),
IN CONTENTNAME_in VARCHAR(1024),
IN CONTENTOID_in CHAR(36),
IN REASON_in VARCHAR(256),
IN DATAFIELD_in VARCHAR(256)
)
LANGUAGE SQL
BEGIN
insert into DB2.PLAYLOGS
(PLAYEROID,
PLAYTIME,CONTENTNAME,CONTENTOID,REASON, DATAFIELD) values
(PLAYEROID_in, PLAYTIME_in, CONTENTNAME_in,
CONTENTOID_in, REASON_in, DATAFIELD_in);

END
==============================================

Thanks a lot.

-Anil

Feb 10 '06 #1
9 10891
an******@gmail.com wrote:
Hi all,

Another novice question.

I want to insert multiple records into my DB2 database using stored
procedure but I do not know how many I would like to insert at any
given time. Each record has 6 columns (fields).

Here's an example of a stored procedure that accepts 6 parameters and
inserts one record. I would like to modify this stored procedure such
that it accepts 6, 12 or 18 or more parameters and inserts 1, 2 3 ..or
more rows into the DB - depending on how many parameters it receives.
So, as an example I want to be able to execute something like:

- Call db2.sp_add_log ("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10","11", "12" ) and
have this call insert 2 records into the DB.

At other times I might want to pass 60 parameters and insert maybe 10
records at a time. Is there a way to decide at run-time the number of
parameters I want to pass to a stored procedure.

=============================================
create PROCEDURE db2.sp_add_log
(
IN PLAYEROID_in CHAR(36),
IN PLAYTIME_in CHAR(23),
IN CONTENTNAME_in VARCHAR(1024),
IN CONTENTOID_in CHAR(36),
IN REASON_in VARCHAR(256),
IN DATAFIELD_in VARCHAR(256)
)
LANGUAGE SQL
BEGIN
insert into DB2.PLAYLOGS
(PLAYEROID,
PLAYTIME,CONTENTNAME,CONTENTOID,REASON, DATAFIELD) values
(PLAYEROID_in, PLAYTIME_in, CONTENTNAME_in,
CONTENTOID_in, REASON_in, DATAFIELD_in);

END
==============================================

Well, they say if all you have is a hammer everything looks like a nail.
Try the same trick again.
Since INSERT is not a cursor you can use EXECUTE IMMEDIATE txt
DB2 supports INSERT INTO T VALUES (....), (.....)
for multi row insert.
Oh and remember ".." is for identifiers, '...' is for strings.

Cheers
Serge

PS: Gluing together statements on the fly and compiling them one of is
not DB2's preferred mode of operation.. It eats CPU, but it gets the job
done. Good learning experience any day.

--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Feb 10 '06 #2
Why do you have OIDs in SQL? Why huge column sizes that invite bad
data? Why did you violate ISO-11179 naming rules? How usable and
descriptive do you think that "data_field" is as a name? Do you know
that a column and field are nothing alike? What is "reason"? A better
name would "vague_reason" for the guy who has to maintain it :) Didyou
pick VARCHAR(256) after actuall research, or is it a "magic number"
that was easy write on the fly?

I do dnot know what DB2's limit on parameters is, but you can fake an
array in SQL/PSM, soemthing like this:
CREATE PROCEDURE AddLog
(IN player_oid_1 CHAR(36), -- OIDs in SQL?
IN play_time_1 CHAR(23), -- temporal data in string?
IN content_name_1 VARCHAR(1024), -- really that big?
IN content_oid_1 CHAR(36), -- OIDs in SQL?
IN vague_reason_1 VARCHAR(256), -- for what??
IN data_field_1 VARCHAR(256), -- fields in SQL? what kind of data

IN player_oid_2 CHAR(36),
IN play_time_2 CHAR(23),
IN content_name_2 VARCHAR(1024),
IN content_oid_2 CHAR(36),
IN vague_reason_2 VARCHAR(256),
IN data_field_2 VARCHAR(256),

etc.

IN player_oid_6 CHAR(36),
IN play_time_6 CHAR(23),
IN content_name_6 VARCHAR(1024),
IN content_oid_6 CHAR(36),
IN vague_reason_6 VARCHAR(256),
IN data_field_6 VARCHAR(256),

)
LANGUAGE SQL
BEGIN

INSERT INTO PlayLogs (player_oid, play_time, content_name,
content_oid, reason, data_field)
VALUES (player_oid_1, play_time_1, content_name_1, content_oid_1,
vague_reason_1, data_field_1);

--skip the insert if the data is missing
IF player_oid_2 IS NOT NULL
THEN
INSERT INTO PlayLogs (player_oid, play_time, content_name,
content_oid, reason, data_field)
VALUES (player_oid_2, play_time_2, content_name_2, content_oid_2,
vague_reason_2, data_field_2);
END IF;

etc.

IF player_oid_6 IS NOT NULL
THEN
INSERT INTO PlayLogs (player_oid, play_time, content_name,
content_oid, reason, data_field)
VALUES (player_oid_6, play_time_6, content_name_6, content_oid_6,
vague_reason_6, data_field_6);
END IF;

END;

The right answer is to split the data in the host program and make
calls the stored procedure, but you knew that from basic Software
Engineering and how a teired architecture works.

Feb 10 '06 #3
If your question is how to accept an unconstrained set of parameters,
the answer is db2 does not support that. However, db2 does allow
PROCEDURE overloading. That is, while the PROCEDURE must have a name,
more than one procedure with the same name (but not the same SPECIFIC
name) can be CREATEd, and each can be given a different set of
parameters. With that, you can CREATE a number of PROCEDUREs, each one
for each multiple of 6.

Ideally, however, this is not the best solution, and the PROCEDURE
should just INSERT one record at a time, and have it called one after
the other.

B.

Feb 14 '06 #4
Brian Tkatch wrote:
If your question is how to accept an unconstrained set of parameters,
the answer is db2 does not support that.


Actually, it does. You could use DB2DARI style procedures (must be written
in C/C++). There you get a SQLDA as input structure and the SQLDA will
contain all the parameters that were provided to the procedure.

However, using DB2DARI procedures is discouraged. And I would say that
there is always an easy work-around, for example by employing temp tables.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Feb 15 '06 #5
Knut Stolze wrote:
Brian Tkatch wrote:

If your question is how to accept an unconstrained set of parameters,
the answer is db2 does not support that.

Actually, it does. You could use DB2DARI style procedures (must be written
in C/C++). There you get a SQLDA as input structure and the SQLDA will
contain all the parameters that were provided to the procedure.

However, using DB2DARI procedures is discouraged. And I would say that
there is always an easy work-around, for example by employing temp tables.

The word you were looking for is DEPRECATED.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 15 '06 #6
>Actually, it does. You could use DB2DARI style procedures (must be written
in C/C++). There you get a SQLDA as input structure and the SQLDA will
contain all the parameters that were provided to the procedure.


Ooh, i didn't know that. I guess that's because i do all the routines
here in SQL.

B.

Feb 15 '06 #7
I don't think you can even do that anymore. Cataloged dari style stored
procedure still need to define all its parameters at creation time or
the compiler simply can't do procedure resolution (sqlcode -440). You
can, however, call the stored procedure uncataloged. As Serge pointed
out, that functionality has been deprecated and sqleproc itself was
removed from the application development header file.

Feb 16 '06 #8
W Gemini wrote:
I don't think you can even do that anymore. Cataloged dari style stored
procedure still need to define all its parameters at creation time or
the compiler simply can't do procedure resolution (sqlcode -440). You
can, however, call the stored procedure uncataloged. As Serge pointed
out, that functionality has been deprecated and sqleproc itself was
removed from the application development header file.


I fully agree. We are talking about things shouldn't be used any longer.
Related to that, you won't find this documented in the V8 manuals any
longer (only up to V7).

Sometimes, however, it is still necessary to call an uncataloged procedure
(see the Spatial Extender's "st_enable_db"). And there you have an
arbitrary number of parameters as you point out. The procedure has to be
invoked with the "call library!function" notion.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Feb 16 '06 #9
That's good point. I would really push for a formal support of variable
number of parameters with IBM though if there's an requirement. I
personally think it will be a useful feature at times.

Feb 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by clicking a botton, several action stored procedures...
1
by: tdlogger | last post by:
Help! I’m running ado.net using sql7 with service pack 4. I’m trying to use stored procedures. When I try passing varchar type parameters it comes back with 0 transactions. If I pass int...
11
by: Bã§TãRÐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
2
by: dragonmsw | last post by:
I'm having a problem invoking DB2 Stored Procedures from REXX. I've written a generic SP tester where you enter the schema and name of the SP. The next panel displays the parms for the SP and...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
1
by: xFiver | last post by:
Hey gang, I'm having a tough time wrapping my head around this problem. More than anything, I'm looking for the "why" of why this isn't working. THE SETUP: I have a Cross-Tab query (we'll...
1
by: RB | last post by:
Hi All, I'm trying to use more data-adapters/data-sets in a project, because they are so awesome! Anyhoo, I want to use them with Stored Procedures, rather than raw SQL text. The hard part of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.