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 > can variable name be same as column name?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-04, 04:27
Jake K Jake K is offline
Registered User
 
Join Date: Feb 2004
Posts: 107
can variable name be same as column name?

Hi Friends,

I am using Db2v8.1.
Will DB2 allow me to declare a variable name same as the column name? I tried it. It gets successfully created. Here it is:
------------------------------------------------------------------
CREATE PROCEDURE SIMPLEFOR(OUT dept SMALLINT)
LANGUAGE SQL
BEGIN
FOR v2 AS
SELECT dept FROM staff
DO
SET dept = dept + dept;
END FOR ;
END
@
------------------------------------------------------------------

If I used the column name inside the for loop, how will DB2 differentiate between the column name and the variable name?
I expect some error messages to avoid these kind of conflicts. Please let me know if i am not correct.

Thanks & Regards,
Jake
Reply With Quote
  #2 (permalink)  
Old 02-13-04, 08:18
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Jake,
I believe that it is a scope issue. The "dept" inside the FOR loop is not the same ase the "dept" parameter. The columns defined in the select of the FOR loop are only valid within the FOR loop.

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 02-13-04, 09:10
Jake K Jake K is offline
Registered User
 
Join Date: Feb 2004
Posts: 107
Hi Andy,

Thanks for your help. You are correct. It's the scoping which comes into play if there is ambiguity.

If the parameter name is accidently same as the column name, then it will generate unexcepted result.
So the lesson is
"Never use the parameter name that is same as the column name."

Once again thanks for solving this.

With Regards,
Jake

Quote:
Originally posted by ARWinner
Jake,
I believe that it is a scope issue. The "dept" inside the FOR loop is not the same ase the "dept" parameter. The columns defined in the select of the FOR loop are only valid within the FOR loop.

HTH

Andy
Reply With Quote
  #4 (permalink)  
Old 02-13-04, 14:47
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
Re: can variable name be same as column name?

While it's not a good practice to have identical names and parameters, you can resolve your problem via params/fields qualifiers

CREATE PROCEDURE SIMPLEFOR(OUT dept SMALLINT)
LANGUAGE SQL
BEGIN
FOR v2 AS
SELECT dept FROM staff
DO
SET SIMPLEFOR.dept = SIMPLEFOR.dept + v2.dept;
END FOR ;
END
@


If the proc above not just an example, you can re-write it to
select sum(staff.dept) into SIMPLEFOR.dept from staff
Reply With Quote
  #5 (permalink)  
Old 02-16-04, 00:23
Jake K Jake K is offline
Registered User
 
Join Date: Feb 2004
Posts: 107
Re: can variable name be same as column name?

Hi Chuzhoi,

Thanks for spending your valuable time to answer the question. It works fine after I qualified the names, as you mentioned. I wrote this procedure for learning the behaviour of the DB2. Anyway, thanks for the query.

With Regards,
Jake

Quote:
Originally posted by chuzhoi
While it's not a good practice to have identical names and parameters, you can resolve your problem via params/fields qualifiers

CREATE PROCEDURE SIMPLEFOR(OUT dept SMALLINT)
LANGUAGE SQL
BEGIN
FOR v2 AS
SELECT dept FROM staff
DO
SET SIMPLEFOR.dept = SIMPLEFOR.dept + v2.dept;
END FOR ;
END
@


If the proc above not just an example, you can re-write it to
select sum(staff.dept) into SIMPLEFOR.dept from staff
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