5

实际上我对 PL/SQL 很陌生

我在 SQL Fiddle 中使用 oracle pl/sql 创建了下表

create table Employee(name varchar2(100),id integer, salary integer,PRIMARY KEY(id));
insert into Employee(name,id,salary) values('sa',94,100);
insert into Employee(name,id,salary) values('pr',88,150);
insert into Employee(name,id,salary) values('ji',33,900);
insert into Employee(name,id,salary) values('na',24,880);
insert into Employee(name,id,salary) values('po',65,770);
insert into Employee(name,id,salary) values('ri',69,910);
insert into Employee(name,id,salary) values('uj',12,650);
insert into Employee(name,id,salary) values('ad',43,440);
insert into Employee(name,id,salary) values('sam',40,550);

我执行了以下查询

 DECLARE
 employee_record Employee%ROWTYPE;
 BEGIN
 select * into employee_record from Employee where id>90;
 dbms_output.put_line(employee_record.name||' '||employee_record.id||' '||employee_record.salary);
 END;
 /

我得到以下输出

Record Count: 0; Execution Time: 2ms 

它应该打印员工记录中存在的值,对吗?我的 sql 查询有问题还是 sql fiddle 无法显示 dbms_output 有问题?

4

2 回答 2

12

You need to emulate dbms_output.put_line :)

Schema:

create table Employee(
  name varchar2(100),
  id integer, 
  salary integer,
  PRIMARY KEY(id)
);

insert into Employee(name,id,salary) values('sa',94,100);
insert into Employee(name,id,salary) values('pr',88,150);
insert into Employee(name,id,salary) values('ji',33,900);
insert into Employee(name,id,salary) values('na',24,880);
insert into Employee(name,id,salary) values('po',65,770);
insert into Employee(name,id,salary) values('ri',69,910);
insert into Employee(name,id,salary) values('uj',12,650);
insert into Employee(name,id,salary) values('ad',43,440);
insert into Employee(name,id,salary) values('sam',40,550);

create table dbmsoutput (
  pos int,
  mes varchar2(4000)
);

SQL:

DECLARE
  employee_record Employee%ROWTYPE;
  procedure put_line(p_mes in varchar2) is
     v_pos int; 
  begin  
     select count(0) into v_pos from dbmsoutput;  
     insert into dbmsoutput (pos, mes) values (v_pos, p_mes);
  end;
BEGIN
 put_line('Hello!  This code is powered by dbms_output emulator :)');
 -- Your code here:
 select * into employee_record from Employee where id>90;
 put_line(employee_record.name||' '||employee_record.id||' '||employee_record.salary);
 --
 put_line('Bye!');
END;
/


SELECT mes FROM dbmsoutput order by pos

fiddle

于 2013-10-02T17:25:54.377 回答
5

出于好奇,您可以从 SQL Fiddle获得有限dbms_output的结果,但是您需要一个函数来提取缓冲的行并以您可以选择的形式返回它们。这使用流水线表:

create type t_lines as table of varchar2(4000)
/

create or replace function get_lines
return t_lines pipelined is
  lines dbms_output.chararr;
  numlines integer;
begin
  numlines := 999;
  dbms_output.get_lines(lines, numlines);
  if numlines > 0 then
    for i in 1..numlines loop
      pipe row (lines(i));
    end loop;
  else
    pipe row ('No data');
  end if;
end;
/

然后,在您发出dbms_output.put_line呼叫之后:

select * from table(get_lines);

演示。并查看dbms_output文档以了解其get_lines过程的作用以及它与您的put_lines调用的关系。

但仅仅因为你可以做某事,并不意味着你一定应该这样做。这很尴尬并且无法扩展,但是尝试通过 SQL Fiddle 学习 PL/SQL 也不是真的。

我赞同 Ben 的建议,即使用您自己的数据库,但我建议您查看可以在 VirtualBox 中运行的预构建 VM 映像,这可以为您节省大量设置时间 - 您不需要不必担心如何安装 Oracle 软件或创建和配置数据库,它刚刚可以使用,如果出现问题,您可以将其丢弃或轻松重新开始。

于 2013-10-02T17:25:10.933 回答