472,353 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Declaring a variable in SQL DB2

Hi,
I am having trouble with a simple task of declaring a variable. Is
this possible?

Here is what I want to do.

DECLARE start_date date;
DECLARE end_date date;

SET start_date = '2005-01-01'
SET end_date = '2006-01-01'

SELECT *
FROM ONEILLR.EMP_ACT
where EMENDATE between start_date and end_date;

Thanks
Ross

Nov 12 '05 #1
2 52877
ro*********@gmail.com wrote:
Hi,
I am having trouble with a simple task of declaring a variable. Is
this possible?

Here is what I want to do.

DECLARE start_date date;
DECLARE end_date date;

SET start_date = '2005-01-01'
SET end_date = '2006-01-01'

SELECT *
FROM ONEILLR.EMP_ACT
where EMENDATE between start_date and end_date;

Thanks
Ross


Ross,

I'm assuming you are talking in the context of a stored procedure or a user
defined function ?

create procedure db001.sample
(
in inval INTEGER,
out reply char(5)
)
SPECIFIC DB001.Sample
BEGIN
--
-- Define working storage
--
DECLARE SQLCODE INTEGER;
DECLARE SQLSTATE CHAR(5);
DECLARE v_col1 INTEGER;
DECLARE v_col2 INTEGER;
DECLARE v_sqlcode INTEGER DEFAULT 0;
--
-- Here's a cursor (for processing multiple rows)
--
DECLARE c01cursor CURSOR FOR
SELECT
COL1
FROM
DB001.TABLE1
WHERE
COL2 = inval
ORDER BY COL3
;
--
-- Open cursor
--
OPEN c01cursor;
--
-- Fetch first DAD
--
FETCH FROM c01cursor INTO v_col1;
SET v_sqlcode = SQLCODE;
--
-- Process all rowss
--
WHILE ( v_sqlcode = 0 ) DO
--
-- obviously you want to do some real work here
--
FETCH FROM c01cursor INTO v_col1;
SET v_sqlcode = SQLCODE;
END WHILE;
--
-- Close cursor
--
CLOSE c01dad;
--
-- And here is a singleton select
--
SET (v_col1, v_col2) =
( SELECT col1, col2
FROM
DB001.TABLE1
WHERE COLKEY = keyval
);
SET reply = '00000';

END#

HTH

Phil
Nov 12 '05 #2
ro*********@gmail.com wrote:
Hi,
I am having trouble with a simple task of declaring a variable. Is
this possible?

Here is what I want to do.

DECLARE start_date date;
DECLARE end_date date;

SET start_date = '2005-01-01'
SET end_date = '2006-01-01'

SELECT *
FROM ONEILLR.EMP_ACT
where EMENDATE between start_date and end_date;

Thanks
Ross

Inside of an object (like a trigger or routine): Yes
DB2 today does not support "global" variables.
The standard recommendation is to use a regular table with one row for
"public" varibales, that is a variable which has the same value for all
sessions; or a global temp table for "private" variables.
In practically, when referring to such a "variable" table inside of a
procedure, you want to read the row into a local variables first using
SELECT INTO instead of adding joins all over the map.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3

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

Similar topics

2
by: R.G. Vervoort | last post by:
is it possible to declare a variable in the onchange event of a <select> and use it later in php I would like to put the value of the selected...
5
by: fred | last post by:
I don't know if I'm doing this correctly. I have a little programming experience in python, c++ and some others but this is my first time with...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I...
9
jacoder
by: jacoder | last post by:
i amm on my 4th C class ever;) i have taken to the language ok but im stuck trying to declare the values of my 3 variables hers my effort HOPE...
2
by: vlsidesign | last post by:
Here is my a portion of my program: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; int fc_conv(int fahr); ... snip...
3
by: mamun | last post by:
Hi all, I am trying to create variables dynamically. This is needed because the user interface can have ten different textboxes with name as...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring...
7
by: billq | last post by:
Hello, I ran across a code snippet with declared a variable like private Viewport? viewport I do not understand the use of the ? in the code....
2
by: cbrrr | last post by:
I have the variables declared twice where i think they should be. If I declare them global I get an uninitialized error, the other position gives me...
4
by: Busbait | last post by:
Hi I am trying to declare a variable as a FileDialog object in VB for MS Access 2007 Dim fd As FileDialog But, I am receiving an error...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.