【问题标题】:Stored Procedure outputting String and Cursor存储过程输出字符串和游标
【发布时间】:2010-11-24 12:20:55
【问题描述】:

大家好,我有一个只输出表格列的存储过程。相反,我希望有 'There are' [column count] 'students'。作为输出。下面的例子。

CREATE OR REPLACE PROCEDURE active_students (arc in out sys_refcursor)
       as
       begin
            open arc for select count(*) from student;
       end;

这会生成

Count(*)
30

希望阅读

There are 30 students.

【问题讨论】:

    标签: sql oracle stored-procedures


    【解决方案1】:

    用途:

    CREATE OR REPLACE PROCEDURE JSU4290M.active_students (arc in out sys_refcursor)
    AS
    BEGIN
    
      OPEN arc FOR 
      SELECT 'There are '|| COUNT(*) ||' students.' AS col
        FROM STUDENT;
    
    END;
    

    双管道 (||) 是 Oracle(现在是 ANSI 标准)连接字符串的方法。 Oracle 会将整数值隐式转换为字符串。

    【讨论】:

    • @DatabaseDummy:“隐藏”是什么意思?您应该使用列名作为参考,而不是结果集中的位置,因为位置可能会改变。列名也可以更改,但不那么脆弱。
    • @DatabaseDummy:我更新了答案以定义列别名“col”。您可以将别名更改为您选择的其他 ASCII 字符,否则使用双引号可以使用特殊字符(我不建议这样做)。
    猜你喜欢
    • 2012-05-10
    • 2017-07-19
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多