The basic syntax oracle stored procedure
Create or Replace Procedure Proc1 (
p_para1 VARCHAR2,
p_para2 OUT VARCHAR2,
p_para3 in OUT VARCHAR2
) AS
v_name VARCHAR2 (20 is);
the begin
v_name: = 'Chi Master';
p_para3: = v_name;
DBMS_OUTPUT.PUT_LINE ( 'p_para3 : '|| p_para3);
End;
Note: create a statement: create or replace procedure stored procedure name, or replace if there is no statement, then just create a stored procedure. If the stored procedure is present, an error is reported. Create or replace procedure if the system does not have this stored procedure to create a new one, if there is this system stored procedure to put the original deleted, re-create a stored procedure.
Stored procedure name is defined: including stored procedure name and parameter list. Parameter name and parameter types. Parameter name can not be repeated, parameter passing mode: the IN, OUT, the IN OUT
the IN represents an input parameter passed by value manner.
OUT represents an output parameter, as will be appreciated by reference transfer mode. The output can be used as stored procedure for external callers to use.
IN OUT can be input parameters, output parameters can be used.
Data type parameter only need to specify the type name, do not need to specify the width.
Width parameter is determined by the external caller.
Process parameters can be no argument
variable declaration block: followed by the as (is) keywords, keywords can be understood as declare pl / sql for declaring variables.
Variable declaration block is used to declare variables need to use the stored procedure, its scope for the stored procedure. Also here variable declarations must be specified width. Variable follow PL / SQL statement norms.
Process statement block: begin from the beginning to the keyword statement block process. DETAILED logical storage procedure here achieved.
Exception Handling block: keyword exception, an exception is generated by processing the statement. This portion is optional
end block: results of a keyword from the end.
Example:
Replace the SP_NAME Procedure or Create (
- the parameters, the parameter lists, separated by commas.
UID in VARCHAR2, - with length information can not
startDate in date, - the second input parameter
defaultVar in varchar2 default "", - default parameters , if not pass, to be noted that the order of parameters
isok out number, - output parameters
result out varchar2 - a second output parameter
)
AS
- variable declarations, statements each end with a semicolon, the statement may also initializes
var1 VARCHAR2 (. 11);
var2 Number (2): = 123;
the begin
- with string concatenation ||
DBMS_OUTPUT.PUT_LINE ( 'IsOK:' || 'ABC');
- call another procedure stored
sub_sp_name (param1, prarm2, outParam1, outParam2);
end; - stored procedures ended
---------------------
--1. Definition statement block
DECLARE - declare variables var1 number (2); - declare only var2 char (2): = ' var2'; - initialization statement while the begin - statements end; - End statement block
--2. If statement
if a = 1 or b = 2 then elsif c = 3 then else end if;
--3. Case statement branches control
dbms_output.put_line where () is the output of the method, the need to enter the set serveroutput on in this prior dos; will output a corresponding information
DECLARE NUM Number (10): =. 11; the begin Case When the then DBMS_OUTPUT.PUT_LINE NUM = 10 ( 'I 10'); When the then NUM = DBMS_OUTPUT.PUT_LINE. 11 ( 'I. 11'); the else DBMS_OUTPUT.PUT_LINE ( ' I do not know who I am '); End Case; Case NUM the when the then dbms_output.put_line 0 (' I'm 10 '); the when the then dbms_output.put_line 1 (' I'm 11 '); the else dbms_output.put_line (' I I do not know who I am '); End Case; End;
--4. For loop
--for cycle has two main uses.
--4.1, a range of circulating DECLARE I Number (2); the begin for I in. 9 .. 0 Loop DBMS_OUTPUT.PUT_LINE ( 'I:' || I); End Loop; End; - 4.2, implicit cursor traversing - - benefits implicit cursor is not required to manually closed, to facilitate the BEGIN the FOR Re the iN (the FROM UserInfo the SELECT username) LOOP the DBMS_OUTPUT.PUT_LINE (re.username); the END LOOP; the END;
Second, using the following stored procedure:
I am here to create a table userinfo,
-- Create table创建表 create table userinfo ( id varchar2(32) not null, username varchar2(20), password varchar2(20), sex varchar2(2), sal number(20), insertdate date default sysdate ) ; -- Add comments to the columns comment on column userinfo.id is 'id'; comment on column userinfo.username is '用户名'; comment on column userinfo.password is '密码'; comment on column userinfo.sex is '性别'; comment on column userinfo.sal is '工资'; comment on column userinfo.insertdate is '插入时间';
2.1 Create a stored procedure and insert the data call
OR the REPLACE the CREATE - Create a stored procedure, if updated, created does not exist, a data insertion Procedure proc_test (The e_id in VARCHAR, e_username in VARCHAR, e_password in VARCHAR, e_sex in VARCHAR, e_sal in Number) - where the arguments not defined length, or an error IS the begin INSERT INTO UserInfo (ID, username, password, Sex, SAL) values (The e_id, e_username, e_password, e_sex, e_sal); End; - call. 1 the DECLARE ID VARCHAR (32); username VARCHAR (20 is); password VARCHAR (20 is); Sex VARCHAR (2); SAL Number (20 is); the BEGIN ID: = 'asdfasdfa'; username: = 'text11'; password: = 'PAS'; Sex: = 'M'; SAL: = 2000; proc_test (The e_id => ID, e_username => username, e_password => password, = e_sex> Sex, e_sal => SAL); the commit; the END; SELECT * from UserInfo; - see the data successfully inserted
---调用2 begin proc_test(e_id => 'dd', e_username => 'A',e_password => 'aa', e_sex => 'd',e_sal => 1000); commit; end; --调用3 call proc_test(e_id => 'dd', e_username => 'A',e_password => 'aa', e_sex => 'd',e_sal => 1000); commit;
java in the call:
public static void insert() throws ClassNotFoundException, SQLException { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String name = "user2"; String pwd = "user2"; String Drivername = "oracle.jdbc.driver.OracleDriver"; Class.forName(Drivername); Connection connection = DriverManager.getConnection(url, name, pwd); CallableStatement call = connection .prepareCall("call PROc_TEST(e_id => 'dd', e_username => 'A',e_password => 'aa', e_sex => 'd',e_sal => 1000)"); // 执行存储过程 boolean b = call.execute(); if (b = true) { System.out.println("插入成功!"); } connection.close(); }
- 2. create with the return parameter
--in the input parameters, out of the output parameter is the CREATE OR Procedure proc_test1 the REPLACE (T in VARCHAR, R & lt OUT VARCHAR) AS the begin SELECT COUNT (*) INTO from R & lt UserInfo T WHERE username like; End; - call, - multi-line statement is executed '/' indicates the end of the dos in the DECLARE outobj VARCHAR2 (. 4); the BEGIN proc_test1 (T => 'A', R & lt => outobj); DBMS_OUTPUT.PUT_LINE ( 'outobj =' || outobj ); the END;
java in the call:
+ Test print); }+ testPrint); conn.close();
- 3. Create a query with results
- 3. As the oracle stored procedure does not return a value, it's all the return values are replaced by out parameters, the list is no exception, but because it is a collection, so you can not use the general parameters, you must use the pagkage. so it is divided into two parts, --3.1, to build a package. As follows: Create or Replace Package testpackage AS type test_cursor REF IS Cursor; End; --3.2, build stored procedures, stored procedure: Create or Replace Procedure TESTC (p_cursor OUT testpackage.test_cursor) IS the begin Open p_cursor for SELECT * from UserInfo; end;
java in the call:
static void SELECT public () throws a ClassNotFoundException, SQLException { String URL = "JDBC: Oracle: Thin: @localhost: 1521: ORCL"; String name = "user2"; String pwd = "user2"; String Drivername = "the oracle.jdbc .driver.OracleDriver "; Class.forName (Drivername); Connection conn = DriverManager.getConnection (url, name, pwd); CallableStatement proc = conn.prepareCall (" Call TESTC () "?); // stored procedure proc.registerOutParameter (1, oracle.jdbc.OracleTypes.CURSOR); // set the output parameter is a parameter of the first cursor, cursor type. proc.execute (); // perform ResultSet rs = (ResultSet) proc.getObject ( 1); // get the first parameter is a cursor, converted into ResultSet type while (rs.next ()) // get the data { System.out.println("id:" + rs.getString("id") + "\tusername:" + rs.getString("username") + "\tinsertdate:" + rs.getString("insertdate")); } }
4. Create a stored procedure can call other stored procedures
-------------------------- 3. Examples create or replace procedure sp_name (defaultVar in varchar2 default 'A') - the default parameters, if not pass, to pay attention to the order parameter AS - variable declarations, each declaration ends with a semicolon. A statement that can be initialized robj VARCHAR2 (. 4); var2 Number (20 is):; = 123 the begin - with string concatenation || DBMS_OUTPUT.PUT_LINE ( 'IsOK:' || 'ABC'); - calls to other storage process proc_test1 (T => 'A', R & lt => robj); DBMS_OUTPUT.PUT_LINE ( 'R & lt =' || R & lt); end; - storing process ends - calling the begin the SP_NAME ( 'A'); end;