473,406 Members | 2,549 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,406 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 53725
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 option (in <select>) in a variable and later when i...
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 javascript. I'm trying have my website detect the...
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 wanted add one more demonstration to the mix. ...
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 SUMONE CAN CORRECT THIS MESSY CODE & LEND A HELPING...
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 txt1, txt2 and so on. I would like to get values of...
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 and initializing variables inside a loop routine,...
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. Thanks Bill
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 a declaration error. Im using visual C++ express...
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 message “ Compiler error : User-Defined type not...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.