473,327 Members | 2,012 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,327 software developers and data experts.

Use of variables

Hi all,

could anyone please tell me how to declare and use variables in DB2 UDB 8.2?

I did it in SQL Server and Oracle without any problems:
DECLARE @my_var FLOAT
SET @my_var = -0.33;
SELECT * from TEST WHERE t*@my_var >= 12.0; (for SQL Server)
or
DEFINE my_var = -0.33;
SELECT * from TEST WHERE t*&my_var >=12.0; (for Oracle)

Is there such a possibility also for DB2? I just found out that a
declaration of variables is possible with
DECLARE my_var float;
SET my_var = -0.33;
But then SELECT * from TEST WHERE t*my_var >= 12.0;
gives me error SQL0206N during execution in the so called "Befehlseditor"

How could I use the declared variable? With $, : or @ in front of my_var it
also doesn't work.

Thanks in advance for good ideas!

Cheers,
Ina
Nov 12 '05 #1
4 22566

"Ina Schmitz" <we*@inalein.net> wrote in message
news:d3*************@news.t-online.com...
Hi all,

could anyone please tell me how to declare and use variables in DB2 UDB 8.2?
I did it in SQL Server and Oracle without any problems:
DECLARE @my_var FLOAT
SET @my_var = -0.33;
SELECT * from TEST WHERE t*@my_var >= 12.0; (for SQL Server)
or
DEFINE my_var = -0.33;
SELECT * from TEST WHERE t*&my_var >=12.0; (for Oracle)

Is there such a possibility also for DB2? I just found out that a
declaration of variables is possible with
DECLARE my_var float;
SET my_var = -0.33;
But then SELECT * from TEST WHERE t*my_var >= 12.0;
gives me error SQL0206N during execution in the so called "Befehlseditor"

How could I use the declared variable? With $, : or @ in front of my_var it also doesn't work.

Thanks in advance for good ideas!

Based on the sample SQL procedures in the Info Center, it appears that you
need to do declare the variable with one statement, for example:

DECLARE my_var integer;

Then, you need to set its value as in this example:

SET my_var = -0.33;

I haven't actually tried this myself so I may be missing something.

Rhino
Nov 12 '05 #2
Ina Schmitz wrote:
Hi all,

could anyone please tell me how to declare and use variables in DB2 UDB 8.2?

I did it in SQL Server and Oracle without any problems:
DECLARE @my_var FLOAT
SET @my_var = -0.33;
SELECT * from TEST WHERE t*@my_var >= 12.0; (for SQL Server)
or
DEFINE my_var = -0.33;
SELECT * from TEST WHERE t*&my_var >=12.0; (for Oracle)

Is there such a possibility also for DB2? I just found out that a
declaration of variables is possible with
DECLARE my_var float;
SET my_var = -0.33;
But then SELECT * from TEST WHERE t*my_var >= 12.0;
gives me error SQL0206N during execution in the so called "Befehlseditor"

How could I use the declared variable? With $, : or @ in front of my_var it
also doesn't work.

Can you give the full example. I.e. is this standalone in a BEGIN
ATOMIC, in an SQL Function ...?
SQL Variables share the same namespace as columns. So there is no need
to use extra characters.
DB2 does not support global variables and it uses scoping rules.
That is:
BEGIN ATOMIC
DECLARE my_var FLOAT;
SET my_var = 5;
END
@
VALUES my_var
@
will fail because my_var is referenced outside teh compoun that declare it.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
> Can you give the full example. I.e. is this standalone in a BEGIN ATOMIC,
in an SQL Function ...?

The full example is:
BEGIN ATOMIC
DECLARE xval FLOAT;
SET xval = -0.33;
SELECT *
FROM TEST
WHERE t*xval >= 12.0;
END;
I executed this in the "Befehlseditor" (I don't know how this is called in
the English version of DB2) and got error number SQL0104N.
I also tried it without "begin atomic... end" around, but then got the error
SQL0206N that xval is not known in the context.
Could you please tell me where there is the error? And how the correct
syntax is?

Thanks in advance,
Ina
Nov 12 '05 #4
Ina Schmitz wrote:
Can you give the full example. I.e. is this standalone in a BEGIN ATOMIC,
in an SQL Function ...?


The full example is:
BEGIN ATOMIC
DECLARE xval FLOAT;
SET xval = -0.33;
SELECT *
FROM TEST
WHERE t*xval >= 12.0;
END;
I executed this in the "Befehlseditor" (I don't know how this is called in
the English version of DB2) and got error number SQL0104N.
I also tried it without "begin atomic... end" around, but then got the error
SQL0206N that xval is not known in the context.
Could you please tell me where there is the error? And how the correct
syntax is?

Yes, that makes sense. As I said DB2 does not suppport global variable.
So you do need the BEGIN ATOMIC..END, a trigger, procedure or function.
(or you use a client language like shell scripting, Perl, ....)
Now to avoid the -104 you need to set the delimiter:
db2 -td%

BEGIN ATOMIC
DECLARE xval FLOAT;
SET xval = -0.33;
SELECT *
FROM TEST
WHERE t*xval >= 12.0;
END
%

I.e. the end of the SQL Statement is marked with a '%'.
Otherwise CLP things yoru statement ends after "FLOAT"

Now when you try thsi you will see that teh statement works, but you get
no resultset back.
That is working as designed. BEGIN..END is not a cursor.
It is used to script logic against the database.

For what you want to do I recommend using SQL Procedures.

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

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

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
4
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
7
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.