473,386 Members | 1,674 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,386 software developers and data experts.

Running a batch of sql queries?

Our application currently interfaces to SQL Server and Oracle, we're
implementing it into DB2 currently and I have a question to ask.

First a little background - We're interfacing into DB2 v8 on zOS (I
hope the terminology is right - ours is a Windows client app, and the
DB2 is at a customer site and they are taking care of the server),
using ADO with the IBM OLE Db Provider (IBMDADB2). We don't use any
vendor specific APIs, instead we stick to running all of our sql
queries through the recordset and execute ADO methods. This allows us
to minimize the vendor specific code in our app since we restrict
ourselves to using the industry standard SQL syntax (hah! Well, at
least it's less "unstandard" than the individual vendor APIs).

In working with SQL Server and Oracle, one lesson learned is that
sending a series of related queries in a single batch (that is, a
single recordset or execute method) to the database significantly
improves performance (one trip across the WAN to the server, one
execution plan compiled, etc.). Assuming that this is also true for
DB2, my question is how to make this work. So far, I haven't had
success at this (it's working just fine in sending the queries
individually, I'm trying to get it to work in a batch).

Here's a simplification of my situation (hopefully, not an
oversimplification). Table1 has an identity field, along with other
data columns, and there is a one-to-many relationship to Table2. There
could be one or a couple of dozen records inserted into Table2,
depending on the context, for each record in Table1, so doing this work
in a stored procedure is, unfortunately, not feasible.

This is an example of the batch that's sent when we're interfacing to
SQL Server:

declare @RecId as int;
Begin Transaction;
Insert Into Table1 (data columns) Values (data values);
Set @RecId = @@Identity;
Insert Into Table2 (Recordid, other data columns) Values (@RecId, other
data values);
Insert Into Table2 (Recordid, other data columns) Values (@RecId, other
data values);
....
Commit Transaction

So, I've tried a variety of things to accomplish the same sort of task
for DB2. Here's one sample:

Declare vRecId as int;
Insert Into Table1 (data columns) Values (data values);
Values IDENTITY_VAL_LOCAL() into :vRecId;
Insert Into Table2 (Recordid, other data columns) Values (:vRecId,
other data values);
Insert Into Table2 (Recordid, other data columns) Values (:vRecId,
other data values);
....
Commit

So, what am I doing wrong? Is this feasible?

Apologies for the longish post, TIA, and have a nice day!
Mark

Mar 2 '06 #1
7 7813
ma************@gmail.com wrote:
Our application currently interfaces to SQL Server and Oracle, we're
implementing it into DB2 currently and I have a question to ask.

First a little background - We're interfacing into DB2 v8 on zOS (I
hope the terminology is right - ours is a Windows client app, and the
DB2 is at a customer site and they are taking care of the server),
using ADO with the IBM OLE Db Provider (IBMDADB2). We don't use any
vendor specific APIs, instead we stick to running all of our sql
queries through the recordset and execute ADO methods. This allows us
to minimize the vendor specific code in our app since we restrict
ourselves to using the industry standard SQL syntax (hah! Well, at
least it's less "unstandard" than the individual vendor APIs).

In working with SQL Server and Oracle, one lesson learned is that
sending a series of related queries in a single batch (that is, a
single recordset or execute method) to the database significantly
improves performance (one trip across the WAN to the server, one
execution plan compiled, etc.). Assuming that this is also true for
DB2, my question is how to make this work. So far, I haven't had
success at this (it's working just fine in sending the queries
individually, I'm trying to get it to work in a batch).

Here's a simplification of my situation (hopefully, not an
oversimplification). Table1 has an identity field, along with other
data columns, and there is a one-to-many relationship to Table2. There
could be one or a couple of dozen records inserted into Table2,
depending on the context, for each record in Table1, so doing this work
in a stored procedure is, unfortunately, not feasible.

This is an example of the batch that's sent when we're interfacing to
SQL Server:

declare @RecId as int;
Begin Transaction;
Insert Into Table1 (data columns) Values (data values);
Set @RecId = @@Identity;
Insert Into Table2 (Recordid, other data columns) Values (@RecId, other
data values);
Insert Into Table2 (Recordid, other data columns) Values (@RecId, other
data values);
...
Commit Transaction

So, I've tried a variety of things to accomplish the same sort of task
for DB2. Here's one sample:

Declare vRecId as int;
Insert Into Table1 (data columns) Values (data values);
Values IDENTITY_VAL_LOCAL() into :vRecId;
Insert Into Table2 (Recordid, other data columns) Values (:vRecId,
other data values);
Insert Into Table2 (Recordid, other data columns) Values (:vRecId,
other data values);
...
Commit

So, what am I doing wrong? Is this feasible?

Apologies for the longish post, TIA, and have a nice day!
Mark

You want t take a look at multi-row insert:
http://publib.boulder.ibm.com/infoce...bjnvmstr50.htm

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 2 '06 #2
>From the link reference, and reading other docs on the Insert, this
means that I can collapse the multiple Insert queries for Table2 down
to a single query. That's cool - thanks.

But I think my bigger challenge is how do I get the identity value
generated by the insert into Table1 into the insert query (or queries)
when inserting into Table2?

I'd like to run a single execute method from my client app, for
improved efficiency, that'll perform the inserts into both tables:

Insert into Table1 (column names) Values (data values);
Insert into Table2 (RecordId, other column names) Values (<Identity
from Table1>, other data values), (<Identity from Table1>, other data
values), ...

What do I need to add to the above sql (maybe similar to my SQL Server
version of this, maybe not) to achieve this?

Thanks for your time
Mark

Mar 2 '06 #3
ma************@gmail.com wrote:
From the link reference, and reading other docs on the Insert, this

means that I can collapse the multiple Insert queries for Table2 down
to a single query. That's cool - thanks.

But I think my bigger challenge is how do I get the identity value
generated by the insert into Table1 into the insert query (or queries)
when inserting into Table2?

I'd like to run a single execute method from my client app, for
improved efficiency, that'll perform the inserts into both tables:

Insert into Table1 (column names) Values (data values);
Insert into Table2 (RecordId, other column names) Values (<Identity
from Table1>, other data values), (<Identity from Table1>, other data
values), ...

What do I need to add to the above sql (maybe similar to my SQL Server
version of this, maybe not) to achieve this?

DB2 has two solutions for you here.
One is the solution that mimics SQL Server .
The function identity_val_local() will return the identity value
generated for the last single row insert.
The second solution is to use
SELECT <identity_col> FROM FINAL TABLE(INSERT INTO table1 ....)
If you are familiar with Oracle's INSERT WITH RETURN, it's the same
thing (just better ;-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 2 '06 #4
In article <46************@individual.net>, Serge Rielau
(sr*****@ca.ibm.com) says...
DB2 has two solutions for you here.
One is the solution that mimics SQL Server .
The function identity_val_local() will return the identity value
generated for the last single row insert.
The second solution is to use
SELECT <identity_col> FROM FINAL TABLE(INSERT INTO table1 ....)
If you are familiar with Oracle's INSERT WITH RETURN, it's the same
thing (just better ;-)

Cheers
Serge


Is his gonna work on DB2 for zOS also (because that's what the OP was
talking about).
Mar 2 '06 #5
Gert van der Kooij wrote:
In article <46************@individual.net>, Serge Rielau
(sr*****@ca.ibm.com) says...
DB2 has two solutions for you here.
One is the solution that mimics SQL Server .
The function identity_val_local() will return the identity value
generated for the last single row insert.
The second solution is to use
SELECT <identity_col> FROM FINAL TABLE(INSERT INTO table1 ....)
If you are familiar with Oracle's INSERT WITH RETURN, it's the same
thing (just better ;-)

Cheers
Serge


Is his gonna work on DB2 for zOS also (because that's what the OP was
talking about).

Yes.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 2 '06 #6
I have tried using identity_val_local (). Maybe something else is
wrong because it's returning NULL. In the simpliest scenario of a
single record into Table1 and Table2:

Insert into Table1 (column names) Values (data values);
Insert into Table2 (RecordId, other column names) Values
(identity_val_local(), other data values);
Commit

This ends up trying to insert a null value in Table2 for that column.

Mar 2 '06 #7
In article <46************@individual.net>, Serge Rielau
(sr*****@ca.ibm.com) says...
Gert van der Kooij wrote:
In article <46************@individual.net>, Serge Rielau
(sr*****@ca.ibm.com) says...
DB2 has two solutions for you here.
One is the solution that mimics SQL Server .
The function identity_val_local() will return the identity value
generated for the last single row insert.
The second solution is to use
SELECT <identity_col> FROM FINAL TABLE(INSERT INTO table1 ....)
If you are familiar with Oracle's INSERT WITH RETURN, it's the same
thing (just better ;-)

Cheers
Serge


Is his gonna work on DB2 for zOS also (because that's what the OP was
talking about).

Yes.


That's nice, thanks.
Mar 2 '06 #8

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

Similar topics

0
by: Kris | last post by:
Is it possible to run batch programs under OWF control? I need to use SQL Loader working under OWF. Regards, Kris
6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
0
by: Javier Fernandez | last post by:
We just migrate out p690 from kernel 32 bits to kernel 64 bits in order to migrate DB2 v7 to DB2 v8 FP3. The only problem without a solution we find out is since we did the change we cannot run...
0
by: morgal | last post by:
Well, I'll look into my crystal ball and tell you what I've experienced according to your lack of description of your problem and error codes: 1st make 1 recordset per databinding. I'll assume...
3
by: John McGinty | last post by:
simple as that really. I want to run a batch script when a button in pressed. The script is to run a DTS package (thats another issue entirely) to refresh some data which is all working just fine....
1
by: Sreelu | last post by:
Hi One of my applications need to upload some data to some secured sites, using SFTP. This is to be done dynamically. So I'm creating a batch file on the fly and calling a script file(.txt), using...
2
by: Antonio | last post by:
I recently installed on my Windows 2003 Server the Support tools in order to create DNS using dnscmd.exe. I created a batch file called File.bat which I wanted to run from a webservice but it...
2
by: Stephen | last post by:
Hi all, I want to create an app that runs a batch file which inturn runs applications. question: if I start the batch file from app1, how can app1 know that the batch file ran successfully...
4
by: Shiraz | last post by:
Hi I'm using Visual Studio Installer to make my installer, and have not as yet figured out a straightforward way to use it to set environmental variables. Amongst the various things I tried, I'm...
3
by: John | last post by:
Hi. I have a number of batch jobs that are ran nightly on our Windows 2000 based Oracle 8.1.7 (soon to be 9i) server. I have these designed just right, so the Windows Scheduled Tasks runs them...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.