【问题标题】:SELECT statement in Oracle and SQL ServerOracle 和 SQL Server 中的 SELECT 语句
【发布时间】:2015-10-30 22:26:07
【问题描述】:

我有一个类似的 SQL Server 脚本

declare @num int
begin
--//........while loops/ if conditions...
--//........remove code for simplicity
select num from tbl where cnd = @num;
end

我在 Oracle 中做过这个;

Declare v_num number;
begin
--//........while loops/ if conditions...
--//........remove code for simplicity
select num from tbl where cnd = v_num;
end;
/

我遇到了一个错误

PLS-00428:此 SELECT 语句中应有一个 INTO 子句

整个 sql server 查询大约有 500 行,其中声明和分配了可能的变量....在脚本的末尾有一个使用许多这些变量和多个表的 select 语句。我怎样才能让这个 select 语句在 Oracle 中工作?

【问题讨论】:

  • 我们实际上需要更多的代码来理解你想要什么。您想将 num 分配给 v_num 吗? v_num 是否由代码的另一部分填充? @num 是什么?
  • 整个sql query 大约是500 line,它声明和分配了可能的变量......在脚本的末尾有一个select 语句使用了许多这些变量和多个表。我怎样才能让select 语句在Oracle 中起作用。
  • 你得到了woodoo prist的祝福吗?如果您不显示 Relevant 代码,我们怎么知道问题出在哪里?
  • 在您的 Oracle 代码中,您声明了一个变量 v_num,但使用了一个变量 @num。这是一个错字吗?我不太了解 oracle,但 pl/sql 中的选择可能必须选择到游标或另一个表中。至少错误消息表明了这一点。
  • @Peter Paul Kiefer 感谢typo。更正了。

标签: sql sql-server oracle oracle12c


【解决方案1】:

如果你想使用 decalre 块中的v_num,你应该重写你的代码如下:

Declare v_num number;
begin
--//........while loops/ if conditions...
--//........remove code for simplicity
select num from tbl where cnd = v_num ;
end;
/

如果你使用如下:

Declare v_num number;
begin
--//........while loops/ if conditions...
--//........remove code for simplicity
select num from tbl where cnd = :v_num ;
end;
/

您必须将值插入参数v_num

如果你想从numv_num 中选择值,你应该重写你的代码如下:

Declare v_num number;
begin
--//........while loops/ if conditions...
--//........remove code for simplicity
select num into v_num from tbl;
end;
/

【讨论】:

    【解决方案2】:

    应该是

    Declare v_num number;
    begin
    --//........while loops/ if conditions...
    --//........remove code for simplicity
    select num from tbl where cnd = :v_num;
    end;
    /
    

    【讨论】:

    • 即使我这样做select num from tbl 我也收到错误PLS-00428: an INTO clause is expected in this SELECT statement
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2011-04-28
    • 1970-01-01
    • 2018-10-06
    • 2016-10-30
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多