Procedure within a cursor
I have a procedure within a cursor.
How do I assign the "termination_date" calculated in the cursor to the "v_end_date" and pass it into the procedure?
declare
cursor x is select a.store_no || '-' || b.store_name name,
a.store_no,
landlord_turnover_date execution_date,
open_date commencement_date,
decode(
decode(sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), 1, option1_end_date,
decode(sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), 1, option2_end_date,
decode(sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), 1, option3_end_date,
decode(sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), 1, option4_end_date,
decode(sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), 1, option5_end_date,
decode(sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), 1, option6_end_date)))))),null,
nvl(option6_end_date,nvl(option5_end_date,nvl(option5_end_date,nvl(option4_end_date,nvl(option3_end_date,nvl(option2_end_date,option1_end_date)))))),
decode(sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), 1, option1_end_date,
decode(sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), 1, option2_end_date,
decode(sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), 1, option3_end_date,
decode(sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), 1, option4_end_date,
decode(sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), 1, option5_end_date,
decode(sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), 1, option6_end_date))))))) termination_date,
option1_base_rent,
option1_end_date,
option2_base_rent,
option2_end_date,
option3_base_rent,
option3_end_date,
option4_base_rent,
option4_end_date,
option5_base_rent,
option5_end_date,
option6_base_rent,
option6_end_date,
c.vendor_number,
d.vendor_id,
e.vendor_site_id,
cost_center
from lease_terms a, store b, workrent c, po_vendors d, po_vendor_sites_all e
where flag = 'Y'
and a.store_no = b.store
and a.store_no = c.store_no
and c.rent_amt_5301 > 0
and c.vendor_number = d.segment1
and d.vendor_id = e.vendor_id
and c.pay_site_name = e.vendor_site_code;
v_loc_id number;
v_norm varchar2(1);
v_term_id number;
v_lease_id number;
v_change_id number;
v_div varchar2(2);
v_exp_id number;
v_acc_id number;
PROCEDURE insert_payment ( v_term_id IN number,
v_lease_id IN number,
v_change_id IN number,
v_start_date IN date,
v_end_date IN date,
v_vendor_id IN number,
v_vendor_site_id IN number,
v_amt IN number,
v_normalize IN varchar2,
v_expense_id IN NUMBER,
v_lia_id IN NUMBER)
IS
BEGIN
insert into pn_payment_terms_all (
payment_term_id,
last_update_date,
last_updated_by,
creation_date,
created_by,
last_update_login,
payment_purpose_code,
payment_term_type_code,
frequency_code,
lease_id,
lease_change_id,
start_date,
end_date,
vendor_id ,
vendor_site_id,
actual_amount,
set_of_books_id,
currency_code,
rate,
org_id,
normalize,
schedule_day,
lease_status)
values
( v_term_id,
SYSDATE,
-1,
SYSDATE,
-1,
0,
'RENT',
'BASER',
'MON',
v_lease_id,
v_change_id,
v_start_date,
v_end_date,
v_vendor_id,
v_vendor_site_id,
v_amt,
1,
'USD',
1,
101,
v_normalize,
1,
'ACT');
btw the nested DECODEs could be simplified greatly using CASE, or if this is 8i you might still be able to unnest them with something like:
decode(1,
sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), option1_end_date,
sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), option2_end_date,
sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), option3_end_date,
sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), option4_end_date,
sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), option5_end_date,
sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), option6_end_date,
NVL(...) )Unfortunately I rather lost track of it after that. You may also be able to simplify the nested NVLs using COALESCE, e.g.
coalesce
( option6_end_date,
option5_end_date,
option5_end_date,
option4_end_date,
option3_end_date,
option2_end_date,
option1_end_date ), ...although if this is 8i COALESCE may not be available.
Why they couldn't just have added more optional arguments to NVL I will never understand.
Similar Messages
-
Case tatement not working within a cursor
I am having a problem getting the case statement to work within a cursor in a store procedure in our 2008 r2 environment. Below is the cursor that I'm referring too. Any suggestions would be greatly appreciated.
SET NOCOUNT ON;
DECLARE @part_id as int, @WAREHOUSE_ID AS varchar(80), @SAFETY_STOCK_QTY AS int;
DECLARE parts_cursor CURSOR FOR
select part_id ,WAREHOUSE_ID, coalesce(SAFETY_STOCK_QTY,0) from PART_WAREHOUSE where Part_ID in (SELECT distinct #FLINES03.Part from #FLINES03)
OPEN parts_cursor
FETCH NEXT FROM parts_cursor
INTO @part_id, @WAREHOUSE_ID, @SAFETY_STOCK_QTY
WHILE @@FETCH_STATUS = 0
BEGIN
CASE @WAREHOUSE_ID
WHEN 'AAA' THEN UPDATE #FLINES03 SET WHS1_SS_QTY = @SAFETY_STOCK_QTY WHERE #FLINES03.PART = @part_id AND #FLINES03.WHS1 = @WAREHOUSE_ID
WHEN 'BBB'THEN UPDATE #FLINES03 SET WHS2_SS_QTY = @SAFETY_STOCK_QTY WHERE #FLINES03.PART = @part_id AND #FLINES03.WHS2 = @WAREHOUSE_ID
WHEN 'CCC' THEN UPDATE #FLINES03 SET WHS3_SS_QTY = @SAFETY_STOCK_QTY WHERE #FLINES03.PART = @part_id AND #FLINES03.WHS3 = @WAREHOUSE_ID
END
FETCH NEXT FROM parts_cursor INTO @part_id, @WAREHOUSE_ID, @SAFETY_STOCK_QTY
END
CLOSE parts_cursor
DEALLOCATE parts_cursorCASE is an expression, not a statement. It means you can not execute code. You need to change your code to IF statements, e.g.
IF @WAREHOUSE_ID = 'AAA'
UPDATE ....
IF
UPDATE ...
Also, what is the purpose of using cursor? Can you provide the whole picture of what you're trying to achieve?
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
Query within the Cursor loop is very slow
Hi ,
I have a stored procedure which has Select/Insert operation which is looped within the cursor. The Select uses variables from the cursor, and inserts the data into the table.
My problem is the peformance. The stored procedure executes in 2 mins if I hard code the where clause values in the Select. Instead if I use the variables obtained from the cursor, the query takes so long (more than 20 mins) to insert the same no of records into the table.
Below is the snippet of my code
create procedure sample is
declare
v_acct_month number(2);
v_sp_name varchar2(30);
cursor v_cur is Select distinct Acct_Month,salesperson_name from period;
begin
open v_cur;
loop
fetch v_cur into v_acct_month, v_sp_name;
exit when v_cur%notfound;
Insert into T1
Select * from T2,T3.. Where T2.month=v_acct_month,t3.sp_name=v_sp_name;
end loop;
End;
The query is not using the optimizer when the variables are used.The optimizer is used when I hard code the values in the select.
Please give me suggestions to make this query faster..
Thanks,
Arun
Edited by: [email protected] on Mar 28, 2009 10:18 AMHi,
Whenever you write code, format it to show the scope of BEGIN, LOOP, IF statements, and so on.
Type these 6 characters
{code}
(small letters only, inside curly brackets) before and after the formatted code, to preserve spacing on this site.
You've discovered why the tecnique you're using is called "slow-by-slow" processing.
You don't need a cursor. You may not even need PL/SQL.
Just write an INSERT statement that reference the table from the cursor and the tables from your current INSERT statement.
Here's one way:
INSERT
INTO t1 (columns)
SELECT columns
FROM t2
, t3
WHERE ...
AND (t2.month, t3.sp_name) IN (
SELECT DISTINCT
acct_month
, salesperson_name
FROM period
);Depending on the details of your case, there may be a better way, such as a 3-way join. -
Cursor within the cursor showing error
Hi,
I am using a cursor within the cursor to create a procedure.I am getting the below error while compiling this
15/7 PL/SQL: SQL Statement ignored
16/14 PL/SQL: ORA-00942: table or view does not exist
79/10 PL/SQL: Statement ignored
82/31 PLS-00364: loop index variable 'I' use is invalid
84/10 PL/SQL: Statement ignored
84/50 PLS-00364: loop index variable 'I' use is invalid
98/10 PL/SQL: Statement ignored
101/31 PLS-00364: loop index variable 'I' use is invalid
103/10 PL/SQL: Statement ignored
103/50 PLS-00364: loop index variable 'I' use is invalid
Am i using a wrong syntax,Could anyone help me on this..Please find my procedure code below
CREATE OR REPLACE PROCEDURE DM_EIS.YOY_PURGE_PRC
AS
VAR_TEMP NUMBER := 1;
VAR_TAB_NAME VARCHAR2(100);
VAR_TAB_OWNER VARCHAR2(100);
--For fetching table list to be processed
CURSOR CUR_ONE IS
SELECT OWNER,TABLE_NAME,FILTER_DATE,FILTER_VALUE
FROM DM_EIS.NONPART_PURGE_CTL WHERE NVL(STATUS,'NULL') <> 'COMPLETED'
AND PURGE_PREFERENCE='Y';
--For fetching indexs that are to be disabled
CURSOR CUR_IND IS
SELECT * FROM DBA_INDEXES WHERE TABLE_NAME = var_tab_name
AND OWNER = var_tab_owner
AND UNIQUENESS <> 'UNIQUE';
BEGIN
FOR C IN CUR_ONE
LOOP
--CHECK IF TEMP TABLE IS ALREADY PRESENT, IF SO DROP IT.
SELECT COUNT(*) INTO VAR_TEMP FROM ALL_TABLES WHERE TABLE_NAME = 'T_NOPART_PURGE';
IF VAR_TEMP = 1
THEN
EXECUTE IMMEDIATE 'DROP TABLE DM_EIS.T_NOPART_PURGE';
END IF;
---update the columns of Control table
UPDATE DM_EIS.NONPART_PURGE_CTL
SET STATUS = 'RUNNING'
WHERE TABLE_NAME = C.TABLE_NAME
AND OWNER = C.OWNER;
COMMIT;
UPDATE DM_EIS.NONPART_PURGE_CTL
SET START_TIME = SYSDATE
WHERE TABLE_NAME = C.TABLE_NAME
AND OWNER = C.OWNER;
COMMIT;
--Check whether the column value is date or year
IF C.FILTER_VALUE = 'YEAR' THEN
--Move data to temp table and truncate main table
execute immediate 'create table DM_EIS.T_NOPART_PURGE as select /*+ PARALLEL(ext,4) */ ext.* from '||c.owner||'.'|| c.table_name || ' ext where nvl(' || c.FILTER_DATE || ',''01-Feb-2009'') >= ''2009''';
DBMS_OUTPUT.PUT_LINE('Truncating the main table'||C.OWNER||'.'|| C.TABLE_NAME);
EXECUTE IMMEDIATE 'TRUNCATE TABLE ' ||C.OWNER||'.'|| C.TABLE_NAME;
else
--Move data to temp table and truncate main table
execute immediate 'create table DM_EIS.T_NOPART_PURGE as select /*+ PARALLEL(ext,4) */ ext.* from '||c.owner||'.'|| c.table_name || ' ext where nvl(' || c.FILTER_DATE || ',''01-Feb-2009'') >= ''01-Jan-2009''';
DBMS_OUTPUT.PUT_LINE('Truncating the main table'||C.OWNER||'.'|| C.TABLE_NAME);
EXECUTE IMMEDIATE 'TRUNCATE TABLE ' ||C.OWNER||'.'|| C.TABLE_NAME;
end if;
var_tab_name := c.table_name;
var_tab_owner := c.owner;
--Disable the indexes and load data back to main table,
for i in cur_ind
loop
execute immediate 'Alter index '||c.OWNER ||'.'|| i.index_name || ' unusable';
dbms_output.put_line(c.OWNER ||'.'|| i.index_name||' is unusable');
end loop;
---insert back the data into the table from temporary table
execute immediate 'insert /*Append*/ into '||c.OWNER ||'.'|| c.table_name || ' select /* PARALLEL(tmp,4) */ tmp.* from DM_EIS.T_NOPART_PURGE tmp';
commit;
--Rebuild the indexes
for i in cur_ind
loop
execute immediate 'Alter index '||c.OWNER ||'.'|| i.index_name || ' rebuild compute statistics';
dbms_output.put_line(c.OWNER ||'.'|| i.index_name||' is rebuilt');
end loop;
--Gather stats for the table
DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=>c.OWNER,
TABNAME=>c.table_name,
CASCADE=>TRUE,
ESTIMATE_PERCENT=>dbms_stats.auto_sample_size
dbms_output.put_line(c.OWNER ||'.'|| c.table_name||' is analyzed');
---update the columns of Control table
update DM_EIS.NONPART_PURGE_CTL
set status = 'COMPLETED'
where TABLE_NAME = c.TABLE_NAME
AND OWNER = C.OWNER;
commit;
UPDATE DM_EIS.NONPART_PURGE_CTL
SET END_TIME = SYSDATE
WHERE TABLE_NAME = C.TABLE_NAME
AND OWNER = C.OWNER;
COMMIT;
UPDATE DM_EIS.NONPART_PURGE_CTL
SET ELAPSED_TIME =ROUND((END_TIME-START_TIME)*1440,2)
WHERE TABLE_NAME = C.TABLE_NAME
AND OWNER = C.OWNER;
COMMIT;
UPDATE DM_EIS.NONPART_PURGE_CTL
SET LAST_UPDATED_DT = SYSDATE
WHERE TABLE_NAME = C.TABLE_NAME
AND OWNER = C.OWNER;
COMMIT;
dbms_output.put_line('Data Purging is done for '||c.OWNER ||'.'|| c.table_name);
end loop;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20003,' ERROR IN DM_EIS.NONPART_PURGE_PRC'|| SQLCODE||'-'||SQLERRM);
End;
/Oracle is not a Microsoft product. What it appears you are doing is a phenomenally efficient way to bring your database to its knees whimpering and begging that you take a class from Oracle University.
Temporary tables in Oracle are permanent tables and that is the proper design for working with the product. Creating "temp" tables using dynamic SQL in a stored procedure is multiple layers of bad practice layered on top of each other. Something made more obvious by the fact that you appear to not have read that portion of the docs that explain the differences between explicit privilege grants and grants made via roles when it comes to PL/SQL.
My recommendation is that you throw this code away and read the docs. First on the security model, roles, and object privileges. Next on the use of Global Temporary Tables. -
Using stored procedures within Crystal Reports
Hello all,
Background Information:
I am trying to teach myself how to execute a stored procedure within Crystal Reports. This is an aspect of Crystal that my work group has not utilized before and we're trying to gather information on the subject. We use Oracle to create and execute functions and procedures all the time, but I've never tried this within Crystal.
I use the "Add Command" functionality within Crystal on most of my reports so that I can taylor the sql to the report. I find this easier to do versus using the ODBC connection to the tables and writing the code through the Crystal Reports wizard. I also frequently use functions within these sql statements that are inserted in the Add Command.
What I'm trying to achieve:
I have a report that needs to run as a "trial", and then later as a committed "run". This is for a monthly billing system. Essentially, the user will run the report as the "trial", preview the data, make any necessay corrections, and then later, run the actual billing run. Within my application, the bills are not actually marked as "billed" until they are actually "billed', if that makes sense. As a result, the "trial" report will need to mark the bills as "billed", generate the report, and then rollback the data (so that the bills are not "billed". Once the actual billing reports are ran, the same report will run, but with a "commit" at the end of the report so that the bills are now "billed".
I'm trying simple tests and failing (i.e. taking baby steps to learn what capabilities Crystal has):
I created as simple of a procedure as I can envision. This procedure inserts the word "test" in one of my fields for a provided account number. When I try to run this procedure via Crystal (via New Report ->History->My ODBC Database link->Stored Procedures), Crystal asks for the account number parameter, as it should. But I then receive the error message:
Database Connector Error: '42000:[Microsoft][ODBC driver for Oracle]Syntax error or access violation'
The existing ODBC connection has work great for years and years to retrieve data from tables, but this is the first time I've tried to utilize procedures. Can anybody help? And can anybody explain what the Stored Procedures link is supposed to do? Am I going down the right path?
thanks,
NoelHello,
Make sure the Oracle client install path is in the PATH statement. And also make sure you are using an IN/OUT cursor. CR only reads the last SELECT statement. Search the Documents area on how to create a Stored Procedure.
Also, if you are using CR XI ( 11.0 ) then upgrade to CR XI R2 and apply all service packs ( SP 6 ). Go to this link: http://www.sdn.sap.com/irj/boc and download the trial version of CR XI R2 and use your XI keycode, then apply the patches which you can get to by clicking on the BusinessObjects tab above, then Downloads.
Direct link to the Trial version: http://www.sap.com/solutions/sapbusinessobjects/sme/freetrials/index.epx
It may fix your issue also.
Thanks again
Don -
Calling a stored procedure within a package
We have a number of packages containing stored procedures. In an existing production application, we used embedded SQL in C programs to call these stored procs
e.g.
EXEC SQL EXECUTE
BEGIN owner.fees_calc.some_fee(:parm1,...);
END;
END-EXEC;
Now, I am trying to use SQLJ to call this same stored proc in the package. However, I am getting a compilation error from sqlj saying that it cannot find a stored procedure or function of that name. It works fine if I use a stored proc that is not in a package.
So how do I call a stored procedure within a package? Or is this not currently possible with sqlj?
I am also getting a warning just before the error and I'm wondering if the error is being caused by this:
Warning: You are using an Oracle JDBC driver, but connecting to a non-Oracle database. SQLJ will perform JDBC-generic SQL checking.
I am connecting to an Oracle 7.3.3 database using an Oracle 7.3.4 JDBC driver. I also tried using the Oracle 8.0.5 JDBC driver for the same database but I get the same warning message.I used the following code to call a stored
procedure via SQLJ:
try {
#sql [iCtx] {
CALL ibs.cvs_validate.validate_port_id(:IN record_id ,:IN poe_pod_flag,:IN port_id,:OUT error_code,:OUT error_message) };
where
"ibs" is the schema
"cvs_validate" is the package
"validate_port_id" is the procedure
The code runs fine, but to get it to compile
in JDeveloper 2.0, I had to disable the "Check SQL semantics against database schema" option on the Project Properties (SQLJ) property sheet.
null -
With clause within the cursor ???
Hello gurus,
Can we have a with clause within the cursor ???
something like this ..
cursor test_cur is
WITH unpivoted_data AS
SELECT DISTINCT
f.cust_id
f.office_cd
f.type_cd
FROM refoff ro,
cutomer p,
office f
WHERE f.cust_id = p.cust_id
AND f.office_cd = ro.office_cd
AND f.type_cd IN ('ACCT', 'pay')
SELECT cust_id
, MAX (CASE WHEN type_cd = 'ACCT' THEN office_id END) AS acct_office_id
, MAX (CASE WHEN type_cd = 'pay' THEN office_id END) AS pay_office_id
FROM unpivoted_data
GROUP BY cust_id
test_cur_rec test_cur% rowtype;
Begin
for test_cur_rec in test_cur
loop
insert into test1 (.....) values (.......);
end loop;
end;
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....???
Thank you so much!!!Hi,
user642297 wrote:
Hello gurus,
Can we have a with clause within the cursor ???Sure, but don't take my word for it. Try it and see!
If you get an error, post the error message.
If you get unexpected results, explain.
Whenever you have a problem, post your full Oracle version number.
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....??? Type these 6 characters:
(all small letters, inside curly brackets) before and after formatted sections. -
SSRS 2008 Using Stored Procedure with SysRef Cursor Oracle
Hi,
I am new to SSRS.
I have a ssrs procedure which runs to Return Result of Select Query. ( In form of SYS REF Cursor in Oracle ).
When I am trying to see the Value in Test Window. It is showing Output. But when I m Running the Report in the environment it is giving me error.
Please help me if there is a better way of handling it.
As I read in some forum as SSRS has issue handling SYS Ref Cursor...
Thanks
Priyank
Thanks Priyank PiyushHi Priyank,
According to your description, you are use a Stored Procedure from SysRef Cursor Oracle as the report dataset. The query works well in Dataset Query Designer, while an error occurs when you render the report.
As per my understanding, the problem is that there are something wrong with the report design. Please double-check the design of the report. In order to trouble shoot the problem more efficiently, could you please post the detail error message? Then we can
make further analysis.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
How can I create cursors within the cursor?
How can I create cursors within the cursor?
Table1 2001 - 2007 data
Account_no
Account_eff_dt
No_account_holder
Num
Seq_Num
Value1
Value2
Table2_Historical (doesn't have Num as a field) 1990 - 2000 data
Account_no
Account_eff_dt
No_account_holder
Seq_Num
Value1
Value2
Table3_06
Account_no
Account_eff_dt
No_account_holder
Num
My result table should be:
Table_result_06
Account_no
Account_eff_dt
No_account_holder
Num
Value1_min (the minimum value for the minimum of record_sequence)
Value2_max (the maximum value for the maximum of record_sequence)
I have to get data from Table1 and Table2_Historical. If one account was open in 1998 and is still effective, the minimum value of that account is in the Table2_Historical.
Let's say I open a cursor:
cursor_first is
select * from table3_06;
open csr_first
loop
fetch cursor_first into
v_Account_no
v_Account_eff_dt
v_No_account_holder
v_Num
EXIT WHEN csr_first%NOTFOUND;
How can I open a second cursor from here that will get the Seq_Num from Table1
csr_second
select Seq_Num from Table1 where
v_Account_no = Account_no
v_Account_eff_dt <= Account_eff_dt
v_No_account_holder=No_account_holder
v_Num = Num
How does it works???
Thanks a lotThanks so much for replying back. Here is what I am trying to do.
I have to create a table for each year 2002, 2003, 2004, 2005, 2006 that has all the account numbers that are active each year plus some other characteristics.
Let’s say I will create Table_result_06. This table will have the following fields. The account number, account effective date, Number of the account holder and the field Num (The primary key is a combination of all 4 fields), the beginning look of value 1 in 2006, the last look of value 1 in 2006.
Table_result_06
Account_no key
Account_eff_dt key
No_account_holder key
Num key
Value1_min (the minimum value for the minimum of record_sequence)
Value2_max (the maximum value for the maximum of record_sequence)
All the active account numbers with the Account_eff_dt, No_account_holder and Num are in the Table3. As such I can build a query that connects Table3 with table Table1 on all 4 fileds, Account_no, Account_eff_dt, No_account_holder, Num and find the Value1_min for the min of req_sequence in 2006 and Value1_max for the max of req_sequence in 2006. Here my problem starts.
Table 1 doesn’t have a new entry if nothing has changed in the account. So if this account was open in 1993 and nothing has changed I don’t have an entry in 2006 but this doesn’t mean that this account doesn’t exist in 2006. So to find the minimum value I have to go back in 1993 and find the max and min for that year and that will be max and min in 2006 as well.
As such I have to go to Table_2 historical and search for min and max. But this table doesn’t have the field NUM and if I match only on the Account_no, Account_eff_dt and No_account_holder I don’t get a unique record.
So how can I connect all three tables, Table 1 for max and min, if it doesn’t find anything will go to table 2 find the two values and populate Table_result_06.
Thanks so much again for your hep, -
Function call in procedure within Package Body
I am a novice in PL/SQL so that I can't find out where the problem is. I am testing a function call in procedure within a Package Body.
But the PL/SQL Complier doesn't compile the package body but I don't know what I do wrong. Plz let me know how to call a function in procedure within a Package Body?
Here are the Packaget test programs..
CREATE OR REPLACE PACKAGE manage_students
IS
PROCEDURE find_sname;
PROCEDURE find_test;
PROCEDURE find_test_called;
FUNCTION GET_LASTMT
RETURN SEQUENCE_TEST.SEQ_NO%TYPE;
END manage_students;
CREATE OR REPLACE PACKAGE BODY manage_students AS
v_max_nbr SEQUENCE_TEST.SEQ_NO%TYPE;
PROCEDURE find_sname
IS
BEGIN
BEGIN
SELECT MAX(SEQ_NO)
INTO v_max_nbr
from SEQUENCE_TEST;
DBMS_OUTPUT.PUT_LINE('MAX NUMBER is : '||v_max_nbr);
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
RETURN;
END;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
END find_sname;
PROCEDURE find_test
IS
BEGIN
BEGIN
DBMS_OUTPUT.PUT_LINE('MAX NUMBER Called from another procedure : '||v_max_nbr);
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
RETURN;
END;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
END find_test;
FUNCTION GET_LASTMT
RETURN SEQUENCE_TEST.SEQ_NO%TYPE
IS
v_max_nbr SEQUENCE_TEST.SEQ_NO%TYPE;
BEGIN
SELECT MAX(SEQ_NO)
INTO v_max_nbr
from SEQUENCE_TEST;
RETURN v_max_nbr;
EXCEPTION
WHEN OTHERS
THEN
DECLARE
v_sqlerrm VARCHAR2(250) :=
SUBSTR(SQLERRM,1,250);
BEGIN
RAISE_APPLICATION_ERROR(-20003,
'Error in instructor_id: '||v_sqlerrm);
END;
END GET_LASTMT;
PROCEDURE find_test_called
IS
BEGIN
BEGIN
V_max := Manage_students.GET_LASTMT;
DBMS_OUTPUT.PUT_LINE('MAX_NUMBER :'|| V_max);
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
RETURN NULL;
END;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('Error in finding student_id: ');
END find_test_called;
END manage_students;
DECLARE
v_max SEQUENCE_TEST.SEQ_NO%TYPE;
BEGIN
manage_students.find_sname;
DBMS_OUTPUT.PUT_LINE ('Student ID: Execute.');
manage_students.find_test;
manage_students.find_test_called;
END;
-----------------------------------------------------------------------------------------------Hi,
Welcome to the forum!
You'll find that there are a lot of people willing to help you.
Are you willing to help them? Whenever you have a problem, post enough for people to re-create the problem themselves. That includes CREATE TABLE and INSERT statements for all the tables you use.
Error messages are very helpful. Post the complete error message you're getting, including line number. (It looks like your EXCEPTION sections aren't doing much, except hiding the real errors. That's a bad programming practice, but probably not causing your present problem - just a future one.)
Never post unformatted code. Indent the code to show the extent of each procedure, and the blocks within each one.
When posting formatted text on this site, type these 6 characters:
\(all small letters, inside curly brackets) before and after each section of formatted test, to preserve the spacing.
For example, the procedure find_test_called would be a lot easier to read like this:PROCEDURE find_test_called
IS
BEGIN
BEGIN
V_max := Manage_students.GET_LASTMT;
DBMS_OUTPUT.PUT_LINE ('MAX_NUMBER :' || V_max);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('Error in finding student_id: ');
RETURN NULL;
END;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('Error in finding student_id: ');
END find_test_called;
It's much easier to tell from the code above that you're trying to return NULL from a procedure. Only functions can return anything (counting NULL); procedures can have RETURN statements, but that single word"RETURN;" is the entire statement. -
Call a procedure within a Procedure
hi
How can i call a procedure within a procedure
Thanks in advanceSQL> create procedure b as
2 begin
3 null;
4 end;
5 /
Procedure created.
SQL> create procedure a as
2 begin
3 b;
4 end;
5 /
Procedure created.
SQL> -
Procedure within procedure problem
Hi
I have a table of 5 different magazines and a table of purchases of those magazines. I have written a procedure to take the details of a given magazine and place the sales for a given month into a sales table as follows:
create or replace procedure monthly_sales(mag number, startdate date, enddate date) is
magtotal number(7,0);
magprice magazine.unitprice%type;
magsales number(7,2);
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = mag and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, mag, magtotal, magsales);
end;
What I would like to do though is have a procedure that you just need to run once and it will enter the sales for a given month for all magazines into the sales table. My thought was to try to do this using procedures within a procedure as follows:
create or replace procedure monthly_sales(startdate date, enddate date) is
magtotal number(7,0);
magprice magazine.unitprice%type;
magsales number(7,2);
procedure mag1 is
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = 1 and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, 1, magtotal, magsales);
end mag1;
procedure mag2 is
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = 2 and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, 2, magtotal, magsales);
end mag2;
procedure mag3 is
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = 3 and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, 3, magtotal, magsales);
end mag3;
procedure mag4 is
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = 4 and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, 4, magtotal, magsales);
end mag4;
begin
select count(p.magid), m.unitprice into magtotal, magprice from purchase p, magazine m where p.datepurchased between startdate and enddate and p.magid = 5 and m.magid=p.magid
group by m.unitprice;
magsales := magtotal*magprice;
insert into sales values(startdate, 5, magtotal, magsales);
end;
However, when I run this it is ignoring all the procedures within the main procedure and just entering the results for magazine 5. I'm at a loss as to why this isn't working, is this even the correct way to go about it? any help would be greatly appreciated
thanksWhy doing it the hard way?
A single insert statement will do the trick.
I made a bit of a guess as to the structure of your tables:
create table magazine (magid number primary key, unitprice number);
create table purchase (magid number references magazine(magid), datepurchased date);
create table sales (startdate date, magid number references magazine(magid), magtotal number, magsales number);
insert into magazine(magid, unitprice) values (1, 3.95);
insert into magazine(magid, unitprice) values (2, 4.95);
insert into magazine(magid, unitprice) values (3, 3.50);
insert into magazine(magid, unitprice) values (4, 6.0);
insert into magazine(magid, unitprice) values (5, 5.50);
insert into purchase(magid, datepurchased) values (1, sysdate);
insert into purchase(magid, datepurchased) values (1, sysdate);
insert into purchase(magid, datepurchased) values (2, sysdate);
insert into purchase(magid, datepurchased) values (2, sysdate);
insert into purchase(magid, datepurchased) values (2, sysdate);
insert into purchase(magid, datepurchased) values (4, sysdate);
insert into purchase(magid, datepurchased) values (5, sysdate);
insert into purchase(magid, datepurchased) values (5, sysdate);
insert into purchase(magid, datepurchased) values (5, sysdate);
insert into purchase(magid, datepurchased) values (5, sysdate);
commit;
create or replace procedure monthly_sales(p_startdate in date, p_enddate in date)
is
begin
insert into sales (startdate, magid, magtotal, magsales)
select p_startdate
, p.magid
, count(p.magid)
, count(p.magid) * m.unitprice
from purchase p
join magazine m on m.magid = p.magid
where p.datepurchased between p_startdate and p_enddate
group by p.magid
, m.unitprice;
end;
begin
monthly_sales(trunc(sysdate,'MM'), last_day(trunc(sysdate,'MM')));
end;
select * from sales;
STARTDATE MAGID MAGTOTAL MAGSALES
01-JAN-11 1 2 7.9
01-JAN-11 2 3 14.85
01-JAN-11 4 1 6
01-JAN-11 5 4 22 -
Call procedure within procedure
Hai
I have some question
1)
This below procedure create user at run time
============================================
create or replace procedure new_u1( p_name in varchar2,
p_pw in varchar2,
p_def_tblspace in varchar2 default 'users' )
as
begin
execute immediate 'create user ' || P_name || ' identified by ' ||
p_pw || ' default tablespace ' || p_def_tblspace ||
' temporary tablespace temp';
execute immediate 'grant create session to ' || p_name;
end;
This below procedure create tables at run time
==============================================
create or replace procedure M2 (table_name varchar2) as
cursor1 integer;
begin
cursor1 := dbms_sql.open_cursor;
dbms_sql.parse(cursor1, 'DROP TABLE ' || table_name,
dbms_sql.v7);
dbms_sql.close_cursor(cursor1);
end;
I want ,when first procedure calls ,it calls second procedure and create tables in that schema (call procedure within procedure).How to do that?
2)What is diffrenece between row and statement level trigger?how many types of triggers?
3)what is mutating trigger or table?
4)dbms_stats.gather_table_stats package it collects the statistics about the tables .I want statistics to be collected at every day 9am and 7pm how to do that?
Thanks in advance
MohanYou could use an expression like this, to generate the run times:
SQL> create table times (dt date) ;
Table created.
SQL> BEGIN
2 FOR idx IN 1 .. 24
3 LOOP
4 INSERT INTO times VALUES (trunc(SYSDATE) + (idx / 24));
5 INSERT INTO times VALUES (trunc(SYSDATE) + (idx / 24) + (1 / (24 * 60)));
6 INSERT INTO times VALUES (trunc(SYSDATE) + (idx / 24) + (59 / (24 * 60)));
7 END LOOP;
8 END;
9 /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT to_char(dt,
2 'dd-mon-yyyy hh:mi:ss AM'),
3 to_char(decode(sign(to_number(to_char(dt,
4 'hh24MI')) - 900),
5 -1,
6 trunc(dt) + (9 / 24),
7 0,
8 trunc(dt) + (9 / 24),
9 decode(sign(to_number(to_char(dt,
10 'hh24MI')) - 1900),
11 -1,
12 trunc(dt) + (19 / 24),
13 0,
14 trunc(dt) + (19 / 24),
15 trunc(dt) + 1 + (9 / 24))),
16 'dd-mon-yyyy hh:mi:ss AM')
17 FROM times
18 ORDER BY dt
19 /
TO_CHAR(DT,'DD-MON-YYYY TO_CHAR(DECODE(SIGN(TO_
01-jan-2004 01:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 01:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 01:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 02:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 02:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 02:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 03:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 03:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 03:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 04:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 04:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 04:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 05:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 05:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 05:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 06:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 06:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 06:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 07:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 07:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 07:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 08:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 08:01:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 08:59:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 09:00:00 AM 01-jan-2004 09:00:00 AM
01-jan-2004 09:01:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 09:59:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 10:00:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 10:01:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 10:59:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 11:00:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 11:01:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 11:59:00 AM 01-jan-2004 07:00:00 PM
01-jan-2004 12:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 12:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 12:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 01:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 01:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 01:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 02:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 02:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 02:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 03:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 03:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 03:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 04:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 04:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 04:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 05:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 05:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 05:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 06:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 06:01:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 06:59:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 07:00:00 PM 01-jan-2004 07:00:00 PM
01-jan-2004 07:01:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 07:59:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 08:00:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 08:01:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 08:59:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 09:00:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 09:01:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 09:59:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 10:00:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 10:01:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 10:59:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 11:00:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 11:01:00 PM 02-jan-2004 09:00:00 AM
01-jan-2004 11:59:00 PM 02-jan-2004 09:00:00 AM
02-jan-2004 12:00:00 AM 02-jan-2004 09:00:00 AM
02-jan-2004 12:01:00 AM 02-jan-2004 09:00:00 AM
02-jan-2004 12:59:00 AM 02-jan-2004 09:00:00 AM
72 rows selected.
SQL> -
Calling a oracle procedure within a procedure - Urgent
Hi
Can some one tell me, how to call a oracle procedure within a procedure ?
Thanks
MuraliYou could always try looking in the manuals.
-
Executing procedure within same procedure
Hi all,
Can any one tell me what would be happen if execute procedure within same procedure.
Example :
create or replace procedure print(str in varchar2)
is
begin
print(str);
dbms_output.put_line(str);
exception
when others then
null;
end print ;
Regards,
P PrakashBut, you could call the same procedure in recursion procedure.
This kind of procedure has :
1) action of the recursion procedure
2) condition to go out from the same recursive procedure
SQL> create or replace procedure print(str in varchar2) is
2 num_letters natural;
3 string_tmp varchar2(50);
4 begin
5 -- recursion action
6 dbms_output.put_line(str);
7
8 -- recursion condition
9 num_letters := nvl(length(str), 0);
10 if num_letters > 0 then
11 print(substr(str, 1, num_letters - 1));
12 end if;
13 end print;
14 /
Procedure created
SQL> exec print('abcdefghijkl');
abcdefghijkl
abcdefghijk
abcdefghij
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a
PL/SQL procedure successfully completed
SQL>
Maybe you are looking for
-
Hi All, By using client proxy i am moving material master data into a file, i have finished IR part and activated it after that i went to R/3 side and generated the proxy and activated it . after that i have written a program in abap editor to fetch
-
We have been using ARIS for process modeling. When ARIS was first installed 3 years ago the IDS consultant suggestd that we go with a 4 level process hierarchy, as follows: Level 1 Process Group (VACD) Level 2 Scenario (EPC) Level 3 Process (EPC colu
-
just bought this phone to help ff but i found out they are irresponcible.zte is careless about updates, i tried manualy signatur prblm updte faild, no real sport from anyone.i m not that rich .i could have buy food for my famely for all week for the
-
Bootcamp error in Windows mode
After starting Windows in Boot Camp, I get a error message that Boot Camp has encountered a problem and needs to close. The icon to switch back to OS in the lower tray is missing. My work around is that I go to control panel > bootcamp and use that t
-
Unable to Install CS4 Design Premium on new Macbook Pro
I have recieved an answer regarding my inability to install my software on my new computer, but it does not address the problem that there is no record of my previous purchase. I'm sure this is because it was purchased in 2009 and whatever e mail wa