473,387 Members | 1,621 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,387 software developers and data experts.

Variables in SQL???

Another of those "What's the DB2 equivalent to this Sybase syntax"

I use local variables in Sybase SQL scripts to hold values for use
later in the script.

For example:

declare @timenow datetime
select @timenow = getdate()
delete from TableA where activity_date < @timenow
delete from TableB where activity_date < @timenow
delete from TableC where activity_date < @timenow

I've used a datatime variable @timenow to hold a value for later
use, but it could have used an int, char, etc (any datatype). Are
local variables used in DB2 scripts, or do I need to drop the value
into a temp table?

Thanks.
Oct 2 '08 #1
5 29144
On Oct 2, 11:24*am, Richard <rmcgor...@gmail.comwrote:
Another of those "What's the DB2 equivalent to this Sybase syntax"

I use local variables in Sybase SQL scripts to hold values for use
later in the script.

For example:

declare @timenow datetime
select @timenow = getdate()
delete from TableA where activity_date < @timenow
delete from TableB where activity_date < @timenow
delete from TableC where activity_date < @timenow

I've used a datatime variable @timenow to hold a value for later
use, but it could have used an int, char, etc (any datatype). *Are
local variables used in DB2 scripts, or do I need to drop the value
into a temp table?

Thanks.
Not *quite* the same, but pretty close:

BEGIN ATOMIC
DECLARE V_TIMENOW TIMESTAMP;--
SET V_TIMENOW = CURRENT_TIMESTAMP;--
DELETE FROM TABLEA WHERE ACTIVITY_DATE < V_TIMENOW;--
DELETE FROM TABLEB WHERE ACTIVITY_DATE < V_TIMENOW;--
DELETE FROM TABLEC WHERE ACTIVITY_DATE < V_TIMENOW;--
END;

If ACTIVITY_DATE truly is just a date and not a timestamp, then (after
you change the column name ;-) you'll want to do this:

BEGIN ATOMIC
DECLARE V_DATE DATE;--
SET V_DATE = CURRENT_DATE;--
DELETE FROM TABLEA WHERE ACTIVITY_DATE < V_DATE;--
DELETE FROM TABLEB WHERE ACTIVITY_DATE < V_DATE;--
DELETE FROM TABLEC WHERE ACTIVITY_DATE < V_DATE;--
END;

One thing about the BEGIN ATOMIC is it's a black box output-wise. All
you'll get after it completes is a "statement completed successfully"-
type message.

--Jeff
Oct 2 '08 #2
That worked. Thanks. :-)

A couple of follow-up questions:

How would I display the value stored in a variable? In Sybase I could
"select" it, or "print" it. The select gives me an error and I don't
see the print option in the "SQL Reference Volume 2" manual. No other
commands in Volume 2 are popping out as obvious options. I must be
missing it.
Why do I need to type

SELECT current date FROM sysibm.sysdummy1

in my script, instead of

SELECT current date

to display the current date, yet in your example you can say

SET V_DATENOW = current date

Is it just a difference between the SET versus SELECT command?

Thanks.
Oct 2 '08 #3
On Oct 2, 2:24 pm, Richard <rmcgor...@gmail.comwrote:
Another of those "What's the DB2 equivalent to this Sybase syntax"

I use local variables in Sybase SQL scripts to hold values for use
later in the script.

For example:

declare @timenow datetime
select @timenow = getdate()
delete from TableA where activity_date < @timenow
delete from TableB where activity_date < @timenow
delete from TableC where activity_date < @timenow

I've used a datatime variable @timenow to hold a value for later
use, but it could have used an int, char, etc (any datatype). Are
local variables used in DB2 scripts, or do I need to drop the value
into a temp table?

Thanks.
In v9.5, db2 support variable.

create variable sch.timenow date;

set sch.timenow = current date;
delete from t1 where activate_date < sch.timenow;
delete from t2 where activate_date < sch.timenow;
Oct 2 '08 #4
On Oct 2, 1:14*pm, Richard <rmcgor...@gmail.comwrote:
That worked. *Thanks. :-)

A couple of follow-up questions:

How would I display the value stored in a variable? *In Sybase I could
"select" it, or "print" it. *The select gives me an error and I don't
see the print option in the "SQL Reference Volume 2" manual. *No other
commands in Volume 2 are popping out as obvious options. *I must be
missing it.

Why do I need to type

* * SELECT current date FROM sysibm.sysdummy1

in my script, instead of

* * SELECT current date

to display the current date, yet in your example you can say

* *SET V_DATENOW = current date

Is it just a difference between the SET versus SELECT command?

Thanks.
Well, not to pick on Sybase, but--while handy--I don't believe a query
like 'SELECT current date' is valid, i.e. well-formed (ANSI), SQL, as
it's missing the FROM clause. Hence the need in DB2 to refer to the
dummy table.

Populating the variable is a different story, as that's in a
procedural context and the current date is coming from a special
register.

In a stored procedure you could also do a SELECT...INTO and populate a
variable using a SELECT, in addition to populating it via SET using a
scalar fullselect. In other words, in DB2, *depending on the setting*
you could do:

DECLARE V_TIMENOW TIMESTAMP;--
SET V_TIMENOW = CURRENT TIMESTAMP;--

--or--

DECLARE V_TIMENOW TIMESTAMP;--
SELECT CURRENT TIMESTAMP INTO V_TIMENOW FROM SYSIBM.SYSDUMMY1;--

Come to think of it, you don't even need SYSIBM.SYSDUMMY1, you also
could do something like:

SELECT CURRENT TIMESTAMP INTO V_TIMENOW FROM TABLE(VALUES(1)) T(C1);--

--or--

DECLARE V_TIMENOW TIMESTAMP;--
SET V_TIMENOW = (SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1);--

--Jeff

Oct 2 '08 #5
DB2 9.5 provides global variables:
CREATE VARIABLE X INTEGER;
and
SET x = <some expression>;
To get them out from a client you can use:
SET ? = x;
(I don't think that works from CLP)

Insteda of SELECT x FROM SYSIBM.SYSDUMMY1 you can use another ANSI
construct:
VALUES x;
Same numbers of characters to type as SELECT :-)

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Oct 10 '08 #6

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.