473,732 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DB2 Two "how to" questions - [conditional loading and variable use]

Hi,

I usually get stuck with one of two questions that keep coming up.
Perhaps some of you brilliant people can shed some light...
1. How to do use if statements in load commands (I usually need this
in the task center):
if (select count(*) from mytable 1000) then load from file

2. How to create temporary variables that I can reuse over several
places within a single SQL statement:
myvar = 100;
select * from mytable where column1 = myvar and column2 = myvar and
column3 = myvar;

Thanks!

Aug 13 '07 #1
5 6758
On Mon, 13 Aug 2007 14:53:27 -0700, jcolino scribbled:
Hi,

I usually get stuck with one of two questions that keep coming up.
Perhaps some of you brilliant people can shed some light...
1. How to do use if statements in load commands (I usually need this in
the task center):
if (select count(*) from mytable 1000) then load from file

2. How to create temporary variables that I can reuse over several
places within a single SQL statement: myvar = 100;
select * from mytable where column1 = myvar and column2 = myvar and
column3 = myvar;
In answer to both questions I'd say as far as DB2's SQL dialect goes: you
can't. Okay, that's not strictly true. I think there are ways doing this
in DB2 with "pure" SQL, but still it's not the "right" way of going about
this.

Generally, the philosophy in DB2 seems to be "leave the control flow and
global stuff to some higher layer", e.g. a shell script running DB2
commands via the CLP (Command Line Processor). The CLP makes this very
easy by enabling persistant connections between calls. For example, a
simple chunk of shell-script under Linux/UNIX:
#!/bin/sh

myvar = 100

db2 CONNECT TO MYDB
if [[ $(db2 "SELECT COUNT(*) FROM MYTABLE WHERE MYCOL 1000") -ne 0 ]]
then
db2 "LOAD FROM MYFILE.IXF OF IXF INSERT INTO MYTABLE"
fi

db2 "SELECT * FROM MYTABLE WHERE COLUMN1 = $myvar AND COLUMN2 = $myvar
AND COLUMN3 = $myvar"
This is rather more difficult on Windows (primarily because CMD is so
useless as a scripting environment), in which case I'd recommend a decent
scripting language like Perl/Ruby/Python instead.

If you're absolutely determined to do this in pure SQL, you'll probably
need to do it with a stored procedure (one that uses the ADMIN_CMD
procedure to run the LOAD command in ths first case). For example (these
are off the top of my head - untested):

--#SET TERMINATOR @

/* Drop the procedures if they already exist */
DROP SPECIFIC PROCEDURE TASK1@
DROP SPECIFIC PROCEDURE TASK2@

