REF Cursor with search string
Hello all,.I'm trying to implement a procedure that returns records in the hr.employees table when search strings are passed.
Here is the package:
create or replace package search_app
IS
--
TYPE emp_rec IS RECORD (last_name hr.employees.last_name%TYPE
,first_name hr.employees.first_name%TYPE
,job_id hr.employees.first_name%TYPE
,salary hr.employees.salary%TYPE
,dept hr.departments.department_name%TYPE
);
--
TYPE emp_curs IS REF CURSOR RETURN emp_rec;
--
PROCEDURE get_employees (p_last_name_search IN hr.employees.last_name%TYPE
,p_first_name_search IN hr.employees.first_name%TYPE
,p_job_id_search IN hr.employees.job_id%TYPE
,pr_emps OUT emp_curs
,p_error OUT VARCHAR2
);
--
END search_app;
/
--
create or replace package body search_app
IS
--
PROCEDURE get_employees (p_last_name_search IN hr.employees.last_name%TYPE
,p_first_name_search IN hr.employees.first_name%TYPE
,p_job_id_search IN hr.employees.job_id%TYPE
,pr_emps OUT emp_curs
,p_error OUT VARCHAR2
)
IS
--
v_last_name_search hr.employees.last_name%TYPE;
v_first_name_search hr.employees.first_name%TYPE;
v_job_id_search VARCHAR2(100);
v_error_msg VARCHAR2(200);
--
BEGIN
--
OPEN pr_emps FOR
--
SELECT e.last_name
,e.first_name
,e.job_id
,e.salary
,d.department_name as dept
FROM hr.employees e
,hr.departments d
WHERE (e.first_name LIKE p_first_name_search
AND
--OR
e.last_name LIKE p_last_name_search
AND
--OR
e.job_id IN p_job_id_search
)
AND d.department_id = e.department_id;
--
p_error := v_error_msg;
--
EXCEPTION
--
WHEN OTHERS THEN
v_error_msg := SUBSTR(SQLERRM,1,200);
END get_employees;
--
END search_app;
/
sho error
---
here is the stub I am using to test.
--
set serveroutput on size 1000000
spool search_app_OR.log
--
DECLARE
--
v_first_name_search hr.employees.first_name%TYPE;
v_last_name_search hr.employees.last_name%TYPE;
v_job_id_search VARCHAR2(100);
my_emps search_app.emp_curs;
--
v_first_name hr.employees.first_name%TYPE;
v_last_name hr.employees.last_name%TYPE;
v_job_id VARCHAR2(100);
v_salary hr.employees.salary%TYPE;
v_dept_name hr.departments.department_name%TYPE;
v_error_msg VARCHAR2(200);
--
v_count PLS_INTEGER;
--
BEGIN
--
v_first_name_search := '%';
v_last_name_search := 'De%';
-- v_job_id_search := '%';
v_job_id_search := chr(40)||chr(39)||'AD_VP'||chr(39)||','||chr(39)||'SH_CLERK'||chr(39)||chr(41);
--
search_app.get_employees(p_last_name_search => v_last_name_search
,p_first_name_search => v_first_name_search
,p_job_id_search => v_job_id_search
,pr_emps => my_emps
,p_error => v_error_msg
);
--
LOOP
--
FETCH my_emps INTO v_last_name, v_first_name,v_job_id, v_salary, v_dept_name;
EXIT when my_emps%NOTFOUND;
--
DBMS_OUTPUT.PUT_LINE ('...last_name : '||v_last_name);
DBMS_OUTPUT.PUT_LINE ('...first_name: '||v_first_name);
DBMS_OUTPUT.PUT_LINE ('...job_id : '||v_job_id);
DBMS_OUTPUT.PUT_LINE ('...salary : '||v_salary);
DBMS_OUTPUT.PUT_LINE ('...dept : '||v_dept_name);
DBMS_OUTPUT.PUT_LINE ('...error_msg : '||v_error_msg);
--
END LOOP;
--
v_count := my_emps%ROWCOUNT;
--
CLOSE my_emps;
--
DBMS_OUTPUT.PUT_LINE ('v_last_name_search : '||v_last_name_search);
DBMS_OUTPUT.PUT_LINE ('v_first_name_search : '||v_first_name_search);
DBMS_OUTPUT.PUT_LINE ('v_job_id_search : '||v_job_id_search);
DBMS_OUTPUT.PUT_LINE ('num rec fetched : '||v_count);
--
EXCEPTION
WHEN OTHERS THEN
v_error_msg := SUBSTR(SQLERRM,1,200);
DBMS_OUTPUT.PUT_LINE ('error: '||v_error_msg);
END;
/
spool off
The results I get are as follows:When I OR in the request for get_employees for search parameters, 106 records are returned.
When I AND in the application, not of records returned is 0. It should return 2 records. Why is it not return all records? Any suggestion on how this procedure should be applied?
Thank you
Raman
Published by: rxshah on June 8, 2010 12:52 AM
Hello
The problem is that you try to implement a dynamic list in a static SQL statement:
job_id IN '(''AD_VP'',''SH_CLERK'')'
It will not work like that.
Check out these links for some tips:
http://tkyte.blogspot.com/2006/06/varying-in-lists.html
http://asktom.Oracle.com/pls/asktom/f?p=100:11:1242571115131928:P11_QUESTION_ID:110612348061
Tags: Database
Similar Questions
-
How to create a REF CURSOR from the static values?
We call for a program of power supply (from PL/SQL) that returns a string with data (separated by {}), lines like this:
Our PL/SQL procedure should exit a REF CURSOR with lines like this:{packetId:236,packetName:ADSL-320K-1M-3G}, {packetId:257,packetName:ADSL-1024K-1M-20G}, {packetId:232,packetName:ADSL-INTERANET-UNLIMITED}, {packetId:234,packetName:ADSL-512K-3M-16G}, .................. ......... .....
How do this pls?236, ADSL-320K-1M-3G 257, ADSL-1024K-1M-20G 232, ADSL-INTERANET-UNLIMITED 234, ADSL-512K-3M-16G ......... ......
Published by: Channa on May 18, 2012 02:30Hello
Channa wrote:
It's the first gives ORA-00942: table or view does not exist for the FROM t . When I can replace the t split_t , I get ORA-32031: illegal reference to a query with the term name.What is at stake here pls?
Use the name of your real of the table where I used t.
If you don't have a table, just a string, then select the double. -
How to call a stored procedure with a REF CURSOR output parameter
I'm looking forward to example calling a function/stored procedure with a REF CURSOR output parameter and get the result.
In other words, I have a stored function/procedure that runs a SELECT statement using the OCI library and then he could get the values of each row and each column.
I put a code snippet, it have only the main thing to call a simple stored procedure and to print the name of each column of the cursor, but I couldn t to print out values in the table that calls the stored procedure.
I understand that the next step is to call an OCIStmtFetch.
How to associate the slider with the OCIStmtFetch?
If you need more information, just tell me.
I use ANSI C with HP - UX (HP - UX C) operating system and Oracle 10 g.
Kind regards.
Antonio Garcia
/ * callOracleSP * /.
#include < stdio.h >
#include < string.h >
#include < oci.h >
#include < stdlib.h > to
char * pConnectChar = "Server";
char * pUsernameChar = "user";
char * pPasswordChar = "passwd";
char * sqlCharArray1 = "BEGIN SP_GETCITIES (:,: c); END; « ;
int retval;
UB4 parmcnt = 0;
UB4 pos2 = 0;
text * pcoln [20];
UB4 namelen [20];
char state_key [5];
OCIStmt * pOciStatement;
OCIStmt * pOciStatCursor;
OCIError * pOciError;
OCIEnv * pOciEnviron;
OCIServer * pOciServer;
OCISession * pOciSession;
OCISvcCtx * pOciServiceContext;
OCIBind * pOciBind [500];
OCIParam * pOciParam;
int main()
{
retval = OCIEnvCreate (& pOciEnviron, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
retval = OCIEnvInit (& pOciEnviron, OCI_DEFAULT, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciError, OCI_HTYPE_ERROR, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciServiceContext, OCI_HTYPE_SVCCTX, 0, NULL);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciStatement, OCI_HTYPE_STMT, 0, NULL);
retval = OCILogon (pOciEnviron, pOciError, & pOciServiceContext,(unsigned char *) pUsernameChar,
strlen (pUsernameChar), (unsigned char *) pPasswordChar, strlen (pPasswordChar).
(unsigned char *) pConnectChar, strlen (pConnectChar));
printf ("retval=%d\n",retval OCILogon);
retval = OCIStmtPrepare (pOciStatement, pOciError, (unsigned char *) sqlCharArray1, strlen (sqlCharArray1),)
OCI_NTV_SYNTAX, OCI_DEFAULT);
printf ("StmtPrepare retval=%d\n",retval);
retval = OCIHandleAlloc (pOciEnviron, (void *) & pOciStatCursor, OCI_HTYPE_STMT, 0, NULL);
retval = 1 OCIBindByPos(pOciStatement,&pOciBind[0], pOciError, (ub4), (void *) & state_key,)
((sb4) sizeof (state_key), SQLT_STR, (void *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT (ub4));
printf ("BindByPos OCI_HTYPE_STMT retval=%d\n",retval);
retval = OCIBindByPos(pOciStatement,&pOciBind[1], pOciError, (ub4) 2, (void *) & pOciStatCursor,)
((sb4) 0, SQLT_RSET, (void *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT (ub4));
printf ("BindByPos OCI_HTYPE_STMT retval=%d\n",retval);
strcpy (state_key, 'ca');
retval = OCIStmtExecute (pOciServiceContext, pOciStatement, pOciError, (ub4) 1, (ub4) 0,)
(OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT (ub4));
printf ("StmtExecute retval=%d\n",retval);
/ * How to get the values of the cursor? */
/ * Number of parameters of the cursor * /.
OCIAttrGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), (void *) & parmcnt,(ub4 *) 0,)
(ub4) (OCI_ATTR_PARAM_COUNT, pOciError);
printf ("\nNumber of the slider settings = %d\n",parmcnt);
for (int pos = 1; pos < = (int) parmcnt; pos ++)
{
OCIAttrGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), (void *) & pos2,(ub4 *) 0,)
(ub4) (OCI_ATTR_CURRENT_POSITION, pOciError);
retval = OCIParamGet ((void *) pOciStatCursor, OCI_HTYPE_STMT (ub4), pOciError, (void *) & pOciParam,)
POS (ub4));
OCIAttrGet pOciParam, (ub4) ((void*) OCI_DTYPE_PARAM,(void*) & pcoln [pos - 1],(ub4 *) & namelen [pos-1],)
(ub4) OCI_ATTR_NAME,(OCIError *) pOciError);
}
for (int i = 1; i < = (int) parmcnt; i ++)
printf ("%i\tNAME = % column. ("* s\n", i, namelen [i-1], pcoln [i-1]);
return 0;
}
This is the script that create the table, insert records and create the stored procedure
CREATE TABLE CITIES)
STATE_CODE VARCHAR2 (2) NULL,
CITY_CODE NUMBER (15.5) NULL,
CITY_NAME VARCHAR2 (30) NULL
)
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 30, 'SAN DIEGO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 40 'SACRAMENTO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('FL', 10, 'MIAMI')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('FL', 20, 'ORLANDO')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('NEW YORK', 10, 'NEW YORK')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('NEW YORK', 20, 'ALBANY')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 10, 'LOS ANGELES')
/
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES ('CA', 20, 'SAN FRANCISCO')
/
CREATE or REPLACE PACKAGE globalPkg AUTHID CURRENT_USER AS
/ * The following is specific global variables T/SQL. */
TYPE RCT1 IS REF CURSOR; / * new cursor low definition * /.
END globalPkg;
/
CREATE OR REPLACE PROCEDURE SP_ADDCITY)
P_STATE_CODE IN VARCHAR,
P_CITY_CODE NUMBER,
P_CITY_NAME IN VARCHAR2,
P_RETURN IN NUMBERS)
AS
StoO_error INTEGER;
StoO_selcnt INTEGER;
StoO_rowcnt INTEGER;
StoO_errmsg VARCHAR2 (255);
BEGIN
StoO_rowcnt: = 0;
StoO_error: = 0;
StoO_selcnt: = 0;
P_RETURN: = 0;
INSERT INTO CITIES (STATE_CODE, CITY_CODE, CITY_NAME)
VALUES (P_STATE_CODE, P_CITY_CODE, P_CITY_NAME);
StoO_rowcnt: = number of LINES SQL %;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
StoO_rowcnt: = 2;
WHILE OTHERS THEN
StoO_rowcnt: = 0;
StoO_selcnt: = 0;
StoO_error: = SQLCODE;
StoO_errmsg: = SQLERRM;
IF StoO_error! = 0 THEN
BEGIN
P_RETURN: = 1;
RETURN;
END;
END IF;
END;
/
CREATE OR REPLACE PROCEDURE SP_GETCITIES)
STATE_KEY IN VARCHAR,
RC1 IN OUT globalPkg.RCT1)
AS
StoO_error INTEGER;
StoO_selcnt INTEGER;
StoO_rowcnt INTEGER;
StoO_errmsg VARCHAR2 (255);
BEGIN
StoO_rowcnt: = 0;
StoO_error: = 0;
StoO_selcnt: = 0;
OPEN FOR RC1
SELECT STATE_CODE, CITY_CODE, FRANCISCO
CITIES
WHERE STATE_CODE = STATE_KEY
ORDER BY CITY_CODE;
StoO_rowcnt: = number of LINES SQL %;
EXCEPTION
WHILE OTHERS THEN
StoO_rowcnt: = 0;
StoO_error: = SQLCODE;
StoO_errmsg: = SQLERRM;
END;
/Hi Antonio,.
I see this:
c_buf=(ub1 **)calloc(sizeof(ub1 *),3); ... rc=OCIDefineByPos(pOciStatCursor,&pdef,(OCIError *)pOciError,pos,c_buf[pos-1],size+1,(ub2)type,(dvoid *)c_indp[pos-1],(ub2 *)0,(ub2 *)0,OCI_DEFAULT);
That I don't understand. You allocate space for 3 pointers ub1 but I don't see where these pointers are then initialized to point to where the data is to be stored.
I do not read correctly?
Sorry for posting code long, but here is an example of code that I have. It is much more 'code' for your code, but maybe that will be enough...
NOTE: This is just the code example and not rigorous. For example, I don't check the memory, allocations etc in this code!
Kind regards
Mark
#ifdef WIN32 #define _CRT_SECURE_NO_DEPRECATE 1 #endif #include
#include #include #include void checkerr(sword status, OCIError *errhp); int main(int argc, char *argv[]) { OCIEnv *envhp = NULL; /* OCI Environment handle */ OCIError *errhp = NULL; /* OCI Error handle */ OCISvcCtx *svchp = NULL; /* OCI Service Context handle */ OCIServer *srvhp = NULL; /* OCI Server handle */ OCISession *usrhp = NULL; /* OCI User Session handle */ OCIStmt *stmtp = NULL; /* OCI Statement handle */ OCIStmt *cursr = NULL; /* OCI Statement handle */ OCIParam *prmp1 = NULL; /* OCI Parameter handle */ OCIParam *prmp2 = NULL; /* OCI Parameter handle */ OCIParam *prmp3 = NULL; /* OCI Parameter handle */ OCIDefine *defp1 = NULL; /* OCI Define handle */ OCIDefine *defp2 = NULL; /* OCI Define handle */ OCIDefine *defp3 = NULL; /* OCI Define handle */ OCIBind *bndp1 = NULL; /* OCI Bind handle */ OCIBind *bndp2 = NULL; /* OCI Bind handle */ OCIBind *bndp3 = NULL; /* OCI Bind handle */ /* used to hold column width */ ub2 col_width; /* used to set the prefetch count */ ub4 prefetch_count = 32; /* will hold output from database */ oratext *pEmpId = NULL; oratext *pFirstName = NULL; oratext *pLastName = NULL; /* the anonymous block to execute */ /* this opens a ref cursor */ oratext *sqlstmt = "begin " \ " open :1 for " \ " select to_char(employee_id), " \ " first_name, " \ " last_name " \ " from hr.employees " \ " order by last_name, " \ " first_name; " \ "end;"; /* used to hold the results of each OCI call */ sword result = 0; /* Initialize and create a default environment */ result = OCIEnvCreate(&envhp, OCI_DEFAULT, (dvoid *) 0, 0, 0, 0, (size_t) 0, (dvoid **) 0); /* allocate an error handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* allocate a service context handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX, 0, (dvoid **) 0); /* allocate a server handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, 0, (dvoid **) 0); /* allocate a user session handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &usrhp, OCI_HTYPE_SESSION, 0, (dvoid **) 0); /* create a server context using the "ORADEMO" database */ result = OCIServerAttach(srvhp, errhp, "ORADEMO", (ub4) strlen("ORADEMO"), OCI_DEFAULT); /* set the server attribute in the service context handle */ result = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp); /* open the session with the database */ /* using external authentication */ result = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_EXT, OCI_DEFAULT); /* set the user session attribute in the service context handle */ result = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) usrhp, (ub4) 0, OCI_ATTR_SESSION, errhp); /* allocate the statement handle */ result = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmtp, OCI_HTYPE_STMT, 0, (dvoid **) 0); /* prepare the statement for execution */ result = OCIStmtPrepare(stmtp, errhp, sqlstmt, (ub4) strlen((char *) sqlstmt), OCI_NTV_SYNTAX, OCI_DEFAULT); /* allocate the handle for the ref cursor */ result = OCIHandleAlloc((dvoid *) envhp, (void **) &cursr, OCI_HTYPE_STMT, 0, NULL); /* bind the ref cursor parameter */ result = OCIBindByPos(stmtp, &bndp1, errhp, 1, &cursr, 0, SQLT_RSET, NULL, 0, NULL, 0, 0, OCI_DEFAULT); /* execute the statement */ result = OCIStmtExecute(svchp, stmtp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT); /* get parameter descriptor for first column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp1, (ub4) 1); /* get parameter descriptor for second column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp2, (ub4) 2); /* get parameter descriptor for third column */ result = OCIParamGet((dvoid *) cursr, OCI_HTYPE_STMT, errhp, (dvoid **) &prmp3, (ub4) 3); /* get the first column width in characters */ result = OCIAttrGet((dvoid*) prmp1, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pEmpId = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the first column in the results */ result = OCIDefineByPos(cursr, &defp1, errhp, 1, (dvoid *) pEmpId, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* get the second column width in characters */ result = OCIAttrGet((dvoid*) prmp2, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pFirstName = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the second column in the results */ result = OCIDefineByPos(cursr, &defp2, errhp, 2, (dvoid *) pFirstName, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* get the third column width in characters */ result = OCIAttrGet((dvoid*) prmp3, (ub4) OCI_DTYPE_PARAM, (dvoid*) &col_width, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, errhp); /* allocate memory to hold the result */ pLastName = (oratext *) malloc(sizeof(oratext) * (col_width + 1)); /* define the third column in the results */ result = OCIDefineByPos(cursr, &defp3, errhp, 3, (dvoid *) pLastName, (sword) col_width + 1, SQLT_STR, (dvoid *) NULL, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); /* loop through and print the results */ while ((result = OCIStmtFetch(cursr, errhp, (ub4) 1, (ub2) OCI_FETCH_NEXT, (ub4) OCI_DEFAULT)) == OCI_SUCCESS) { printf("Employee ID: %s\n", pEmpId); printf(" First Name: %s\n", pFirstName); printf(" Last Name: %s\n\n", pLastName); } /* free allocated memory */ free(pEmpId); free(pFirstName); free(pLastName); pEmpId = NULL; pFirstName = NULL; pLastName = NULL; /* terminate the session with the database */ result = OCISessionEnd(svchp, errhp, usrhp, OCI_DEFAULT); /* detach from the server */ result = OCIServerDetach(srvhp, errhp, OCI_DEFAULT); /* deallocate the environment handle */ /* OCI will deallocate the child handles */ result = OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return OCI_SUCCESS; } void checkerr(sword status, OCIError *errhp) { oratext errbuf[512]; sb4 errcode = 0; switch (status) { case OCI_SUCCESS: break; case OCI_ERROR: case OCI_SUCCESS_WITH_INFO: (void) OCIErrorGet((dvoid *) errhp, (ub4) 1, (oratext *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); (void) printf("Error: %.*s\n", sizeof(errbuf), errbuf); break; case OCI_NEED_DATA: (void) printf("Error - OCI_NEED_DATA\n"); break; case OCI_NO_DATA: (void) printf("Error - OCI_NO_DATA\n"); break; case OCI_INVALID_HANDLE: (void) printf("Error - OCI_INVALID_HANDLE\n"); break; case OCI_STILL_EXECUTING: (void) printf("Error - OCI_STILL_EXECUTING\n"); break; case OCI_CONTINUE: (void) printf("Error - OCI_CONTINUE\n"); break; default: break; } } -
How to perform procedures with REF CURSOR &; PLSQL settings table in SQL Dev?
I have 3 SQL Developer. I create a procedure with a set of tables/PL/SQL as an input parameter and a SYS_REFCURSOR as output parameter.
When I press the green arrow (Run button) block the dialog box run PL/SQL PL/SQL has no place at the PL/SQL table with records of entry and also to see the output of the SYS_REFCURSOR.
I get the error ORA-06550 and many other errors when I run the present.
How to do this please?In the debug code, lose the ABS. Prefix the SYS_REFCURSOR QC_SQL_DEVELOPER_TEST.
Also, do not use same name of your settings on your bind variable (so e.g.: OUT_REF_CURSOR).
REF CURSOR is supported output types, so you will get the output of their share in the output tab.
However the output system does not recognize your own types as your PL/SQL table. You need to write an exit procedure yourself, indexing correctly, using the DBMS_OUTPUT.Have fun
K. -
Problem with weakly typed ref Cursor
I have problem with a procedure in a package that returns a weakly typed reference cursor.
The cursor that is returned to give the desired result when connected as sys and sysdba.
But the user has rights of execution on the slider isn't a result.
If I run the query for the cursor as a user I get the desired result
I tried creating a new procedure outside of the package and it still doesn't work the way I want it.
I tested changing authentication none in sqlnet.ora and still no results
Procedure
create or replace procedure ReportUnits (in the SYS_REFCURSOR cResult)
AS
Start
Open the cResult for
Select distinct c.company, c.name, c.companycode
all_views o, fakta.balance_table b, fakta.company c
where b.company = c.company
and o.view_name like upper(b.company ||) '_' || b.balance_table | '_view %');
end ReportUnits;
Tried the two SYS_REFCURSOR and
Typedef t_cursor as ref cursor;
I get no error message.802379 wrote:
Sorry user_views does not solve the problem using all user_views I get no result points of view because interested does not belong to the user.So who owns the upper(b.company ||) '_' || b.balance_table | views of '_view' %)? And the package is created with copyrighted (by default) or with authid current_user? If the package is created with copyrighted, it still operates as package regardles of owner who runs it. Package owner (not the appellant) must have accsess to view granted directly, without going through role (rights define stored objects do honor no roles). And since rights define package runs as the owner of package, all_views will show views package owner has access, not package calling.
One last thing - what happens if more than one schema has upper(b.company ||) '_' || b.balance_table | views of '_view' %)? Do you want them or only in a specific schema?
SY.
-
Call a procedure stored via JDBC with a REF CURSOR * input variable.
Hello
S/n of my client has provided me with a stored procedure that I need to call to get information about products prices. Something along the lines of:
some_package.getPrices (products IN OUT csr_type);
where csr_type represents a REF CURSOR. This cursor has a product ID column and a price column. The plan is to move the cursor with the populated product id column and have the routine decorate the slider with the prices.
The setting in this plan is that it seems that it is not possible to go from JDBC REF CURSOR in . However, I'm not sure, since I can only find throw comments on various forums (e.g. http://www.orafaq.com/forum/t/35088/0/), without any reference. If I go back to the client and tell them that their idea will not work, I prefer to be able to point them to a documentation somewhere
So I guess my questions are:
1. it is indeed impossible to pass a REF CURSOR type as a variable input on JDBC to a stored procedure?
2. is there information I can do about my client?
3. am I on the right track thinking I need to go down the path of an array of objects?
Thank you very much to anyone who can help
Peter Svehla.Hi Peter,.
I see it, missed that you actually want to pass in a list of products. Don't think that you can do this with the current procedure. A slider is just a pointer to a result set.
How the result set is created and what it looks like, is determined when the cursor is opened.What does this procedure with the cursor? -It does not have some OPEN
TO ; inside? Or, perhaps, you are supposed to send in an open cursor (does not much sense to me)?
Concerning
Peter -
Made Oracle 6i LOV with ref cursor returned by a function of database
Hi all
I want to dynamically create a LOV in oracle forms 6i using the value of ref cursor returned by the function of database.
is this possible?
Using loop, I could able to display the values returned by the ref cursor, but how can I assign these values in the LOV?
You will need to loop through your REF Cursor and assign each value to a group of registration of forms and then assign the Group Record to your LOV. Take a look at the built-ins CREATE_GROUP, ADD_GROUP_COLUMN and ADD_GROUP_ROW in the help system of forms for more information about how to use these built-ins and examples of how to use them.
Craig...
-
Hi all
We declare a ref cursor using:
type ecur is ref cursor;
ecur C1;
Instead, why we can't have a direct keyword as
REF C1 cursor;
Kind regards
Sri Ram.You can :))
c1 SYS_REFCURSOR;
-
Error when you work with Ref Cursor
Hi, I tried the following, but the err
Error isDECLARE TYPE ref_nm IS REF CURSOR; vref REF_NM; vemp emp%rowtype; BEGIN OPEN vref FOR SELECT ename ,sal FROM EMP; LOOP FETCH vref INTO vemp; EXIT WHEN vref%NOTFOUND; DBMS_OUTPUT.PUT_LINE ( vemp.ename ||','||vemp.sal ); END LOOP; CLOSE vref; END;
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
Use you this structure as a buffer of extraction:
PEMV emp % rowtype;This structure contains the whole line of EMP - all columns.
That's what you're looking for cursor - 2 columns (not the whole line):
Vref OPEN for SELECT ename, sal of EMP;You can not expect from PL/SQL to find out how to move the 2 column values in a structure that has more than just 2 columns. The error message is quite clear about this - read and think what is transport of the error.
-
Hello
I have a package that returns a Ref Cursor, in this procedure, I have a dynamic sql code that is built according to certain values, and the query is a select query, is it possible that I can put that dynamic sql in the ref cursor and return of the procedure.
Or y at - it no alternative better workaround.
Thanks in advance.
NaveenYes you can.
Try this...
create or replace package test_pack is type ref_cur is ref cursor; procedure just_print(ref_var ref_cur); end; / create or replace package body test_pack is procedure just_print(ref_var ref_cur) is l_var emp%rowtype; begin loop fetch ref_var into l_var; exit when ref_var%notfound; dbms_output.put_line(l_var.ename); end loop; end; end; / declare cur_var test_pack.ref_cur; dsql varchar2(100); begin dsql := 'select * from emp where deptno=10'; open cur_var for dsql; test_pack.just_print(cur_var); end; / CLARK KING MILLER PL/SQL procedure successfully completed. Elapsed: 00:00:00.00
Kind regards
Prazy -
How to run the packaged procedure with Ref Cursor
Hello.
The question may be very simple for you... but I was confused how to run
I have the following package
But I want to test (run) this procedure...CREATE OR REPLACE PACKAGE CURSPKG AS TYPE T_CURSOR IS REF CURSOR; PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, IO_CURSOR IN OUT T_CURSOR); END CURSPKG; / CREATE OR REPLACE PACKAGE BODY CURSPKG AS PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, IO_CURSOR IN OUT T_CURSOR) IS V_CURSOR T_CURSOR; BEGIN IF N_EMPNO <> 0 THEN OPEN V_CURSOR FOR SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO AND EMP.EMPNO = N_EMPNO; ELSE OPEN V_CURSOR FOR SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO; END IF; IO_CURSOR := V_CURSOR; END OPEN_ONE_CURSOR; END CURSPKG; /
But confused how to have Ref Cursor
Could you help me in this...
Thank youYou must declare a variable of type T_CURSOR and pass it to the procedure like this.
declare lOutCursor CURSPKG.T_CURSOR; begin CURSPKG.OPEN_ONE_CURSOR(
, lOutCursor); end; -
The use of bind variables in dynamic query created for Ref Cursor
Hello
I'm in a situation where there is a Ref cursor to which the query is built execution based on a loop. This is why the number of links would be known until the program runs.
The application is currently using literals instead of bind variables.
code snippet of the above is
strSql: = "select * from emp where 1 = 1 and ().
loop cursor1
If cond is true then
strSql = strSql | "ename = ' |" Cursor1.ColumnName;
end loop;
Open cursor2 for strSql;
How to use links in the example above.sb92075 wrote:
user13019948 wrote:
HelloHere is the code I have my trying to change literal-based link to the base.
What do you mean by "based bind?
who, what, how determines the values to be 'bound '?
He's referring to the coding style. He is currently using concatenated literal, and the goal is to change it to use the bindings.
If I understand this it is known as method 4 dynamic SQL and requires DBMS_SQL. There are examples autour but they vary according to the type of statement being generated - SELECT statements require column lists to be parsed, unlike the INSERT/UPDATE/DELETE.
This came up recently on my current project and I hit a demo. Here a table of names and values accepted procedure and had to build these in a single WHERE clause along the lines of
AND t_names(i) = t_values(i)
for an undetermined number of elements in the array. For this demonstration, I used a table that we called "attribute" (don't ask) which has columns including 'attribute_id' and 'name', and I need to build a query along the lines of
select description from attribute where attribute_id = :b1 and name = :b2
by the way '1012' and 'ISIN' respectively. (I use a table better and after a CREATE statement for her but I have to rush right now, sorry).
declare k_sql_base constant varchar2(500) := 'select description from attribute'; t_names constant varchar2_t := varchar2_t('attribute_id', 'name'); t_values constant varchar2_t := varchar2_t('1012', 'ISIN'); l_sql varchar2(500) := k_sql_base; l_rows_fetched integer := 0; l_value varchar2(4000); l_cursor_handle integer; begin -- Construct the SQL statement with column names and bind variables e.g. -- 'select description from mars.attribute where attribute_id = :b1 and name = :b2' for i in t_names.first .. t_names.last loop l_sql := l_sql || case i when t_names.first then ' where ' else ' and ' end || t_names(i) || ' = :b' || i; end loop; dbms_output.put_line('SQL statment = ' || l_sql); -- Parse the statement we built above (the remaining steps require a parsed cursor): l_cursor_handle := dbms_sql.open_cursor; dbms_sql.parse(l_cursor_handle, l_sql, dbms_sql.native); -- Associate the 1st column of output with variable l_value - required for SELECT statements: -- (actually the 3rd param here 'column' seems to be only used to get a datatype, in this case we want a string - -- dbms_sql.column_value actually extracts the value into a specified variable, which can be different. -- All examples in the documentation pass a local variable without further comment, so not entirely clear what this does other than set the output datatype.) dbms_sql.define_column(l_cursor_handle, 1, l_value, 4000); -- Now go through values array binding actual values to :bn variables in the cursor (similar to USING clause of EXECUTE IMMEDIATE) for i in t_values.first .. t_values.last loop dbms_sql.bind_variable(l_cursor_handle, ':b'||i, t_values(i)); dbms_output.put_line('Bound :b'||i || ' as ' || t_values(i)); end loop; -- Open the cursor and fetch the result (no loop here because we are expecting a single-row result): l_rows_fetched := dbms_sql.execute_and_fetch(l_cursor_handle); -- 'Returns value of the cursor element for a given position in a cursor' -- Copy the value of column 1 to variable l_value (has to match -- dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_output.put_line('Result = ''' || l_value || ''''); dbms_sql.close_cursor(l_cursor_handle); end;
Hope that helps...
-
REF cursor in sql dynamic help to run immediately
Hello
How can we get the Ref cursor out a dynamic sql statement by executing immediate proceedings
for example, immediately run ' open CROR for select * from dynamicTable'
in this area, CROR is a dynamic cursor and table name is dynamic (known at run time), and we can't write static sql statement.
Thank youI don't know what exactly you are after but here is a sample of what can be done.
SQL> VAR r REFCURSOR; SQL> BEGIN 2 OPEN :r FOR 'SELECT * FROM DUAL'; 3 END; 4 / PL/SQL procedure successfully completed. SQL> PRINT r D - X
You can use the OPEN... FOR education with a dynamic string.
-
Hello
I use ADF 11 g business components.
I have a function that returns a type weak ref cursor. Number of columns is not known until the function is executed. Is there a way to create a display programmatically on this Ref Cursor object?
Published by: Deniz Gulmez on 10.Oca.2010 23:40You can try with
public AttributeDef addDynamicAttribute(java.lang.String attrName) // or public AttributeDef addDynamicAttributeWithType(java.lang.String attrName, java.lang.String javaTypeName, java.lang.String transientExpression)
Timo
-
Is it not possible to get (or use) a search string that includes a reference object?
Hello world
I try to get the full search string property for the property "xl" seen below at the bottom of the image.
I can go as far as the other results [0] (which is a reference), but after that, it seems not possible to access the "xl".
"TS. SequenceCall.ResultList [0]. TS. SequenceCall.ResultList [0]. TS. SequenceCall.ResultList [0]. "AdditionalResults [0].
If I get the interface for the object of 'xl' and then ask 'place' in what concerns the "MainSequenceResults", I get an empty string.
Get the property for container objects and properties in the object "xl" also give the same result when I call «GetLocation (...)» with the MasterSequenceResults the TopObject property.
Is it not possible to have a search string that includes a reference object?
Thank you
Ronnie
Hi Ronnie,.
It is possible, but to get this value, you can use the TestStand API. I have attached a sequence file that shows how to get the values of reference of object using AsPropertyObject, and then the associated GetVal method. It is much easier to understand with an example. Let me know if you have any questions about the sample file of sequence.
Maybe you are looking for
-
Firefox can't Zone Alarm. And based on past experiences, you will support is no longer Firefox 6. There is NO WAY, I am browsing without a firewall. What am I supposed to do?
-
AS100V - does not work after Firmware update
Hi there has anyone else had this problem? I was at half way through the update of the firmware v 2.0 and then the installer said there is a mistake and to reboot, so I did, and now all I get is the red light on the back of the camera. I tried to res
-
How do you get MY photos to display the photos instead of written discription
Hello. I'm in MY pictures and I would program photo poster not written description of the pix for example. jpg, date, etc. How to move this back to this form of display? thanx, Deb
-
Installed updates available every day
My PC asks me if I want to download updates 5 same again and again. I keep saying downloads are successful and I look at the history of downloads and they are there. But my PC still keeps asking me over and over again of downloadSecurity update for W
-
Every day, without fail, I get a notification saying to transfer data. From what I understand (be illiterate technicallly), I have not much to transfer. It's my head. What can I do to stop this notification popping up all the time?