oracle&sql server 存储过程开发日记

sqlserver

ALTER PROCEDURE [dbo].[proc_get_producer_id]
 -- Add the parameters for the stored procedure here
    @userid varchar(50),
    @producerid  varchar(50) output
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 select @producerid = P_Id from T_Producer tp inner join sys_orgmenber so
 on tp.OrgName= so.org
 where   member in(select username from sys_user  where userid = @userid )  --rolename = '加工商' and

END

 

oracle

create or replace procedure proc_get_producer_id
(vi_userid  in varchar2,
 vo_producerid out varchar2
) is
begin  
   select c.company_id into vo_producerid from t_ebook_company c where c.company_id =
   (select u.company_id  from t_wblr_user u where u.user_id =  vi_userid);
 
end proc_get_producer_id;

 

Oracle 存储过程返回结果集

过程返回记录集:

CREATE OR REPLACE PACKAGE pkg_test

AS

    TYPE myrctype IS REF CURSOR;

 

    PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);

END pkg_test;

/

 

CREATE OR REPLACE PACKAGE BODY pkg_test

AS

    PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)

    IS

       sqlstr   VARCHAR2 (500);

    BEGIN

       IF p_id = 0 THEN

          OPEN p_rc FOR

             SELECT ID, NAME, sex, address, postcode, birthday

               FROM student;

       ELSE

          sqlstr :=

             'select id,name,sex,address,postcode,birthday

            from student where id=:w_id';

          OPEN p_rc FOR sqlstr USING p_id;

       END IF;

    END get;

END pkg_test;

/

 

函数返回记录集:

建立带ref cursor定义的包和包体及函数:

CREATE OR REPLACE

package pkg_test as

/* 定义ref cursor类型

    不加return类型,为弱类型,允许动态sql查询,

    否则为强类型,无法使用动态sql查询;

*/

   type myrctype is ref cursor; 

  

--函数申明

   function get(intID number) return myrctype;

end pkg_test;

/

  

CREATE OR REPLACE

package body pkg_test as

--函数体

    function get(intID number) return myrctype is

      rc myrctype;  --定义ref cursor变量

      sqlstr varchar2(500);

    begin

      if intID=0 then

         --静态测试,直接用select语句直接返回结果

         open rc for select id,name,sex,address,postcode,birthday from student;

      else

         --动态sql赋值,用:w_id来申明该变量从外部获得

         sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';

         --动态测试,用sqlstr字符串返回结果,用using关键词传递参数

         open rc for sqlstr using intid;

      end if;

  

      return rc;

    end get;

  

end pkg_test;

/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lb8607

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值