在 Oracle 中的过程中选择语句 [英] Select Statement inside a procedure in Oracle

查看:25
本文介绍了在 Oracle 中的过程中选择语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉问这个问题,但这是因为之前给出的答案都不足以让我理解.我想编写一个返回表中所有列的存储过程.作为一个临时查询,我只写

sorry for asking this question but it's because none of the answers given before are enough for me to understand. I want to write a stored procedure that returns all the columns in a table.As an ad hod query I just write

SELECT * FROM EMPLOYEES

但是在这里,我收到一个错误提示我提供 INTO 子句,我不明白为什么和如何.有人能解释一下在上述情况下我将如何做到这一点,当我只想返回一列值(多行).

but here, I get an error which prompts me to provide INTO clause which I don't understand why and how.Could someone explain how would I do that both in the above case and when I want to return just one column values(multiple rows).

推荐答案

具有 SQL Server 背景的人习惯于编写返回整个查询结果的存储过程,因此尝试编写如下 PL/SQL 过程:

People with a SQL Server background are used to writing stored procedures that return whole query results and so try to write PL/SQL procedures something like this:

procedure get_emps is
begin
    -- this will NOT work!
    select * from emp;
end;

不幸的是,事情没那么简单.PL/SQL 中最接近的等价物可能是返回引用游标的函数:

Unfortunately it's not that simple. Probably the nearest equivalent in PL/SQL is a function returning a ref cursor:

function get_emps return sys_refcursor is
    rc sys_refcursor;
begin
    open rc for
        select * from emp;
    return rc;
end;

您可以从这样的调用程序中调用它:

You could call this from a calling program like this:

declare
    cur sys_refcursor;
    emp_rec emp%rowtype;
begin
    cur := get_emps;
    loop
        fetch cur into emp_rec;
        exit when cur%notfound;
    end loop;
    close cur;
end;

或者在 SQL Plus 中你可以这样做:

or in SQL Plus you could do:

var rc refcursor
:rc := get_emps;
print rc

这篇关于在 Oracle 中的过程中选择语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