Вопрос

I am working with some legacy SQL that is not really well written. There are ambiguous selects that seem to produce the results we expect for the most part, but not always.

Here's a simplified example of what I am talking about:

create table T1(
  A                 VARCHAR2(10),
  B                 VARCHAR2(10),
  C                 VARCHAR2(10),
  D                 VARCHAR2(10)
  )

create table T2(
  A                 VARCHAR2(10),
  B                 VARCHAR2(10)
  )

insert into T1(A, B, C, D)
values ('AA', 'BB', 'CC', 'DD' )

insert into T2(A, B)
values ('Y', 'N' )

So far nothing unusual, except the queries that are being used...

  SELECT * FROM T1, T2 WHERE T2.A ='Y'

    A B   C   D   A_1 B_1
    AA    BB  CC  DD  Y   N


  SELECT * FROM T2, T1 WHERE T2.A ='Y'

    A B   A_1 B_1 C   D
    Y N   AA  BB  CC  DD

This is clearly ambiguous and Oracle tries to help us by respecifying columns with the suffix _1 where needed. Are there circumstances where Oracle would handle this differently? Eg, Different versions, etc.

For example in the first query the value of Field A is 'AA', but might there be circumstances where the same query would set Field A to 'Y'?

The existing code base has this sort of thing all over the place and it will need to be fixed, for now I am just trying to assess how serious a problem this is? I am mainly concerned that the same SQL, run against different Oracle versions could provide different results.

Thank you!

EDIT: To be clear the plan is to disambiguate the queries. For now the question is how will different versions of Oracle handle this?

Это было полезно?

Решение

If possible, you should probably go through the code and explicitly set column names and aliases, and also do the same to any code that accesses your resultsets by column index. The answers in the link below speak to how uncomfortable you should be with your current situation:

select * vs select column

Of course, I know some dev leads that would suggest, "if it ain't broke, don't fix it" but you're not that guy, right?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top