By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,992 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,992 IT Pros & Developers. It's quick & easy.

Use of variables

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.