-----------------基本结构---------------------
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER, --可选参数,in表示传入,out表示输出
参数2 IN NUMBER
)AS --is 或 as
变量1 INTEGER := 0; --定义变量
变量2 DATE;
BEGIN
--存储过程的执行体
NULL;
END 存储过程名字
----------WHILE循环实例---------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
AS
TOTAL NUMBER(10);
COUNTER NUMBER(10) := 0; --计数器
BEGIN
SELECT COUNT(*) INTO TOTAL FROM TEMP_YXX_1129_2;
DBMS_OUTPUT.PUT_LINE('数量:' || TOTAL);
WHILE COUNTER < 10 LOOP
DBMS_OUTPUT.PUT_LINE('循环:' || COUNTER);
COUNTER := COUNTER + 1;
END LOOP;
END PROCEDURE_TEST;
--DROP PROCEDURE PROCEDURE_TEST
-------------FOR IN循环实例----------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
AS
TOTAL NUMBER(10);
COUNTER NUMBER(10) := 0; --计数器
CURSOR CUR IS SELECT * FROM TEMP_YXX_1129_2; --游标的结果不是单个列
BEGIN
SELECT COUNT(*) INTO TOTAL FROM TEMP_YXX_1129_2;
DBMS_OUTPUT.PUT_LINE('数量:' || TOTAL);
FOR CUR_RESULT IN CUR LOOP
BEGIN --这个BEGIN END可选
DBMS_OUTPUT.PUT_LINE('FOR IN:' || CUR_RESULT.NAME);
END; --可选
END LOOP;
END PROCEDURE_TEST;
--DROP PROCEDURE PROCEDURE_TEST
-------------- 带输入参数的存储过程----------------
<<参数的数据类型不能指定长度>>
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
(COU IN NUMBER)
AS
TOTAL NUMBER(10) := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('参数:' || COU);
END PROCEDURE_TEST;
--调用存储过程--
BEGIN
PROCEDURE_TEST(5);
END;
--调用存储过程2--
DECLARE
NUMB NUMBER(10);
BEGIN
NUMB := 8;
PROCEDURE_TEST(NUMB);
END;
----------有输入输出参数的返回值---------------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
(CIN IN NUMBER,COUT OUT NUMBER)
AS
BEGIN
--将表中的AGE字段赋值给COUT
SELECT AGE INTO COUT FROM TEMP_YXX_1129_2 WHERE ID = CIN;
DBMS_OUTPUT.PUT_LINE('参数:' || CIN);
DBMS_OUTPUT.PUT_LINE('存储过程里返回值:' || COUT);
END PROCEDURE_TEST;
--调用存储过程--
DECLARE
NUMB NUMBER(10);
BEGIN
PROCEDURE_TEST(1,NUMB);
DBMS_OUTPUT.PUT_LINE('调用存储过程的返回值:'||NUMB);
END;
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER, --可选参数,in表示传入,out表示输出
参数2 IN NUMBER
)AS --is 或 as
变量1 INTEGER := 0; --定义变量
变量2 DATE;
BEGIN
--存储过程的执行体
NULL;
END 存储过程名字
----------WHILE循环实例---------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
AS
TOTAL NUMBER(10);
COUNTER NUMBER(10) := 0; --计数器
BEGIN
SELECT COUNT(*) INTO TOTAL FROM TEMP_YXX_1129_2;
DBMS_OUTPUT.PUT_LINE('数量:' || TOTAL);
WHILE COUNTER < 10 LOOP
DBMS_OUTPUT.PUT_LINE('循环:' || COUNTER);
COUNTER := COUNTER + 1;
END LOOP;
END PROCEDURE_TEST;
--DROP PROCEDURE PROCEDURE_TEST
-------------FOR IN循环实例----------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
AS
TOTAL NUMBER(10);
COUNTER NUMBER(10) := 0; --计数器
CURSOR CUR IS SELECT * FROM TEMP_YXX_1129_2; --游标的结果不是单个列
BEGIN
SELECT COUNT(*) INTO TOTAL FROM TEMP_YXX_1129_2;
DBMS_OUTPUT.PUT_LINE('数量:' || TOTAL);
FOR CUR_RESULT IN CUR LOOP
BEGIN --这个BEGIN END可选
DBMS_OUTPUT.PUT_LINE('FOR IN:' || CUR_RESULT.NAME);
END; --可选
END LOOP;
END PROCEDURE_TEST;
--DROP PROCEDURE PROCEDURE_TEST
-------------- 带输入参数的存储过程----------------
<<参数的数据类型不能指定长度>>
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
(COU IN NUMBER)
AS
TOTAL NUMBER(10) := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('参数:' || COU);
END PROCEDURE_TEST;
--调用存储过程--
BEGIN
PROCEDURE_TEST(5);
END;
--调用存储过程2--
DECLARE
NUMB NUMBER(10);
BEGIN
NUMB := 8;
PROCEDURE_TEST(NUMB);
END;
----------有输入输出参数的返回值---------------------
CREATE OR REPLACE PROCEDURE PROCEDURE_TEST
(CIN IN NUMBER,COUT OUT NUMBER)
AS
BEGIN
--将表中的AGE字段赋值给COUT
SELECT AGE INTO COUT FROM TEMP_YXX_1129_2 WHERE ID = CIN;
DBMS_OUTPUT.PUT_LINE('参数:' || CIN);
DBMS_OUTPUT.PUT_LINE('存储过程里返回值:' || COUT);
END PROCEDURE_TEST;
--调用存储过程--
DECLARE
NUMB NUMBER(10);
BEGIN
PROCEDURE_TEST(1,NUMB);
DBMS_OUTPUT.PUT_LINE('调用存储过程的返回值:'||NUMB);
END;