欢迎光临
我们一直在努力

通过function解决部分复杂查询的方法-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

在做一个论坛时,需要取出论坛中某个板块的相关信息,同时要取得该板块的版主信息(姓名及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;

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 通过function解决部分复杂查询的方法-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章