简体   繁体   English

PL / SQL和SQL之间的区别。 选择进入

[英]Difference between PL/SQL and SQL. Select into

I have a problem with compiling simple piece of code in PL/SQL. 我在PL / SQL中编译简单的代码有问题。 Here comes the code: 代码如下:

DECLARE
    zm VARCHAR(20);
BEGIN
    SELECT TEA_FIRST_NAME  into zm
    from students join teachers on STU_TEA_ID = TEA_ID
    where STU_ID = (select k.STU_ID from students k where k.STU_FIRST_NAME = 
    :BLOCK9.TEXT_ITEM11);
END;

When I try to compile this I see that error: 当我尝试对此进行编译时,我看到了该错误: 遇到符号错误

However, when I run this piece of code in SQL Navigator: 但是,当我在SQL Navigator中运行这段代码时:

SELECT TEA_FIRST_NAME
from students join teachers on STU_TEA_ID = TEA_ID
where STU_ID = (select k.STU_ID from students k where k.STU_FIRST_NAME = 
'Lukasz');

it runs fine and returns one record. 它运行正常并返回一条记录。 What is happening? 怎么了?

I am using Oracle Forms 10g (10.1.2.3.0) PL/SQL (10.1.0.5.0). 我正在使用Oracle Forms 10g(10.1.2.3.0)PL / SQL(10.1.0.5.0)。 Database version 11.2.0.3.0 数据库版本11.2.0.3.0

From the error it appears to be interpreting the keyword 'join' as a table alias, which is odd and maybe implies you're using a version that pre-dates Oracle adding ANSI joins - I don't use Forms so I don't know how old that would have to be. 从错误中看来,它似乎将关键字“ join”解释为表别名,这很奇怪,可能暗示您使用的版本早于Oracle添加ANSI联接的日期-我不使用Forms,所以我不使用知道那要几岁。 You can run the same anonymous block in your non-Forms client to see that it should work. 您可以在非Forms客户端中运行相同的匿名块,以查看它是否可以工作。

Explicitly aliasing the tables ought to remove the confusion: 显式地别名表应该消除混乱:

DECLARE
    zm VARCHAR(20);
BEGIN
    SELECT TEA_FIRST_NAME  into zm
    from students s join teachers t on s.STU_TEA_ID = t.TEA_ID
    where STU_ID = (select k.STU_ID from students k where k.STU_FIRST_NAME = 
    :BLOCK9.TEXT_ITEM11);
END;

... but as it doesn't seem to understand join it still won't like that. ...但是由于似乎不了解join它仍然不会那样。 If it really is that old then you might have to revert to the old join syntax: 如果确实是那么老,那么您可能必须恢复到旧的联接语法:

DECLARE
    zm VARCHAR(20);
BEGIN
    SELECT TEA_FIRST_NAME  into zm
    from students, teachers
    where STU_TEA_ID = TEA_ID
    and STU_ID = (select k.STU_ID from students k where k.STU_FIRST_NAME = 
    :BLOCK9.TEXT_ITEM11);
END;

... though aliasing the tables would still make it clearer. ...尽管对表进行别名处理仍会使其更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM