If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 local variables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-08, 09:46
karthi_syb karthi_syb is offline
Registered User
 
Join Date: Jun 2008
Location: India
Posts: 94
DB2 local variables

Hi All,

how to declare the local variaables in DB2?

how many types of variables are there?

in sql,

declare @example varchar(15)
set @example = "welcome"

select *
from <tablename>
where column1 = @example

will work...

what is the equavalent command in DB2 to do the above one ? i.e how to write the above code in DB2 ?
Reply With Quote
  #2 (permalink)  
Old 12-17-08, 14:30
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You care to tell us which version of DB2 you are using on which platform? The answer depends on that because we don't know if you have session variables available or have to use compound statements or resort to temp tables.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 12-18-08, 03:08
karthi_syb karthi_syb is offline
Registered User
 
Join Date: Jun 2008
Location: India
Posts: 94
DB2 version 9 on sun solaris machine.
Reply With Quote
  #4 (permalink)  
Old 12-18-08, 04:33
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by karthi_syb
declare @example varchar(15)
set @example = "welcome"
select *
from <tablename>
where column1 = @example
There are several ways to do this.
First of all, you'll definitely want single quotes around "welcome".
According to the reference manual (SC23-5862), you have the DECLARE VARIABLE statement inside a BEGIN ATOMIC (p. 213), the CREATE VARIABLE statement (p. 600), the "SET variable" statement (p. 954) and the "VALUES" statement (p.991) available.
Try one of the following, depending on the environment (embedded SQL, GUI, command line interface, inside a procedure definition) where you want this:

In the "script" GUI:
Code:
  SET example = 'welcome' ;
  SELECT *
  FROM   tablename
  WHERE  column1 = example ;
END
Inside a procedure, function, or trigger definition, or in a dynamic SQL statement (embedded in a host program):
Code:
BEGIN ATOMIC
  DECLARE example VARCHAR(15) ;
  SET example = 'welcome' ;
  SELECT *
  FROM   tablename
  WHERE  column1 = example ;
END
or (in any environment):
Code:
WITH t(example) AS (VALUES('welcome'))
  SELECT *
  FROM   tablename, t
  WHERE  column1 = example
or (although this is probably not what you want, since the variable needs to be created just once, but can be used thereafter by everybody although its content will be private on a per-user basis):
Code:
CREATE VARIABLE example VARCHAR(15) ;
SET example = 'welcome' ;
SELECT *
FROM   tablename
WHERE  column1 = example ;
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 12-18-08 at 04:46.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On