/* Procedure for the first task */
CREATE PROCEDURE TASK1()
SPECIFIC TASK1
LANGUAGE SQL
BEGIN
DECLARE I INTEGER DEFAULT 0;
/* Capture the value returned by the query into I */
SELECT COUNT(*) INTO I FROM MYTABLE WHERE COL1 1000;
/* Conditionally execute the load, depending on I */
IF I <0 THEN
CALL SYSPROC.ADMIN_C MD('LOAD FROM MYFILE.IXF OF IXF INSERT INTO
MYTABLE');
END IF;
END@

/* Procedure for the second task */
CREATE PROCEDURE TASK2()
SPECIFIC TASK2
/* Declare that the procedure returns a result set */
RESULT SETS 1
LANGUAGE SQL
BEGIN
/* Declare the "global" variable (actually a variable local to the
prcoedure body in this case */
DECLARE MYVAR INTEGER;
/* Declare a cursor for the result set */
DECLARE C CURSOR WITH RETURN FOR
SELECT *
FROM MYTABLE
WHERE COLUMN1 = MYVAR
AND COLUMN2 = MYVAR
AND COLUMN3 = MYVAR;
/* Set the global variable to a value */
SET MYVAR = 100;
/* Open the cursor for the return query (it'll use the current value of
MYVAR when its run). Note that the cursor is left open to return the
result set */
OPEN C;
END@

CALL TASK1@
CALL TASK2@

Note that the statement terminator in the above is @ to permit ; to be
used as the terminator within the procedure bodies.
Cheers,

Dave.
Aug 14 '07 #2
<jc*****@gmail. comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.com.. .
Hi,

I usually get stuck with one of two questions that keep coming up.
Perhaps some of you brilliant people can shed some light...
1. How to do use if statements in load commands (I usually need this
in the task center):
if (select count(*) from mytable 1000) then load from file

2. How to create temporary variables that I can reuse over several
places within a single SQL statement:
myvar = 100;
select * from mytable where column1 = myvar and column2 = myvar and
column3 = myvar;

Thanks!
You can use control logic and define variables within a "begin atomic"
block.

However, since a load is not SQL (it is a command), you cannot include it
within an SQL statement. But you can call a stored procedure that causes a
load to occur if you have DB2 version 9.

DB2 version 8.2.2 support the following DB2 commands in the procedure
SYSPROC.ADMIN_C MD.

DESCRIBE
EXPORT
PRUNE HISTORY/LOGFILE
REORG INDEXES/TABLE
RUNSTATS
UPDATE DATABASE CONFIGURATION

DB2 V9 supports these DB2 commands to SYSPROC.ADMIN_C MD:

ADD CONTACT
ADD CONTACTGROUP
AUTOCONFIGURE
BACKUP - online only
DESCRIBE
DROP CONTACT
DROP CONTACTGROUP
EXPORT
FORCE APPLICATION
IMPORT
INITIALIZE TAPE
LOAD
PRUNE HISTORY/LOGFILE
QUIESCE DATABASE
QUIESCE TABLESPACES FOR TABLE
REDISTRIBUTE
REORG INDEXES/TABLE
RESET ALERT CONFIGURATION
RESET DATABASE CONFIGURATION
RESET DATABASE MANAGER CONFIGURATION
REWIND TAPE
RUNSTATS
SET TAPE POSITION
UNQUIESCE DATABASE
UPDATE ALERT CONFIGURATION
UPDATE CONTACT
UPDATE CONTACTGROUP
UPDATE DATABASE CONFIGURATION
UPDATE DATABASE MANAGER CONFIGURATION
UPDATE HEALTH NOTIFICATION CONTACT LIST
UPDATE HISTORY
Aug 14 '07 #3
myvar = 100;
select * from mytable where column1 = myvar and column2 = myvar and
column3 = myvar;
WITH variables(myvar ) AS VALUES (100)
SELECT * from mytable, variables where column1 = myvar and column2 =
myvar and column3 = myvar;

DB2 will unravel it, so no harm done...

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Aug 14 '07 #4
On Mon, 13 Aug 2007 23:15:20 -0400, Serge Rielau scribbled:
>myvar = 100;
select * from mytable where column1 = myvar and column2 = myvar and
column3 = myvar;
WITH variables(myvar ) AS VALUES (100) SELECT * from mytable, variables
where column1 = myvar and column2 = myvar and column3 = myvar;

DB2 will unravel it, so no harm done...
Ooops - that's me not reading the original question properly. I'd assumed
the variable was required across *multiple* SQL statements, hence the
stored proc, when only a single statement was needed - in which case the
CTE with a VALUES statement is indeed a much better solution.

Cheers,

Dave.
Aug 14 '07 #5
Excellent answers\explana tions. Thanks.
Aug 15 '07 #6

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

Similar topics

7
3288
by: RFS666 | last post by:
Hello, I would like to use variables with a type in jscript.NET. I declare them as follows: var x : double = 5.03; This doesn't work in my script, that I write to the page in codebehind with "registerClientScriptBlock" as follows: string script = "<script language=jscript>"; script += "function test()";
42
2130
by: usenet | last post by:
I'm still confused about this and I can't find anywhere that explains it properly. I have the MS book "Access 2003" in front of me and I'm reading Part 5 about VB and so on. It's telling me about how to refer to a specific database and has the example:- Dim dbMyDb As DAO.Database Set dbMyDb = DBEngine.Workspaces(0).Databases(0)
12
3226
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
5
4087
by: Franky | last post by:
Can a Decimal variable be set to "Not a Number" I've been looking in the help and can't find any reference to that. Thanks in advance
21
7854
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
2
1295
by: 1ontherun | last post by:
Hi, Here's the problem. I am trying to allow the user to change a page background to an image file on their computer. The input box is already a 'type=file', however it uses the character "\" when it needs to be "/". Otherwise it errors and the background is blank/white. I used 'type=file' to make it easier for the user to select and input the image file location & filename, of course. In short, I need to filter out "\" in a variable...
5
1849
by: PLS | last post by:
I have a struct containing a few private members and a bunch of properties. When I try to use the structure like this ABC item; item.property1 = 1; item.property2 = 2; ... In total, the properties set will set every private variable.
26
3902
by: Jake Barnes | last post by:
I did a search on the newsgroup comp.lang.javascript. I was searching for "how to play a sound with Javascript". I'm somewhat suprised that the majority of entries are from the 1990s, and there are almost no posts from the last 3 years: http://groups.google.com/group/comp.lang.javascript/search?group=comp.lang.javascript&q=how+to+play+a+sound+with+Javascript&qt_g=Search+this+group Even after sorting by date, there don't appear any...
10
7459
by: Horacius ReX | last post by:
Hi, in some C program I need to port to some architecture, I send to a function the parameter char with predefined values. Inside the function, this data is read and something is calculated. But for some reasons that I can not explain here (too long) the memory is really small and I would need to use the space used by this char. Then I wonder if it can be deleted or destroyed in some way. Afterwards I need to use malloc and free and I...
3
5919
by: Imaginativeone | last post by:
XML <nodeAA>AA</nodeAA> <nodeBB>BB</nodeBB> <nodeCC> <From>12/05</From> <To>11/06</To> <Months>12</Months> <Amount>10.00</Amount> CC </nodeCC>
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.