一個簡單的存儲過程


創建一個簡單的存儲過程
其中涉及到是基本的傳參,定義參數,參數賦值,條件語句,循環語句,用游標遍歷等基本語法

    --創建一個存儲過程 
    create or replace procedure addgoods(    -- 傳入兩個變量,一個輸入變量,一個輸出變量,輸出變量用來作為返回值
            gname1 user_table.user_skey%type , param1 out user_table.user_skey%type ) as   -- 把變量的類型跟表中字段類型保持一致
          id1 number(10);   -- 定義兩個沒有初始化的變量
          i number(10) ;
          cursor ee(v_user_skey Number) is select user_skey from user_table  where user_skey = v_user_skey; --游標 ,帶參數 游標的參數一定不能跟表中的字段一樣,且不區分大小寫
          begin   -- 代碼處理塊 , 用begin end包起來

        begin    -- 處理異常  要把需要處理的代碼塊用BEGIN END包起來,在代碼塊中緊跟EXCEPTION處理
      select user_table.user_skey into id1 from user_table where user_table.user_skey = gname1;
      Exception           
      WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('不存在任何記錄');  -- db的輸出語句,在output標簽下顯示輸出信息
        ROLLBACK;
 	    RAISE;
      END;

   if id1 = 45 then   -- 判斷語句
      begin
        dbms_output.put_line(gname1 || '的id是45');
      end;
   end if;

   if id1 <> 45 then
     begin
      dbms_output.put_line(gname1 || '的id是'||id1);
     end;  
   end if;

   param1:= id1;  -- 變量賦值

   i:= 1;
   while i < 10 loop   -- while 循環
     begin
         dbms_output.put_line('i的值是:' || i);
         i:= i + 1;
     end;
   end loop;

   i:= 1;
   for vvv in ee(999) loop  -- for循環,使用游標
   
       dbms_output.put_line('i的值為:'  || i);
       i:= i + 1;
       dbms_output.put_line('存在的id值有:' || vvv.user_skey); -- 這一行必須有,循環體不能為空
    
   end loop;

   dbms_output.put_line('111111');

end addgoods;



-- 調用存儲過程
begin
     addgoods(999);
end;

-- 使用代碼塊調用存儲過程   只有這個調用有效,輸出參數必須是一個變量
declare
     user_skey number;
     user_id number ;
 begin
   user_skey:= 999;
   addgoods(user_skey,user_id);  -- 傳入兩個參數,一個輸入參數,一個輸出參數,輸入參數必須初始化,輸出參數是否初始化無所謂
   dbms_output.put_line('返回的值是:' || user_id);
 end;

 -- 在sqlcommand中運行
 exec addgoods(user_skey);

create or replace procedure addgoods(
gname1 user_table.user_skey%type) as 
  id1 number(10);
  i number(10) ;
  cursor ee(v_user_skey Number) is select user_skey from user_table  where user_skey = v_user_skey; --游標 ,帶參數
  begin

    begin    -- 處理異常  要把需要處理的代碼塊用BEGIN END包起來,在代碼塊中緊跟EXCEPTION處理
      select user_table.user_skey into id1 from user_table where user_table.user_skey = gname1;
        Exception 
      
      WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('不存在任何記錄');
        ROLLBACK;
 	      RAISE;
    END;

   if id1 = 45 then
      begin
        dbms_output.put_line(gname1 || '的id是45');
      end;
   end if;

   if id1 <> 45 then
     begin
          dbms_output.put_line(gname1 || '的id是'||id1);
     end;  
   end if;
   i:= 1;
   while i < 10 loop
     begin
         dbms_output.put_line('i的值是:' || i);
         i:= i + 1;
     end;
   end loop;
   i:= 1;
   for vvv in ee(999) loop
   
       dbms_output.put_line('i的值為:'  || i);
       i:= i + 1;
       dbms_output.put_line('存在的id值有:' || vvv.user_skey); -- 這一行必須有,循環體不能為空
    
   end loop;

   dbms_output.put_line('111111');

end addgoods;



-- 調用存儲過程
begin
     addgoods(999);
end;

-- 使用代碼塊調用存儲過程
declare
     user_skey number;
     user_id number ;
 begin
   user_skey:= 999;
   addgoods(user_skey);
 end;

 -- 在sqlcommand中運行
 exec addgoods(user_skey);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM