首页 > 解决方案 > 我可以写这种类型的程序吗?

问题描述

Create or replace procedure p1(
      name in varchar2
)
Is
  Details1 emp%rowtype;
  Details2 dept%rowtype;
Begin
  If name in (select ename from emp)
  Then
    Select * into details1 from emp 
    where ename=name;
    dbms_output.put_line(details1.deptno||' 
    '||details1.ename);
  End if;
  If name in (select dname from dept)
  Then
    Select * into details2 from dept 
    where dname=name;
    dbms_output.put_line(details2.deptno||' 
    '||details2.dname);
  End if;
End;

如果我称它为:

Exec p1('BLAKE')

我想要像这样的输出:

Deptno          Ename
-----------    ------------
20              BLAKE

如果我称之为:

Exec p1('Sales')

我想要像这样的输出:

Deptno          Dname
-----------    ------------
30              SALES

在我的过程中,我接受参数变量,如果该名称存在于我的表列中,则通过传递该变量的任何名称,然后我想要该表中的数据。

我举一个例子:当我们在 google 中写 ronaldo 时,有一个关于 ronaldo 的信息来自足球桌(假设足球是 table 并且足球桌中存在 ronaldo 的名字)。当我们在 google 中写 obama 时,有一条来自美国总统表的有关 obama 的信息..

标签: oracleplsql

解决方案


正如其他人所说,很难理解你想要什么,但我相信你想要的是下面的内容,完全未经测试的代码,所以可能有一些语法错误,但至少给你另一个探索的想法。

Create or replace procedure p1(name in varchar2) Is

  V_name varchar2(1000) default null;
  v_dept varchar2(100) default null;
Begin

  begin
    select ename, deptno
    into v_name, v_dept
    from emp
    where upper(trim(name)) = upper(trim(v_name));
  exception 
    when no_data_found then
       v_name = null;
       v_dept = null; 
    when to_many_rows then
       -- handle this however you need
  end;    

  If v_name is not null then
    dbms_output.put_line(v_deptno||' '||v_name);
  else
    begin
      select dname, deptno
      into v_name, v_dept
      from dept 
      where upper(trim(name)) = upper(trim(v_name));
    exception 
      when no_data_found then
         v_name = null;
         v_dept = null; 
    when to_many_rows then
       -- handle this however you need
    end;     
    If v_name is not null then
      dbms_output.put_line(v_dept||' '||v_name);
    End if;
  end if;
End;

推荐阅读