dbTalk Databases Forums  

mysterious behaviour where = (SELECT JAHR ...

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss mysterious behaviour where = (SELECT JAHR ... in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Norbert Winkler
 
Posts: n/a

Default mysterious behaviour where = (SELECT JAHR ... - 09-24-2010 , 03:33 AM






Hi,

the following script creates two tables. The problem causes due to an error
using a not existing YEAR column.
My error, in the real tables, dues to a column named "JAHR" the german word
for year. For testing in a non-german-environment I've replaced JAHR with
YEAR.


In Statement <1> all records are shown.
The machine should show an error like it does with statement <2> or show
only the record with "2010" like in the correct statment <3>.

__________________________________________________
CREATE TABLE TST_ONE_YEAR (
TST_YEAR VARCHAR2(4) NOT NULL
)
/
CREATE TABLE TST_TAB (
YEAR VARCHAR2(4) NOT NULL
)
/

INSERT INTO TST_ONE_YEAR (TST_YEAR) VALUES ('2010');

INSERT INTO TST_TAB (YEAR) VALUES ('2008');
INSERT INTO TST_TAB (YEAR) VALUES ('2009');
INSERT INTO TST_TAB (YEAR) VALUES ('2010');
INSERT INTO TST_TAB (YEAR) VALUES ('XXXX');

commit;

select * from TST_ONE_YEAR;
select * from TST_TAB;

/* <1> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR FROM TST_ONE_YEAR);
/* <2> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR1 FROM TST_ONE_YEAR);
/* <3> */
select * from TST_TAB WHERE YEAR = (SELECT TST_YEAR FROM TST_ONE_YEAR);
________________________________________________



--
Norbert
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit
Production

Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: mysterious behaviour where = (SELECT JAHR ... - 09-24-2010 , 03:39 AM






"Norbert Winkler" <norbert.winkler1 (AT) gmx (DOT) de> a écrit dans le message de news: 18zm0amjxzqek.1iinhm3b7czd4$.dlg (AT) 40tude (DOT) net...
Quote:
Hi,

the following script creates two tables. The problem causes due to an error
using a not existing YEAR column.
My error, in the real tables, dues to a column named "JAHR" the german word
for year. For testing in a non-german-environment I've replaced JAHR with
YEAR.


In Statement <1> all records are shown.
The machine should show an error like it does with statement <2> or show
only the record with "2010" like in the correct statment <3>.

__________________________________________________
CREATE TABLE TST_ONE_YEAR (
TST_YEAR VARCHAR2(4) NOT NULL
)
/
CREATE TABLE TST_TAB (
YEAR VARCHAR2(4) NOT NULL
)
/

INSERT INTO TST_ONE_YEAR (TST_YEAR) VALUES ('2010');

INSERT INTO TST_TAB (YEAR) VALUES ('2008');
INSERT INTO TST_TAB (YEAR) VALUES ('2009');
INSERT INTO TST_TAB (YEAR) VALUES ('2010');
INSERT INTO TST_TAB (YEAR) VALUES ('XXXX');

commit;

select * from TST_ONE_YEAR;
select * from TST_TAB;

/* <1> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR FROM TST_ONE_YEAR);
/* <2> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR1 FROM TST_ONE_YEAR);
/* <3> */
select * from TST_TAB WHERE YEAR = (SELECT TST_YEAR FROM TST_ONE_YEAR);
________________________________________________



--
Norbert
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit
Production
In <1> The YEAR in the subquery refers to the YEAR column of TST_TAB.
As it does not exist in TST_ONE_YEAR, Oracle (and any RDBMS) has a look
to tables one level outer.

Regards
Michel

Reply With Quote
  #3  
Old   
Kay Kanekowski
 
Posts: n/a

Default Re: mysterious behaviour where = (SELECT JAHR ... - 09-24-2010 , 06:17 AM



Hallo Norbert,
try it with aliases and you see that your first statement fails too.

select * from TST_TAB t WHERE YEAR = (SELECT y.YEAR FROM TST_ONE_YEAR
y);

As Michael said, the year column is coming from tst_tab and this is
true for all rows.

works as designed ... but not as expected ;-)

tschüss
Kay

Reply With Quote
  #4  
Old   
Mark D Powell
 
Posts: n/a

Default Re: mysterious behaviour where = (SELECT JAHR ... - 09-25-2010 , 09:00 AM



On Sep 24, 7:17*am, Kay Kanekowski <kay.kanekow... (AT) web (DOT) de> wrote:
Quote:
Hallo Norbert,
try it with aliases and you see that your first statement fails too.

select * from TST_TAB t WHERE YEAR = (SELECT y.YEAR FROM TST_ONE_YEAR
y);

As Michael said, the year column is coming from tst_tab and this is
true for all rows.

works as designed ... but not as expected ;-)

tschüss
Kay
Actually this should not be unexpected behavior since the SQL manual
explicitly states this requirement: "If columns in a subquery have the
same name as columns in the containing statement, then you must prefix
any reference to the column of the table from the containing statement
with the table name or alias. To make your statements easier to read,
always qualify the columns in a subquery with the name or alias of the
table, view, or materialized view."

Reference: Oracle® Database SQL Reference 10g Release 2 (10.2) Part
Number B14200-02 section Using Subqueries

HTH -- Mark D Powell --

Reply With Quote
  #5  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: mysterious behaviour where = (SELECT JAHR ... - 09-26-2010 , 10:34 AM



On 09/24/2010 10:33 AM, Norbert Winkler wrote:
Quote:
Hi,

the following script creates two tables. The problem causes due to an error
using a not existing YEAR column.
My error, in the real tables, dues to a column named "JAHR" the german word
for year. For testing in a non-german-environment I've replaced JAHR with
YEAR.


In Statement<1> all records are shown.
The machine should show an error like it does with statement<2> or show
only the record with "2010" like in the correct statment<3>.

__________________________________________________
CREATE TABLE TST_ONE_YEAR (
TST_YEAR VARCHAR2(4) NOT NULL
)
/
CREATE TABLE TST_TAB (
YEAR VARCHAR2(4) NOT NULL
)
/

INSERT INTO TST_ONE_YEAR (TST_YEAR) VALUES ('2010');

INSERT INTO TST_TAB (YEAR) VALUES ('2008');
INSERT INTO TST_TAB (YEAR) VALUES ('2009');
INSERT INTO TST_TAB (YEAR) VALUES ('2010');
INSERT INTO TST_TAB (YEAR) VALUES ('XXXX');

commit;

select * from TST_ONE_YEAR;
select * from TST_TAB;

/*<1> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR FROM TST_ONE_YEAR);
/*<2> */
select * from TST_TAB WHERE YEAR = (SELECT YEAR1 FROM TST_ONE_YEAR);
/*<3> */
select * from TST_TAB WHERE YEAR = (SELECT TST_YEAR FROM TST_ONE_YEAR);
________________________________________________



Apart from the semantics, the technical design sucks.

Do NOT name columns in your design after something
that is not stored. Naming a column YEAR (or JAHR in german)
makes people expect something of a date is being stored.

You actually store character strings, not (part of) dates,
hence YEAR_INDICATOR would be more appropriate.
Calling the column YEAR will bite you. In performance (treating it
as date, using implicit conversions, indexes useless), or while
sorting.
--

Regards,

Frank van Bortel

Reply With Quote
  #6  
Old   
Kay Kanekowski
 
Posts: n/a

Default Re: mysterious behaviour where = (SELECT JAHR ... - 09-27-2010 , 03:00 AM



Am 25.09.2010 16:00, schrieb Mark D Powell:
Quote:
On Sep 24, 7:17 am, Kay Kanekowski<kay.kanekow... (AT) web (DOT) de> wrote:

works as designed ... but not as expected ;-)


Actually this should not be unexpected behavior since the SQL manual
explicitly states this requirement: "If columns in a subquery have the
same name as columns in the containing statement, then you must prefix
any reference to the column of the table from the containing statement
with the table name or alias. To make your statements easier to read,
always qualify the columns in a subquery with the name or alias of the
table, view, or materialized view."

Reference: Oracle® Database SQL Reference 10g Release 2 (10.2) Part
Number B14200-02 section Using Subqueries

HTH -- Mark D Powell --
ok, i correct it:

works as designed ... but not as Norbert and i expect it ;-)

regards
Kay

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.