在做一个论坛时,需要取出论坛中某个板块的相关信息,同时要取得该板块的版主信息(姓名及id),但版主可能是多个,不便于使用直接关联的手段来实现,那样可能导致数据量大,而且会增加程序的复杂度。
后考虑一种变通的手段,可以利用一个function来取得所有版主信息,将他们拼合为一个字符串,在取得板块信息时,在select语句中加入一个function ,从而得到相应的结果。
以下是部分的例子:
———-利用function 取得版主信息
function getowneroftheme(p_themeid number) return varchar2
is
tempstr varchar2(300);
tempcur tcur;
vuserid varchar2(20);
vusername varchar2(20);
begin
open tempcur for select a.userid,a.username
from home_user a,home_forumowner b
where a.userid=b.userid and b.themeid=p_themeid;
loop
fetch tempcur into vuserid,vusername;
exit when tempcur%notfound or tempcur%notfound is null;
tempstr:=tempstr || vuserid ||:||vusername||,;
end loop;
close tempcur;
return tempstr;
end;
—在调用的存储过程中,在select子句中调用function的值
procedure()
is
begin
select themeid,getowneroftheme(themeid) owner
from home_forumtheme order by themeid;
end;
