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 > How To Create Variable?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-11, 16:25
captainlaw captainlaw is offline
Registered User
 
Join Date: May 2011
Posts: 11
How To Create Variable?

I'm new to DB2 syntax. Is the below syntax correct for create new variable to be consumed by subsequent queries?

CREATE VARIABLE MKTG.TheDate VARCHAR(10) ;
SET TheDate = '2011-05-02' ;
SELECT TheDate,* FROM MKTG.LAWMAPPING

What kind of permission do I need to create a variable?
Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-02-11, 16:35
DebianDog DebianDog is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
you know you can select "current date" or "current time" right?

But here are some examples:

DB2 Database for Linux, UNIX, and Windows
Reply With Quote
  #3 (permalink)  
Old 05-02-11, 17:15
captainlaw captainlaw is offline
Registered User
 
Join Date: May 2011
Posts: 11
I am creating variables other than dates, so would need to know how to correctly create bunch of variable names.

I'm getting permission erorr when executing the following syntax.
What kind of permission do I need? I just want local variable that can be used during a batch run (ie declare @str varchar(255) set @str='partner')

Is there a local variable example? or do I have to create variables objects in the schema itself?

CREATE VARIABLE MKTG.STR VARCHAR(10) ;
Reply With Quote
  #4 (permalink)  
Old 05-02-11, 17:24
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by captainlaw View Post
I just want local variable that can be used during a batch run (ie declare @str varchar(255) set @str='partner')
May be what you need is a compound SQL statement?

Compound SQL (compiled) - IBM DB2 9.7 for Linux, UNIX, and Windows
Reply With Quote
  #5 (permalink)  
Old 05-03-11, 23:10
captainlaw captainlaw is offline
Registered User
 
Join Date: May 2011
Posts: 11
I tried your suggestion of Compound SQL. See my script below. I'm get declare syntax error. Can someone provide syntax for my logic below?

declare lawint INTEGER default 0;
declare lawstring VARCHAR(255) default 'lebron';
while lawint<3
begin
print lawstring+cast(lawint as varchar(10))
set lawint=lawint+1
end
Reply With Quote
Reply

Tags
variable

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