To obtain a data set from Oracle Procedure, the Ref Cursor method is usually used. To obtain this Cursor, there are several ways:
1. dynamic SQL return:
In this case, Procedure operations are usually relatively simple. For example, you can use a combination of SQL statements or Join operations on multiple tables, but you can use one SQL statement to complete the query.
- Create or replace procedure sp_getcurrentstockpallet (
- Startdate DATE,
- Enddate DATE,
- Status CHAR,
- Material_no VARCHAR2,
- Pallet_id VARCHAR2,
- Box_id VARCHAR2,
- Plant VARCHAR2,
- Stloc VARCHAR2,
- Customer VARCHAR2,
- Creator VARCHAR2,
- Mat_doc VARCHAR2,
- Box_count NUMBER,
- Result out sys_refcursor
- )
- IS
- V_ SQL VARCHAR2 (1000 );
- BEGIN
- V_ SQL: =
- 'Select B. status, B. pallet_id, B. wm_pallet_id, count (B. box_id) box_count, sum (B. glass_qty) total_qty, B. unit, B. material_no,
- B. grade, a. plant, a. stloc, a. area, a. bin, B. customer, B. product_type, B. CREATE_TIME, B. remark
- From sd_current_pallet a, sd_current_box B
- Where a. pallet_id = B. pallet_id ';
- IF material_no IS NOT NULL
- THEN
- V_ SQL: = v_ SQL |'And B. material_no ='|''''| Material_no |'''';
- End if;
- V_ SQL: =
- V_ SQL
- | 'Group by B. status, B. pallet_id, B. wm_pallet_id, B. unit, B. material_no,
- B. grade, a. plant, a. stloc, a. area, a. bin, B. customer, B. product_type, B. CREATE_TIME, B. remark ';
- Open result for v_ SQL;
- EXCEPTION
- WHEN NO_DATA_FOUND
- THEN
- NULL;
- WHEN OTHERS
- THEN
- RAISE;
- END sp_getcurrentstockpallet;
2. If the Procedure logic operation is complicated, it cannot be completed in one SQL statement. In general, intermediate calculation data needs to be temporarily stored in the operation process.
In this case, you can use the following methods:
1) Use a nested Table to dynamically generate a dataset and use the Table () function to return the dataset. In this way, you need to create an Object in the database and create a nested table,
The Type defined in the local scope cannot be recognized.
Create an Object.
- Create or replace type stockPallet_typeAs Object
- (
- Status CHAR (1 ),
- Material_no VARCHAR2 (20 ),
- Pallet_id VARCHAR2 (30 ),
- Box_id VARCHAR2 (30 ),
- Plant VARCHAR2 (4 ),
- Stloc VARCHAR2 (4 ),
- Customer VARCHAR2 (10 ),
- Creator VARCHAR2 (10 ),
- Mat_doc VARCHAR2 (20 ),
- Box_count NUMBER (10)
- )
Create a nested table with the Object stockPallet_type created above
- Create or replace type. t_stockpallet_nestAsTable of stockpallet_type;