16. 通过内部函数提高sql效率.
select h.empno,e.ename,h.hist_type,t.type_desc,count(*)
from history_type t,emp e,emp_history h
where h.empno = e.empno
and h.hist_type = t.hist_type
group by h.empno,e.ename,h.hist_type,t.type_desc;
通过调用下面的函数可以提高效率.
function lookup_hist_type(typ in number) return varchar2
as
tdesc varchar2(30);
cursor c1 is
select type_desc
from history_type
where hist_type = typ;
begin
open c1;
fetch c1 into tdesc;
close c1;
return (nvl(tdesc,’?’));
end;
function lookup_emp(emp in number) return varchar2
as
ename varchar2(30);
cursor c1 is
select ename
from emp
where empno=emp;
begin
open c1;
fetch c1 into ename;
close c1;
return (nvl(ename,’?’));
end;
select h.empno,lookup_emp(h.empno),
h.hist_type,lookup_hist_type(h.hist_type),count(*)
from emp_history h
group by h.empno , h.hist_type;
(译者按: 经常在论坛中看到如 ’能不能用一个sql写出….’ 的贴子, 殊不知复杂的sql往往牺牲了执行效率. 能够掌握上面的运用函数解决问题的方法在实际工作中是非常有意义的)
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器