oracle - 我可以写这种类型的程序吗?
问题描述
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 的信息..
解决方案
正如其他人所说,很难理解你想要什么,但我相信你想要的是下面的内容,完全未经测试的代码,所以可能有一些语法错误,但至少给你另一个探索的想法。
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;
推荐阅读
- java - 用递归方法计算可以被2除的元素
- php - 与雄辩模型相关的“哪里”
- c# - 脚 IK 光线投射碰到边缘,不知道使用什么旋转
- javascript - 带有命名空间的意外标识符
- reactjs - 这是避免“你在提前返回后不小心调用了 React Hook 吗?”的安全方法吗?
- javascript - 如何嵌入带有 swf 文件的文件夹?
- html - 下载属性html
- ms-access - 即使输入框已链接,参数输入也会显示
- php - 我为我的域设置了一个全局 cookie,当用户导航到一个页面时,我想检查该 cookie 是否存在;如果没有,那么它会给他们一个 404
- shell - awk:将一个字段拆分为一列中的多行