Sort data in a ref cursor
Hello,
I would like to know if the following is possible:
I'm getting data from different sources in a ref cursor. Is it possible to sort this ref cursor on a specific column in the ref cursor? If so, how?
Thanks
Karuna
Note that this is a duplicate post of this thread
sort data in ref cursor
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
Similar Messages
-
Ref Cursors / throwing data into a Ref Cursor as data is fetched
I was wondering if anyone has executed an SQL statement and as each row is being fetched back from an SQL, doing some data checks and processing to see if the row is valid to return or not, based on the values being fetched in an SQL.
For example, I'm taking an SQL statement and trying to do some tuning. I have an Exists clause in the Where statement that has a nested sub-query with some parameters passed in. I am attempting to move that statement to a function call in a package (which is called in the SELECT statement). As I fetch each row back, I want to check some values that are Selected and if the values are met, then, I want to execute the function to see if the data exists. If it does exist, then, I want the fetched row returned in the Ref Cursor. If the criteria is met and the row doesn't exist in the function call, then, I don't want the fetched row to return.
Right now, the data has to be thrown to REF Cursor because it's being outputted to the Java application as a Result Set.
I've found many statements where you can take a SELECT statement and throw the Results in the Ref Cursor. But, I want to go a step further and before I throw each row in the Ref Cursor, I want to some processing to see if I put the Fetched Row in the Ref Cursor.
If someone has a better idea to accomplish this, I'm all ears. Like I say, I'm doing this method only for the sake of doing some database tuning and I think this will speed things up. Having the EXISTS clause works and it runs fast from an End-user standpoint but, when it processes on the database with the nested subquery, it is slow.
Here's an example of something that might be a problem (Notice the nested subquery). I moved the nested subquery to a function call written on the database package and make the call to the procedure/package in the SELECT statement. As I process each row, I want to check some values prior having the function call execute. If it meet some criteria, then the record is Ok to fetch and display in the Ref Cursor. If it does not meet the criteria and goes through the function and doesn't return data, then, I don't want the Fetched row from the main query to return the data.:
SELECT EMPNO,
FIRST_NAME,
LAST_NAME
FROM EMP E,
DEPT D
WHERE E.DEPTNO = D.DEPTNO
AND EXISTS (SELECT 'X'
FROM MANAGER M
WHERE M.MANAGER_ID = E.MANAGER_ID
AND MANAGER_TYPE IN (SELECT MANAGER_TYPE
FROM MANAGER_LOOKUP ML WHERE ML.MANAGER_TYPE = M.MANAGER_TYPE))
Any help or ideas of other things to try is appreciated. Keep in mind that I am returning this data to the Java application so, throwing the data to a Ref Cursor in the PL/SQL is the ideal method.
ChrisRef cursors are not required nor desirable when writing java database application. Cursors are mentioned only once in the JDBC documentation reference guide, in the section "Memory Leaks and Running Out of Cursors".
In a word cursors are just plain ridiculous, and in fact I never used them in my 15+ years of application development practice:
http://vadimtropashko.wordpress.com/cursors/ -
Getting data stored in ref cursor ( got from store proc in oracle) to excel sheet)
Hey, I am trying to Get data stored in ref cursor ( got from store proc in oracle) to excel sheet in a virtual folder using ssis.
I am getting errors and cant do it. If anyone can help meHi Nabin000,
The Oracle stored procedure doesn't work with SSIS source adapters such as OLE DB Source because the data provider doesn't provide metadata for the source adapter. To achieve your goal, you can use a Script Component as source to call
the Oracle stored procedure by using System.Data.OracleClient OracleDataReader, and get the rows and add them to the pipeline buffer. For more information, please see:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1d0b3a1b-8792-469c-b0d1-f2fbb9e9ff20/dump-oracle-ref-cursor-into-ms-sql-staging-table-using-ssis
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/fcdaa97e-8415-4c3e-8ffd-1ad45b590d57/executing-an-oracle-stored-procedure-from-ssis?forum=sqlintegrationservices
http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledatareader(VS.90).aspx
Regards,
Mike Yin
TechNet Community Support -
How to retrieve data from a REF CURSOR using OCI 8.0?
I found an example in Oracle docs (shown below) that discusses how to bind a REF CURSOR for later data retrieval, but it does not explain actually how to do the later data retrieval.
I hope someone can explain it to me. Thanks
The OCI provides the ability to bind and define PL/SQL REF CURSORs and nested tables. An application can use a statement handle to bind and define these types of variables. As an example, consider this PL/SQL block:
static const text plsql_block = (text )
"begin \
OPEN :cursor1 FOR SELECT empno, ename, job, mgr, sal, deptno \
FROM emp_rc WHERE job=:job ORDER BY empno; \
OPEN :cursor2 FOR SELECT * FROM dept_rc ORDER BY deptno; \
end;";
An application would allocate a statement handle for binding, by calling OCIHandleAlloc(), and then bind the :cursor1 placeholder to the statement handle, as in the following code, where :cursor1 is bound to stm2p. Note that the handle allocation code is not included here.
err = OCIStmtPrepare (stm1p, errhp, (text *) nst_tab, strlen(nst_tab),
OCI_NTV_SYNTAX, OCI_DEFAULT);
err = OCIBindByName (stm1p, (OCIBind **) bndp, errhp,
(text *)":cursor1", (sb4)strlen((char *)":cursor1"),
(dvoid *)&stm2p, (sb4) 0, SQLT_RSET, (dvoid *)0,
(ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, (ub4)OCI_DEFAULT);
In this code, stm1p is the statement handle for the PL/SQL block, while stm2p is the statement handle which is bound as a REF CURSOR for later data retrieval. A value of SQLT_RSET is passed for the dty parameter.( sorry, i forgot the Link where i get this html fiLes, so i just copy-paste here )
( maybe it can heLp you a bit. -- it's heLp me, for sure )
And the following is thanks to Brett Rosen :
I noticed that you didn't have an OCI entry
on http://osi.oracle.com/~tkyte/ResultSets/index.html .
Here is OCI code to do this (Oracle 81) if you want to include it on
that page.
Some error checking and cleanup has been removed, but the below should
work. (once dbname has been replaced appropriately)
Brett
int main(int argc, char* argv[])
OCIError* pOciError;
char* pConnectChar = "dbname";
char* pUsernameChar = "scott";
char* pPasswordChar = "tiger";
int answer;
OCIStmt* pOciStatement;
char* sqlCharArray = "BEGIN :success := sp_ListEmp; END;";
int id;
char ename[40];
OCIEnv* g_pOciEnvironment = NULL;
OCIServer* g_pOciServer = NULL;
OCISession* g_pOciSession = NULL;
OCISvcCtx* g_pOciServiceContext = NULL;
sb2* pIndicator=0;
sb2* pIndicator2=0;
sb2* pIndicator3=0;
OCIDefine* pOciDefine;
OCIDefine* pOciDefine2;
OCIBind* pBind;
OCIStmt* cursor;
answer = OCIInitialize(OCI_THREADED, NULL, NULL, NULL, NULL);
answer = OCIEnvInit(&g_pOciEnvironment, OCI_DEFAULT, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&pOciError, OCI_HTYPE_ERROR, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciSession, OCI_HTYPE_SESSION, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciServer, OCI_HTYPE_SERVER, 0, NULL);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)&g_pOciServiceContext, OCI_HTYPE_SVCCTX, 0, NULL);
answer = OCIServerAttach(g_pOciServer, pOciError, (unsigned char *)pConnectChar, strlen(pConnectChar),
OCI_DEFAULT);
answer = OCIAttrSet(g_pOciSession, OCI_HTYPE_SESSION, (unsigned char *)pUsernameChar, strlen(pUsernameChar),
OCI_ATTR_USERNAME, pOciError);
answer = OCIAttrSet(g_pOciSession, OCI_HTYPE_SESSION, (unsigned char *)pPasswordChar, strlen(pPasswordChar),
OCI_ATTR_PASSWORD, pOciError);
answer = OCIAttrSet(g_pOciServiceContext, OCI_HTYPE_SVCCTX, g_pOciServer, 0, OCI_ATTR_SERVER, pOciError);
answer = OCIAttrSet(g_pOciServiceContext, OCI_HTYPE_SVCCTX, g_pOciSession, 0, OCI_ATTR_SESSION, pOciError);
answer = OCISessionBegin(g_pOciServiceContext, pOciError, g_pOciSession, OCI_CRED_RDBMS, OCI_DEFAULT);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)(&pOciStatement), OCI_HTYPE_STMT, 0, NULL);
answer = OCIStmtPrepare(pOciStatement, pOciError, (unsigned char *)sqlCharArray, strlen(sqlCharArray),
OCI_NTV_SYNTAX, OCI_DEFAULT);
answer = OCIHandleAlloc(g_pOciEnvironment, (void **)(&cursor), OCI_HTYPE_STMT, 0, NULL);
answer = OCIBindByPos(pOciStatement,&pBind, pOciError, 1, &cursor, 0,SQLT_RSET,
pIndicator2, 0,NULL, 0,0,OCI_DEFAULT);
answer = OCIStmtExecute(g_pOciServiceContext, pOciStatement, pOciError, 1, 0, NULL, NULL,
OCI_COMMIT_ON_SUCCESS);
answer = OCIDefineByPos(cursor,&pOciDefine, pOciError,2,&id,sizeof(int),
SQLT_INT,pIndicator, 0, 0,OCI_DEFAULT);
answer = OCIDefineByPos(cursor,&pOciDefine2, pOciError,1,ename,40,
SQLT_STR,pIndicator3, 0, 0,OCI_DEFAULT);
if (answer == 0)
while ((answer = OCIStmtFetch(cursor,pOciError, 1,OCI_FETCH_NEXT,OCI_DEFAULT)) == 0)
printf("fetched id %d and name %s\n",id,ename);
answer = OCIHandleFree(pOciError, OCI_HTYPE_ERROR);
return 0;
} -
Manipulating data from a ref cursor in the data block of a form
I am using Oracle Forms 10g. I have a ref cursor which returns columns A, B, C, D, E where B, C, D, E are values. The "Query Data Source Name" attribute in the datablock contains "CCTR_Extract_pkg.cctr_refcur". The "Column Name" attribute of item A contains "A", item B contains "B", etc. All of these columns are displayed on the form and it all works perfectly.
I have a new request, the users would like a 6th column displayed - column F - where it equals D - B.
Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form?
If it can be achieved in the Form (only) - then how do I reference the 2 columns?
Thanks in anticipation
Michael
Edited by: user8897365 on 24-Aug-2011 10:32Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form? The REF_CURSOR is the data source of your block so you will have to modify the CCTR_Extract_pkg package and cctr_refcur REF_CURSOR.
If it can be achieved in the Form (only) - then how do I reference the 2 columns?After you have modified your database package, I recommend you run the Data Block Wizard on your block and let Forms detect the 2 new columns. You can do this manually, but it is safer to let Forms do it for you. If you want to do it manually, open the Query Data Source Columns property and add your new columns.
Hope this helps,
Craig B-)
If someone's response is helpful or correct, please mark it accordingly. -
How to update data returned using REF CURSOR
Hi all,
I am trying to update updated data in a gridview but the update button seem to do nothing as i retrieve data using REF CURSOR.
Let me describe the architecture of my application first. I'm trying to implement best practice whenever possible. I am following the data access tutorial published in www.asp.net , the only difference is that i have an Oracle (10g) database. So I split my application into three layers, data access, business logic, and presentation layer. I'm also writing all queries in an Oracle package.
So I have my Oracle packages that perform CRUD operations. Then I have an xsd file that define dataTable based on the package procedure. My business logic layer then calls functions defined in the xsd file. And finally a detailsView control that uses an ObjectDataSource to call business logic functions.
In a nutshell, I am just trying to update records retrieved using REF CURSOR. Your help is very much appreciated. Please let me know if further details are required. Cheers,In the DataSet (xsd) where your DataTable is defined, you just need to add additional methods to the TableAdapter to handle insert, update and delete, either with SQL or by mapping to stored procedures.
Alternatively in code, create an OracleDataAdapter and supply its InsertCommand, UpdateCommand and DeleteCommand.
David -
I am trying to make a ssis package that get the data calling the store proc with two param one is ID and other is Sys_refcursor. Say Store Proc as ListName(Id int, myCur sys_refcursor), which gets the datas with the conditions inside it.
REATE OR REPLACE PROCEDURE schemaName.LISTNAME (P_ID IN INT, LST_NAME_REFCUR IN OUT SYS_REFCURSOR)
IS
P_NAMESOURCE_ID INT;
BEGIN
SELECT SOURCE_ID INTO P_NAMESOURCE_ID FROM SEARCHING_TABLE ST WHERE ST.ID = P_ID;
IF (P_NAMESOURCE_ID=1)
THEN
OPEN LST_SOURCE_REFCUR FOR
SELECT ST.ID,
ST.TRANSACTION_DATE AS TRAN_DATE,
IF (P_NAMESOURCE_ID=1)
THEN
OPEN LST_SOURCE_REFCUR FOR ....
then i need to get the data from that refcursor and fetch those data to excel sheet to a virtual directory.
Any help would be appreciated. I am new to SSIS. and i need to do this assignment this friday.Hi 11srk,
To fetch data from Oracle store procedure, you can use a Script Component as source to call the Oracle stored procedure by using System.Data.OracleClient OracleDataReader, and get the rows and add them to the pipeline buffer. For more information, please
see:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1d0b3a1b-8792-469c-b0d1-f2fbb9e9ff20/dump-oracle-ref-cursor-into-ms-sql-staging-table-using-ssis
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/fcdaa97e-8415-4c3e-8ffd-1ad45b590d57/executing-an-oracle-stored-procedure-from-ssis?forum=sqlintegrationservices
http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledatareader(VS.90).aspx
Regards,
Mike Yin
TechNet Community Support -
Find data where data not in ref cursor
CREATE OR REPLACE PACKAGE types AS
TYPE weak_ref_cursor IS REF CURSOR;
END types;
CREATE OR REPLACE PROCEDURE procedure_name
(p_getname OUT types.weak_ref_cursor,
p_tablename IN VARCHAR2, p_salary IN NUMBER)
AS
v_tem VARCHAR2 (4000);
BEGIN
v_tem := 'SELECT ENAME FROM ' || UPPER (p_tablename) || ' WHERE sal > :b_salary';
OPEN p_getname FOR v_tem USING p_salary;
END procedure_name;
SQL> EXECUTE procedure_name (:g_getname, 'EMP', 2000);
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> print g_getname;
ENAME
ALLEN
WARD
MARTIN
BLAKE
babu
JOHN
TURNER
7 rows selected.
Now i want to select data from emp table where ename not in ouput of g_getname
Please help meYou cannot select that data from using a query(since the program unit is procedure), instead you can write a code (anonymous pl/sql block) and retrive the records and print them using dbms_output.put_line on console.
-
Sorting records dynamically in REF cursor, based upon a dynamic field
Hi,
I have a REF CURSOR built by using row type, table type and PIPELINE function. I have opened the ref cursor now. I would like to update a field called 'RANK' based upon 'RATIO' field in the REF CURSOR. i.e order the records in the ref cursor by RATIO field and then update the RANK as 1, 2, 3, 4, ....
Aim: I want to update a field in the REF CURSOR based upon another numeric field.
Please help me.
OPEN sales FOR
SELECT RANK, ratio
FROM TABLE (fngetfundholdingsale (in_primarykey, in_flag));
loop
fetch sales into sale1;
exit when sales%notfound;
--I want to update sale1.rank based upon ratio
end loop;
Thanks
AshokTry to use NDS (Native Dynamic SQL):
l_order := 'ratio';
OPEN sales FOR
'SELECT rank' ||
' FROM TABLE (fngetfundholdingsale (:in_primarykey, :in_flag))' ||
' ORDER BY ' || l_order
USING in_primarykey, in_flag;Regards,
Zlatko -
Hi,
I am new to Ref Cursor concepts and I am trying a small block but its throwing error. Pls help me.
PACKAGE SPEC:
CREATE OR REPLACE PACKAGE PKG_JOBINFO AS
PROCEDURE JOBINFO ( v_job_id IN number, p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR);
TYPE RESULT_REF_CURSOR IS REF CURSOR;
END PKG_JOBINFO;
PACKAGE BODY:
CREATE OR REPLACE package body PKG_JOBINFO
AS
PROCEDURE JOBINFO ( v_job_id IN number,
p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR)
AS
BEGIN
OPEN p_cursor FOR
SELECT JOB_ID,
JOB_NAME,
TABLE_NAME
FROM JOB_INFO
WHERE JOB_ID=V_JOB_ID;
EXCEPTION
WHEN OTHERS THEN
raise;
END;
END;
While compiling the package i am not getting any errors. I am getting errors only while executing
SQL> exec PKG_JOBINFO.JOBINFO ('23');
BEGIN PKG_JOBINFO.JOBINFO ('23'); END;
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'JOBINFO'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Please help me to resolve this error.
Thanks.user497267 wrote:
Thanks its working. So if we are using ref cursor we have to execute like this only.To add...
The actual issue you were experiencing was not because you were using ref cursors specifically, but because you had an OUT parameter in your procedure.
When you have an OUT parameter, you need to ensure that you pass in a variable to that parameter of the procedure in order that the procedure can populate it. In your case you were only passing in the first parameter, but you weren't passing in a variable to capture the OUTput.
What Alex showed was that by declaring a variable of the same datatype (ref cursor in your case) and passing that in as the second parameter, that variable was populated with the ref cursor information from inside the procedure. Once that variable was populated, after the procedure call, the data from that ref cursor can be obtained (using SQL*Plus' print command in Alex's example). -
I have a function that I created and am trying to use it to output data using a ref cursor. My function call is
declare
ret_cursor crsiweb.types.cursorType;
type output_rec_type is record(
location varchar2(100),
client varchar2(100),
number_in long,
number_out long,
total_cost varchar2(100),
total_charge varchar2(100),
net varchar2(100),
average varchar2(100)
output output_rec_type;
begin
ret_cursor := all_db.net_profit.net_profit(to_date(:P1_Date_Chart,'DD-Mon-RR'));
fetch ret_cursor into output;
dbms_output.put_line('Location' || ',' || 'Company'|| ',' || 'In' ||
','|| 'Out'|| ',' || 'Cost'|| ',' || 'Charge'
|| ',' || 'Net' || ',' || '30 Day Average');
while ret_cursor%FOUND loop
dbms_output.put_line(output.location || ',' || output.client || ',' || output.number_in ||
','|| output.number_out || ',' || output.total_cost || ',' || output.total_charge
|| ',' || output.net || ',' || output.average);
fetch ret_cursor into output;
end loop;
close ret_cursor;
end;
i have it as a PL/SQL Procedure (anonymous block) in a region. When executed I get the following error:
"ORA-06550: line 15, column 25: PLS-00904: insufficient privilege to access object ALL_DB.NET_PROFIT ORA-06550: line 15, column 4: PL/SQL: Statement ignored"
The function is as follows: ( I created the function in OEM)
create or replace procedure all_db.net_profit.net_profit
as
function net_profit(sdate IN date) return crsiweb.types.cursortype as
ret_cursor crsiweb.types.cursortype;
begin
open ret_cursor for select 'a_chase' "Location",a."Client" "Client", sum(nvl(b."In",0)) "In", sum(nvl(a."Out",0)) "Out", to_char(sum(nvl(a."Cost",0)
+ nvl(b."Cost",0)),'FM$9999999990.00PR') "Cost",
to_char(sum(nvl(a."Charge",0) + nvl(b."Charge",0)),'FM$9999999990.00PR') "Charge", to_char(sum(nvl(a."Net",0)
+ nvl(b."Net",0)),'FM$9999999990.00PR') "Net",
to_char(sum(nvl("Average",0)),'FM$9999999990.00PR') "30 Day Average"
-- Get Out, Cost, Net by date_out
from (select client_name "Client", count(date_sent) "Out",
round(sum(nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0)),2)"Cost",
round(sum(nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)),2) "Charge",
round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from a_chase.jim a
inner join a_chase.clients b
on a.clientid = b.clientid
where trunc(date_sent) = trunc(sdate)
group by client_name) a
--Get In, Cost, Net by date_In
left outer join (select client_name "Client", count(date_in) "In",
round(sum(nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0)),2)"Cost",
round(sum(nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)),2) "Charge",
round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from a_chase.jim a
inner join a_chase.clients b
on a.clientid = b.clientid
where trunc(date_in) = trunc(sdate)
group by client_name) b
on a."Client" = b."Client"
-- Get 30 Day Average
inner join (select client_name, round(avg("Net"),2) "Average" from
(select trunc(date_in) "In", client_name, round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from a_chase.jim a
inner join a_chase.clients b
on a.clientid = b.clientid
where trunc(date_in) between (trunc(sdate) - 30) and trunc(sdate)
group by trunc(date_in), client_name)
group by client_name) c
on a."Client" = c.client_name
group by a."Client"
union --BU_TS
select 'bu_ts' "Location",a."Client" "Client", sum(nvl(b."In",0)) "In", sum(nvl(a."Out",0)) "Out", to_char(sum(nvl(a."Cost",0)
+ nvl(b."Cost",0)),'FM$9999999990.00PR') "Cost",
to_char(sum(nvl(a."Charge",0) + nvl(b."Charge",0)),'FM$9999999990.00PR') "Charge", to_char(sum(nvl(a."Net",0)
+ nvl(b."Net",0)),'FM$9999999990.00PR') "Net",
to_char(sum(nvl("Average",0)),'FM$9999999990.00PR') "30 Day Average"
-- Get Out, Cost, Net by date_out
from (select client_name "Client", count(date_sent) "Out",
round(sum(nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0)),2)"Cost",
round(sum(nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)),2) "Charge",
round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from bu_ts.jim a
inner join bu_ts.clients b
on a.clientid = b.clientid
where trunc(date_sent) = trunc(sdate)
group by client_name) a
--Get In, Cost, Net by date_In
left outer join (select client_name "Client", count(date_in) "In",
round(sum(nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0)),2)"Cost",
round(sum(nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)),2) "Charge",
round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from bu_ts.jim a
inner join bu_ts.clients b
on a.clientid = b.clientid
where trunc(date_in) = trunc(sdate)
group by client_name) b
on a."Client" = b."Client"
-- Get 30 Day Average
inner join (select client_name, round(avg("Net"),2) "Average" from
(select trunc(date_in) "In", client_name, round(sum((nvl(product_fee,0) + nvl(copy_fee,0) + nvl(afee1,0) + nvl(afee2,0)+ nvl(afee3,0)
+ nvl(afee4,0)+ nvl(afee5,0)+ nvl(afee6,0)+ nvl(afee7,0)+ nvl(afee1,8)+ nvl(afee1,9)
+ nvl(afee1,10)) - (nvl(product_cost,0) + nvl(acost1,0) + nvl(acost2,0) + nvl(acost3,0) +
nvl(acost4,0) + nvl(acost5,0) + nvl(acost6,0) + nvl(acost7,0)
+ nvl(acost8,0) + nvl(acost9,0) + nvl(acost10,0))),2) "Net"
from bu_ts.jim a
inner join bu_ts.clients b
on a.clientid = b.clientid
where trunc(date_in) between (trunc(sdate) - 30) and trunc(sdate)
group by trunc(date_in), client_name)
group by client_name) c
on a."Client" = c.client_name
group by a."Client"
union --BU
select 'BU' "Location", a."Company" "Client", sum(nvl(b."In",0)) "In", sum(nvl(a."Out",0)) "Out", to_char(sum(nvl(a."Cost",0)
+ nvl(b."Cost",0)),'FM$9999999990.00PR') "Cost",
to_char(sum(nvl(a."Charge", 0) + nvl(b."Charge",0)),'FM$9999999990.00PR') "Charge", to_char(sum(nvl(a."Net",0)
+ nvl(b."Net",0)),'FM$9999999990.00PR') "Net",
to_char(sum(nvl("Average",0)),'FM$9999999990.00PR') "30 Day Average"
from
(--Out Query
select b.clcompanyname "Company", count(*) "Out",
round(sum(nvl(cost1,0) + nvl(cost2,0) + nvl(cost3,0) + nvl(cost4,0) + nvl(cost5,0) + nvl(costx1,0)
+ nvl(costx2,0)+ nvl(costx3,0)+ nvl(costx4,0)+ nvl(costx5,0)+ nvl(costy1,0)+ nvl(costy2,0)+ nvl(costy3,0)
+ nvl(costy4,0)+ nvl(costy5,0)),2) "Cost",
round(sum(nvl(fee1,0) + nvl(fee2,0) + nvl(fee3,0) + nvl(fee4,0) + nvl(fee5,0) + nvl(feex1,0)
+ nvl(feex2,0)+ nvl(feex3,0)+ nvl(feex4,0)+ nvl(feex5,0)+ nvl(feey1,0)+ nvl(feey2,0)+ nvl(feey3,0)
+ nvl(feey4,0)+ nvl(feey5,0)),2) "Charge",
round( sum(nvl(fee1,0) + nvl(fee2,0) + nvl(fee3,0) + nvl(fee4,0) + nvl(fee5,0) + nvl(feex1,0)
+ nvl(feex2,0)+ nvl(feex3,0)+ nvl(feex4,0)+ nvl(feex5,0)+ nvl(feey1,0)+ nvl(feey2,0)+ nvl(feey3,0)
+ nvl(feey4,0)+ nvl(feey5,0) - nvl(cost1,0) - nvl(cost2,0) - nvl(cost3,0) - nvl(cost4,0) - nvl(cost5,0) - nvl(costx1,0)
- nvl(costx2,0)- nvl(costx3,0)- nvl(costx4,0)- nvl(costx5,0)- nvl(costy1,0)- nvl(costy2,0)- nvl(costy3,0)
- nvl(costy4,0)- nvl(costy5,0)),2) "Net"
from [email protected] a
inner join [email protected] b
on a.clientid = b.clientid
where trunc(compdate1) = trunc(sdate)
or trunc(compdate2) = trunc(sdate)
or trunc(compdate3) = trunc(sdate)
or trunc(compdate4) = trunc(sdate)
or trunc(compdate5) = trunc(sdate)
group by b.clcompanyname) a
--in Query
left outer join (select b.clcompanyname "Company", count(a.loandatein) "In",
round(sum(nvl(cost1,0) + nvl(cost2,0) + nvl(cost3,0) + nvl(cost4,0) + nvl(cost5,0) + nvl(costx1,0)
+ nvl(costx2,0)+ nvl(costx3,0)+ nvl(costx4,0)+ nvl(costx5,0)+ nvl(costy1,0)+ nvl(costy2,0)+ nvl(costy3,0)
+ nvl(costy4,0)+ nvl(costy5,0)),2) "Cost",
round(sum(nvl(fee1,0) + nvl(fee2,0) + nvl(fee3,0) + nvl(fee4,0) + nvl(fee5,0) + nvl(feex1,0)
+ nvl(feex2,0)+ nvl(feex3,0)+ nvl(feex4,0)+ nvl(feex5,0)+ nvl(feey1,0)+ nvl(feey2,0)+ nvl(feey3,0)
+ nvl(feey4,0)+ nvl(feey5,0)),2) "Charge",
round( sum(nvl(fee1,0) + nvl(fee2,0) + nvl(fee3,0) + nvl(fee4,0) + nvl(fee5,0) + nvl(feex1,0)
+ nvl(feex2,0)+ nvl(feex3,0)+ nvl(feex4,0)+ nvl(feex5,0)+ nvl(feey1,0)+ nvl(feey2,0)+ nvl(feey3,0)
+ nvl(feey4,0)+ nvl(feey5,0) - nvl(cost1,0) - nvl(cost2,0) - nvl(cost3,0) - nvl(cost4,0) - nvl(cost5,0) - nvl(costx1,0)
- nvl(costx2,0)- nvl(costx3,0)- nvl(costx4,0)- nvl(costx5,0)- nvl(costy1,0)- nvl(costy2,0)- nvl(costy3,0)
- nvl(costy4,0)- nvl(costy5,0)),2) "Net"
from [email protected] a
inner join [email protected] b
on a.clientid = b.clientid
where trunc(a.loandatein) = trunc(sdate)
group by b.clcompanyname) b
on a."Company" = b."Company"
--Avg Query
inner join (select "Company", round(avg("Net"),2) "Average" from
(select trunc(loandatein) "In", b.clcompanyname "Company",
round( sum(nvl(fee1,0) + nvl(fee2,0) + nvl(fee3,0) + nvl(fee4,0) + nvl(fee5,0) + nvl(feex1,0)
+ nvl(feex2,0)+ nvl(feex3,0)+ nvl(feex4,0)+ nvl(feex5,0)+ nvl(feey1,0)+ nvl(feey2,0)+ nvl(feey3,0)
+ nvl(feey4,0)+ nvl(feey5,0) - nvl(cost1,0) - nvl(cost2,0) - nvl(cost3,0) - nvl(cost4,0) - nvl(cost5,0) - nvl(costx1,0)
- nvl(costx2,0)- nvl(costx3,0)- nvl(costx4,0)- nvl(costx5,0)- nvl(costy1,0)- nvl(costy2,0)- nvl(costy3,0)
- nvl(costy4,0)- nvl(costy5,0)),2) "Net"
from [email protected] a
inner join [email protected] b
on a.clientid = b.clientid
where trunc(loandatein) between (trunc(sdate) - 30) and trunc(sdate)
group by trunc(loandatein), b.clcompanyname)
group by "Company") c
on a."Company" = c."Company"
group by a."Company";
return(ret_cursor);
end;
end;
I also have given htmldb_public_user grant/execute privileges on this function. Any help would be greatly appreciated.
Thanks,
Scottthanks, lalit. that's almost correct. htmldb_public_user is the username through which the mod_plsql DAD connects to the database. he has just the privs he needs to serve that function. the htmldb engine compiles into a FLOWS_010500 schema in the production release. having said that, lalit's suggestion is correct. you need to make sure your htmldb application schema (identified by the application-level attribute "Default Parsing Schema") has been directly granted privileges (directly as opposed to coming through a role rant) to run the objects it's calling.
hope this helps,
raj -
Slow performance of getObject on ref cursor
Hi guys and gals,
I have a performance issue in accessing data from a ref cursor. The getObject statement is unbelievably slow. In one case, it takes as long as 3 seconds to execute a single getObject statement. I have tested this using a simple System.currentTimeMillis() before and after the statement in question.
The basic scenario is as follows :
I utilise a class that calls an Oracle PL/SQL Stored Procedure using a CallableStatement. The calling class executes on a Web Server, the Oracle ( DB Server ) resides on a different machine ( standard 3 tier stuff ). Network traffic is not a problem. A number of the output parameters registered for the CallableStatement are ref cursors. After the CallableStatement is executed, I use a getObject and cast it to a Result Set object as follows :
objResults = (ResultSet) objCallable.getObject(18);
Where the 18th param has been registered like so :
objCallable.registerOutParameter (18, OracleTypes.CURSOR);
The volume of data is certainly not excessive, typically 80 - 100 rows of data that (eventually) find their way into the drop down list on a Web Page.
About 5 other ref cursors are used and their data accessed in like manner, some of these taking between 1 & 1.5 seconds each to execute. So the whole process of retrieving from these ref cursors via getObject can take almost 10 seconds.
Has anyone experienced this kind of performance problem before, and if so, how can this be optimized ?Hey folks. Has anybody out there got to the bottom of this? I currently have a situation where my querys can take less than half a second to execute and only return a few rows (which contain only 4 columns of data) taking at most a second to run and yet take up to 30 seconds to return the ResultSet. The most annoying thing is the lack of consistancy, it seems no matter how many times you run it you get different results.
I'm using classes12.jar driver and running on both Windows (development) and Linux(production) on Oracle 8.0.5.
Any help would be hugely appreciated. -
Hello,
I would like to know if the following is possible:
I'm getting data from different sources in a ref cursor. Is it possible to sort this ref cursor on a specific column in the ref cursor? If so, how?
Thanks
KarunaMy problem is ...
I take the ref cursor, and have added some more data in there, based on a few conditions from different tables. So my ref cursor actually has more relevant data as compared to its source. I now need to find a way to sort that info. Is there a way around this?
Thanks
Karuna -
Sorting the results returned by a Ref cursor
Hi All,
I have a scenario where i am asked to sort results returned by a ref cursor.
I have to pass the column to be sorted as the Input parameter to a stored procedure. I tried using 'order by sorting_parameter' in the ref cursor's select query, but it is not sorting the results. It is not throwing any error even.
Please help.
Many Thanks...Hi
i came across the below reply for a thread with the similar query as above.
<<
Justin Cave
Posts: 10,696
From: Michigan, USA
Registered: 10/11/99
Re: sort data in ref cursor
Posted: Feb 3, 2005 10:30 AM in response to: [email protected] Reply
No. You could sort the data in the SQL statement from which the REF CURSOR was created, but once you have a REF CURSOR, you cannot do anything but fetch from it.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
>>
So, the results from a ref cursor cant be sorted?? There is no way out?
Kindly advise. -
REF Cursor to be called via Reports 6i to display data
Hello Sir/Madam,
I'm new to REF Cursor, so I would appriciate your help..
My requirements are to display records higher then nine records for a given facilityID from the REGSTATVLVS table sorted by vlv_nbr (field)
So far, I have Icreated a package and when I execute this package it is prompting
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'LSTVLV_REFCUR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Note: I can display the data using "rownum", but that is not allowing me to sort it by valve#..
Sorry for redundancy for redency
CREATE OR REPLACE package GFS_ADMIN.vlvlst is
type v_cursor is REF CURSOR return REGSTATVLVS%rowtype;
PROCEDURE lstvlv_refcur(vlvno OUT v_cursor);
end;
CREATE OR REPLACE package body vlvlst is
PROCEDURE lstvlv_refcur(vlvno OUT v_cursor) IS
vlv_num varchar2(1000);
cursor vlv_cur is
select vlv_nbr
from REGSTATVLVS r
where r.fclty_seq_nbr = 83
order by vlv_nbr;
vlv_rcd vlv_cur%rowtype;
begin
if not vlv_cur%isopen then
open vlv_cur;
end if;
<<i_loop>> for i in 1..40 loop
fetch vlv_cur into vlv_rcd;
EXIT WHEN vlv_cur%NOTFOUND;
if ( i <= 9 ) then
vlv_num := vlv_rcd.vlv_nbr;
dbms_output.put_line(vlv_num);
-- dbms_output.put_line(vlv_rcd.vlv_nbr);
end if;
end loop;
end;
end;
variable v1 varchar2(6);
set serveroutput on
set autoprint on
execute vlvlst.lstvlv_refcur(:v1);
Regards,
VaniHello InoL,
REF Cursor is working better if it is a Navigator, Toad or SQLPlus.. but it is prompting all sort of errors when it is used within Reports 6i.. Here is what i was trying to achieve..
The purpose of "REF Cursor" is to retrieve multiple parameter values .. ( at least that is what GOOGLE results were about REF Cursor )..
At the same time, a function does not return more then one value and I'm not sure if Reports will allow to use a stored procedure. So, I started to use REF Cursor
"lstvlv_refcur(vlvno OUT v_cursor )" I'm trying to retrieve as I mentioned 9 valve records to one report and remaining valve records to 2nd report, which I'm calling it via SRW reports built-in package..
Here is the latest and greatest code on my REF Cursor..
1) created "global temporary table" one time..
2) within Reports added this package ( as a program unit )
package vlvlst_chk is
type vlvlst_rcd is RECORD
( fctly_seq_nbr number,
vlv_nbr varchar2(6));
type vlvno_refcur is REF CURSOR return vlvlst_rcd;
end;
3) then added a function called QR_1RefCurDS ( as a program unit )
function QR_1RefCurDS return vlvlst_chk.vlvno_refcur is
hld_vlvlst vlvlst_chk.vlvno_refcur;
fctno INTEGER;
vno varchar2(6);
begin
delete from fct_vlv_hold;
commit;
delete from fct_vlv_hold2;
commit;
open hld_vlvlst for
select r.fclty_seq_nbr,r.vlv_nbr
from REGSTATVLVS r
where r.fclty_seq_nbr = :p_2
order by vlv_nbr;
for i in 1..40 loop
fetch hld_vlvlst into fctno, vno;
exit when hld_vlvlst%notfound;
if i <= 9 then
insert into fct_vlv_hold
values(fctno, vno);
commit;
-- return hld_vlvlst;
elsif (i>9 and i<= 40) then
insert into fct_vlv_hold2
values(fctno, vno);
commit;
-- return hld_vlvlst;
else
return hld_vlvlst;
end if;
end loop;
close hld_vlvlst;
return hld_vlvlst;
end;
Here: prompts "REP-0065/REP-0200" error, but writes records to temp tables..
Question is why is it prompting this error?
Edited by: user11141511 on Jan 24, 2012 1:25 PM
Edited by: user11141511 on Jan 24, 2012 1:27 PM
Maybe you are looking for
-
Inter company AR/AP open item clearing - Urgent
Hi, my client is having 3 company codes, these companies exchange goods/services for which intercompany vendor and customer is billed.when it comes to clearing of open items in intercompany vendor/customer client demands some automated approach to re
-
Hi all, I created a smartform for barcode labels printing. The form has its own style and characters and paragraphs use a custom font (it's a Windows font I uploaded in the system). My smartform worked well, but when I newly tested it yesterday, only
-
Is there any difference between MODULES=(!foo) and MOD_BLACKLIST=(foo) I just think that maybe the MOD_BLACKLIST could be ripped out of rc.conf and only use MODULES=() with a ! to disable a module like in DAEMONS=() to clean it up a little bit, if th
-
Hey, new to Macs, IT for 5+ years, but i recently got my hands on a Macbook (white, 1.83 Core Duo, 2006) that wont charge or start on adapter only, the Magsafe stays green. It WILL start with a charged battery in though. I was wondering if anyone had
-
Why does spry vertical menu break in Chrome?
The submenu in this page does not appear in Chrome (the About Me link). www.steveelson.com. Works in FF, Safari, Opera and IE (although there are positioning and backgeround inconsistancies).