SQL小提琴输出错误

时间:2013-10-02 15:43:50

标签: oracle plsql sqlfiddle

其实我是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小提琴无法显示dbms_output的一些问题?

2 个答案:

答案 0 :(得分:12)

您需要模仿dbms_output.put_line:)

架构:

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

答案 1 :(得分: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);

Demo。并查看dbms_output documentation,了解其get_lines程序的功能及其与put_lines来电的关系。

但仅仅因为你可以做某事,并不意味着你必须 。这很尴尬,不能扩展,但也没有尝试通过SQL Fiddle学习PL / SQL。

我要求Ben建议让你自己的数据库使用,但我建议你看一下你可以在VirtualBox中运行的pre-built VM image,这可以节省你很多时间在设置中 - 你不必担心如何安装Oracle软件或创建和配置数据库,只需准备好使用,如果出现问题,您可以将其丢弃或轻松重新启动。