Best way to write a function that returns query texts
Hi, I need to write a function that will take a parameter containing table name and based upon it returns the text of a query. Here is what I am thinking of doing it but would like to get feedback whether there is a better way to do it.
For example:
FUNCTION getSQL(p_1 VARCHAR2, p_2 VARCHAR2) RETURN VARCHAR2
IS
sql varchar2(2000);
BEGIN
--Here i want IF THEN ELSE part and thes based upon it return a desired sql. There will be more than a dozen sqls that the function will return.
IF p_1='Employee' Then
sql:='Select * from employee Where employee_id=p_2;'
Else If p_1='Departement' Then
sql:='Select col1, col2, col3,';
sql:=sql || ' col4, col5, col6';
sql:=sql || ' Where dept_id=p_2';
Else If.....
Else If.....
Else If.....
Else If.....
End IF;
return sql;
END;Edited by: dreporter on Sep 20, 2010 8:43 AM
I'm never sure I understand the desire to put lots of cursors into a single stored procedure, given that they are inherently different things.
However, assuming you have made the above decision, you should almost certainly look at using the overload of dbms_xmlgen.newcontext that accepts a cursor parameter rather than a string. This would allow you to return cursors from your function rather than strings, giving you the option to use bind variables (and prevent associated SQL injection) and perhaps not even do dynamic SQL at all, something like this perhaps...
FUNCTION get_cursor (
p_cursor_type VARCHAR2,
p_cursor_parameter VARCHAR2)
RETURN sys_refcursor
IS
v_sys_cursor sys_refcursor;
BEGIN
CASE p_cursor_type
WHEN 'Employee' THEN
OPEN v_cursor FOR
SELECT empno, ename
FROM emp
WHERE empno = TO_NUMBER (p_cursor_parameter);
WHEN 'Department' THEN
OPEN v_cursor FOR
SELECT deptno, dname
FROM dept
WHERE deptno = TO_NUMBER (p_cursor_parameter);
END CASE;
RETURN v_sys_cursor;
END get_cursor;
/
Similar Messages
-
SOLVED: How can I use or call a function that returns %ROWTYPE?
Hi
edit: you can probably skip all this guff and go straight to the bottom...In the end this is probably just a question of how to use a function that returns a %rowtype. Thanks.
Currently reading Feuerstein's tome, 5th ed. I've downloaded and run the file genaa.sp, which is a code generator. Specifically, you feed it a table name and it generates code (package header and package body) that will create a cache of the specified table's contents.
So, I ran:
HR@XE> @"C:\Documents and Settings\Jason\My Documents\Work\SQL\OPP5.WEB.CODE\OPP5.WEB.CODE\genaa.sp"
749 /
Procedure created.
HR@XE> exec genaa('EMPLOYEES');which generated a nice bunch of code, viz:
create or replace package EMPLOYEES_cache is
function onerow ( EMPLOYEE_ID_in IN HR.EMPLOYEES.EMPLOYEE_ID%TYPE) return HR.EMPLOYEES%ROWTYPE;
function onerow_by_EMP_EMAIL_UK (EMAIL_in IN HR.EMPLOYEES.EMAIL%TYPE) return HR.EMPLOYEES%ROWTYPE;
procedure test;
end EMPLOYEES_cache;
create or replace package body EMPLOYEES_cache is
TYPE EMPLOYEES_aat IS TABLE OF HR.EMPLOYEES%ROWTYPE INDEX BY PLS_INTEGER;
EMP_EMP_ID_PK_aa EMPLOYEES_aat;
TYPE EMP_EMAIL_UK_aat IS TABLE OF HR.EMPLOYEES.EMPLOYEE_ID%TYPE INDEX BY HR.EMPLOYEES.EMAIL%TYPE;
EMP_EMAIL_UK_aa EMP_EMAIL_UK_aat;
function onerow ( EMPLOYEE_ID_in IN HR.EMPLOYEES.EMPLOYEE_ID%TYPE)
return HR.EMPLOYEES%ROWTYPE is
begin
return EMP_EMP_ID_PK_aa (EMPLOYEE_ID_in);
end;
function onerow_by_EMP_EMAIL_UK (EMAIL_in IN HR.EMPLOYEES.EMAIL%TYPE)
return HR.EMPLOYEES%ROWTYPE is
begin
return EMP_EMP_ID_PK_aa (EMP_EMAIL_UK_aa (EMAIL_in));
end;
procedure load_arrays is
begin
FOR rec IN (SELECT * FROM HR.EMPLOYEES)
LOOP
EMP_EMP_ID_PK_aa(rec.EMPLOYEE_ID) := rec;
EMP_EMAIL_UK_aa(rec.EMAIL) := rec.EMPLOYEE_ID;
end loop;
END load_arrays;
procedure test is
pky_rec HR.EMPLOYEES%ROWTYPE;
EMP_EMAIL_UK_aa_rec HR.EMPLOYEES%ROWTYPE;
begin
for rec in (select * from HR.EMPLOYEES) loop
pky_rec := onerow (rec.EMPLOYEE_ID);
EMP_EMAIL_UK_aa_rec := onerow_by_EMP_EMAIL_UK (rec.EMAIL);
if rec.EMPLOYEE_ID = EMP_EMAIL_UK_aa_rec.EMPLOYEE_ID then
dbms_output.put_line ('EMP_EMAIL_UK lookup OK');
else
dbms_output.put_line ('EMP_EMAIL_UK lookup NOT OK');
end if;
end loop;
end test;
BEGIN
load_arrays;
end EMPLOYEES_cache;
/which I have run successfully:
HR@XE> @"C:\Documents and Settings\Jason\My Documents\Work\SQL\EMPLOYEES_CACHE.sql"
Package created.
Package body created.I am now trying to use the functionality within the package.
I have figured out that the section
BEGIN
load_arrays;
end EMPLOYEES_cache;
/is the initialization section, and my understanding is that this is supposed to run when any of the package variables or functions are referenced. Is that correct?
With that in mind, I'm trying to call the onerow() function, but it's not working:
HR@XE> select onerow(100) from dual;
select onerow(100) from dual
ERROR at line 1:
ORA-00904: "ONEROW": invalid identifier
HR@XE> select employees_cache.onerow(100) from dual;
select employees_cache.onerow(100) from dual
ERROR at line 1:
ORA-06553: PLS-801: internal error [55018]
HR@XE> select table(employees_cache.onerow(100)) from dual;
select table(employees_cache.onerow(100)) from dual
ERROR at line 1:
ORA-00936: missing expressionHe provides the code genaa.sp, and a very brief description of what it does, but doesn't tell us how to run the generated code!
Now, I have just done some googling, and it seems that what I am trying to do isn't possible. Apparently %ROWTYPE is PL/SQL, and not understood by SQL, so you can't call onerow() from sql. Correct?
So I try wrapping the call in an exec:
HR@XE> exec select employees_cache.onerow(100) from dual;
BEGIN select employees_cache.onerow(100) from dual; END;
ERROR at line 1:
ORA-06550: line 1, column 30:
PLS-00382: expression is of wrong type
ORA-06550: line 1, column 7:
PLS-00428: an INTO clause is expected in this SELECT statement
HR@XE> exec select table(employees_cache.onerow(100)) from dual;
BEGIN select table(employees_cache.onerow(100)) from dual; END;
ERROR at line 1:
ORA-06550: line 1, column 14:
PL/SQL: ORA-00936: missing expression
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored
HR@XE> exec employees_cache.onerow(100)
BEGIN employees_cache.onerow(100); END;
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00221: 'ONEROW' is not a procedure or is undefined
ORA-06550: line 1, column 7:
PL/SQL: Statement ignoredNo joy.
Of course, now that I'm looking at it again, it seems that the way to go is indicated by the first error:
PLS-00428: an INTO clause is expected in this SELECT statement
So am I supposed to create a type of EMPLOYEES%ROWTYPE in a PL/SQL procedure, and the idea of this code, is that the first call to onerow() runs the initialiation code, which populates the cache, and all subsequent calls to onerow() (whether by my session or any other) will use the cache?
I've had a stab at this, but still, no joy:
create or replace procedure testcache is
emp employees%rowtype;
begin
select employees_cache.onerow(100) from dual into emp;
dbms_output.put_line('Emp id: ' || emp.employee_id);
end testcache;
show errors
HR@XE> @testcache.sql
Warning: Procedure created with compilation errors.
Errors for PROCEDURE TESTCACHE:
LINE/COL ERROR
4/9 PL/SQL: SQL Statement ignored
4/54 PL/SQL: ORA-00933: SQL command not properly ended
HR@XE>Have a feeling this should be really easy. Can anybody help?
Many thanks in advance.
Jason
Edited by: 942375 on 08-Feb-2013 11:45>
Ha, figured it out
>
Hopefully you also figured out that the example is just that: a technical example of how to use certain Oracle functionality. Unfortunately it is also an example of what you should NOT do in an actual application.
That code isn't scaleable, uses expensive PGA memory, has no limit on the amount of memory that might be used and, contrary to your belief will result in EVERY SESSION HAVING ITS OWN CACHE of exactly the same data if the session even touches that package.
Mr. Feuerstein is an expert in SQL and PL/SQL and his books cover virtually all of the functionality available. He also does an excellent job of providing examples to illustrate how that functionality can be combined and used. But the bulk of those examples are intended solely to illustrate the 'technical' aspects of the technology. They do not necessarily reflect best practices and they often do not address performance or other issues that need to be considered when actually using those techniques in a particular application. The examples show WHAT can be done but not necessarily WHEN or even IF a given technique should be used.
It is up to the reader to learn the advantages and disadvantages of each technicalogical piece and determine when and how to use them.
>
Now, I have just done some googling, and it seems that what I am trying to do isn't possible. Apparently %ROWTYPE is PL/SQL, and not understood by SQL, so you can't call onerow() from sql. Correct?
>
That is correct. To be used by SQL you would need to create SQL types using the CREATE TYPE syntax. Currently that syntax does not support anything similar to %ROWTYPE.
>
So am I supposed to create a type of EMPLOYEES%ROWTYPE in a PL/SQL procedure, and the idea of this code, is that the first call to onerow() runs the initialiation code, which populates the cache, and all subsequent calls to onerow() (whether by my session or any other) will use the cache?
>
NO! That is a common misconception. Each session has its own set of package variables. Any session that touches that package will cause the entire EMPLOYEES table to be queried and stored in a new associative array specifically for that session.
That duplicates the cache for each session using the package. So while there might be some marginal benefit for a single session to cache data like that the benefit usually disappears if multiple sessions are involved.
The main use case that I am aware of where such caching has benefit is during ETL processing of staged data when the processing of each record is too complex to be done in SQL and the records need to be BULK loaded and the data manipulated in a loop. Then using an associative array as a lookup table to quickly get a small amount of data can be effective. And if the ETL procedure is being processed in parallel (meaning different sessions) then for a small lookup array the additional memory use is tolerable.
Mitigating against that is the fact that:
1. Such frequently used data that you might store in the array is likely to be cached by Oracle in the buffer cache anyway
2. Newer versions of Oracle now have more than one cache
3. The SQL query needed to get the data from the table will use a bind variable that eliminates repeated hard parsing.
4. The cursor and the buffer caches ARE SHARED by multiple sessions globally.
So the short story is that there would rarely be a use case where ARRAYs like that would be preferred over accessing the data from the table. -
Function that returns currently selected cell address or column
Is there a function that returns the address (or just the column) of the currently selected cell? Simply put, something like =selectedcolumn.
Background: I want to display help text in a cell, and I want that help text to change according to which cell (actually, which column) the user has selected. I have put the help texts in a hidden row. They take up too much space to be displayed all the time. I want one cell to contain the help text of the column of the currently selected cell.
Trying to not use Filemaker for this project if possible
Thanks for any help /MattThe Numbers method, using comments as Barry showed, is probably the best method. An alternate method which is more complex would be to use an Applescript running in the background. The Applescript can scan in the background for which cell/column is currently selected and write that cell/column address into a cell in your table. A formula in your table (most likely a lookup formula) can use that address to serve up the correct help text in a diffrerent cell in the table or in another table.
Some problems with this methodare
The script is not part of the document. It is a separate entity.
The script must be started manually. It will not automatically start when the Numbers document is opened.
If the Numbers document is to be used on other Macs, each would also need the script installed. -
Best way to write an Wrapper class around a POJO
Hi guys,
What is the best way to write an Wrapper around a Hibernate POJO, given the latest 2.2 possibilities? The goal is, of course, to map 'regular' Java Bean properties to JavaFX 2 Properties, so that they can be used in GUI.
Thanks!what about this:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class PersonPropertyWrapper {
private StringProperty firstName;
private StringProperty lastName;
private Person _person;
public PersonPropertyWrapper(Person person) {
super();
this._person = person;
firstName = new SimpleStringProperty(_person.getFirstName()) {
@Override
protected void invalidated() {
_person.setFirstName(getValue());
lastName = new SimpleStringProperty(_person.getLastName()) {
@Override
protected void invalidated() {
_person.setLastName(getValue());
public StringProperty firstNameProperty() {
return firstName;
public StringProperty lastNameProperty() {
return lastName;
public static class Person {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
public String getLastName() {
return lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
public static void main(String[] args) {
Person p = new Person();
p.setFirstName("Jim");
p.setLastName("Green");
PersonPropertyWrapper wrapper = new PersonPropertyWrapper(p);
wrapper.firstNameProperty().setValue("Jerry");
System.out.println(p.getFirstName());
}Edited by: 906680 on 2012-7-27 上午10:56 -
Possible to write a function that takes any type as parameter ?
I need to write a function that will take 2 parameters ( of any type - VARCHAR etc ) and return a boolean.
( Im trying to write a function that will do NULL processing.
something like..
if ( P1=P2 OR ( P1 IS NULL AND P2 IS NULL ) ) then
return true ;
end if;
return false ;
the function simply compares its parameters P1 & P2 and returns true if they are equal - including if both are NULL
Is this possible to write at all.
My code looks very ugly without this being wrapped in a function.You may be able to use SYS.AnyData for this. However, dealing with the data would be confusing, so overloading the FUNCTION would most likely be a better idea.
This doesn't work, but it looked like fun, so i tried:
CREATE OR REPLACE FUNCTION Equal_Or_NULL(A SYS.AnyData, B SYS.AnyData)
RETURN PLS_INTEGER
AS
Local_A VARCHAR2(4000);
Local_B VARCHAR2(4000);
Junk PLS_INTEGER;
BEGIN
IF A IS NULL AND B IS NULL THEN RETURN 1; END IF;
CASE A.GetTypeName
WHEN 'SYS.NUMBER' THEN Junk := A.GetNumber(Local_A); Junk := B.GetNumber(Local_B);
WHEN 'SYS.DATE' THEN Junk := A.GetDate(Local_A); Junk := B.GetDate(Local_B);
WHEN 'SYS.CHAR' THEN Junk := A.GetCHAR(Local_A); Junk := B.GetCHAR(Local_B);
WHEN 'SYS.VARCHAR' THEN Junk := A.GetVARCHAR(Local_A); Junk := B.GetVARCHAR(Local_B);
WHEN 'SYS.VARCHAR2' THEN Junk := A.GetVARCHAR2(Local_A); Junk := B.GetVARCHAR2(Local_B);
END CASE;
RETURN CASE WHEN Local_A = Local_B THEN 1 ELSE 0 END;
END Equal_Or_NULL;
/Overloading would be much simpler, as the arguments could be directly compared.
IMO, Do not do this. Hiding logic in a FUNCTION it for cosmetic purposes is a bad idea. -
Best way to write SELECT statement
Hi,
I am selecting fields from one table, and need to use two fields on that table to look up additional fields in two other tables.
I do not want to use a VIEW to do this.
I need to keep all records in the original selection, yet I've been told that it's not good practice to use LEFT OUTER joins. What I really need to do is multiple LEFT OUTER joins.
What is the best way to write this? Please reply with actual code.
I could use 3 internal tables, where the second 2 use "FOR ALL ENTRIES" to obtain the additional data. But then how do I append the 2 internal tables back to the first? I've been told it's bad practice to use nested loops as well.
Thanks.Hi,
in your case having 2 internal table to update the one internal tables.
do the following steps:
*get the records from tables
sort: itab1 by key field, "Sorting by key is very important
itab2 by key field. "Same key which is used for where condition is used here
loop at itab1 into wa_tab1.
read itab2 into wa_tab2 " This sets the sy-tabix
with key key field = wa_tab1-key field
binary search.
if sy-subrc = 0. "Does not enter the inner loop
v_kna1_index = sy-tabix.
loop at itab2 into wa_tab2 from v_kna1_index. "Avoiding Where clause
if wa_tab2-keyfield <> wa_tab1-key field. "This checks whether to exit out of loop
exit.
endif.
****** Your Actual logic within inner loop ******
endloop. "itab2 Loop
endif.
endloop. " itab1 Loop
Refer the link also you can get idea about the Parallel Cursor - Loop Processing.
http://wiki.sdn.sap.com/wiki/display/Snippets/CopyofABAPCodeforParallelCursor-Loop+Processing
Regards,
Dhina.. -
Function that returns N values, without using a string
Hi, how can i make a function that returns several valures (that hasn't a exact number of returned values, it could return 3 values, or 7) without using a string?
When i need to return several values from a function, i put the values inside a varchar like thus 'XXX,YYY,ZZZ' and so on. Don't know if this has a poor performance.
If you can supply simple examples for what im asking, i would be nice.
(without using a string)Can i create the type objects inside a package? If i
can, they will be local to the package, right?Yes, you're right.
Pipeline returns a row or several?You can use pipelined function in the same way you use table:
SELECT * FROM TABLE(pipelined_funct(agr1, agr2));
It returns results as separate rows. -
Error with function that returns a rowtype...
i have a function that returns a rowtype. below is a simplified version:
create or replace function "GETC"
return mytable%rowtype
is
rec mytable%rowtype;
begin
rec.rowid := 1;
return rec;
end;
eventually i'll want to call this from java. but for now i'd settle for calling it from the XE web front end (and sqlplus).
SQLPLUS: i try to call it from sqlplus by first declaring a variable where i can store the return:
var a mytable%rowtype;
it instantly stops me saying that i can only declare a var of a particular type. so what do i do?
XE interface: i tried various ways to call getc(), but they were all unsuccessful. what should i be doing?
thanks so much for your time.
markoGiven a function like this:
CREATE OR REPLACE FUNCTION get_employee
( p_empno emp.empno%TYPE )
RETURN emp%ROWTYPE
AS
v_result emp%ROWTYPE;
BEGIN
SELECT * INTO v_result
FROM emp
WHERE empno = p_empno;
RETURN v_result;
END get_employee;You can call it within PL/SQL like this:
DECLARE
rec emp%ROWTYPE;
BEGIN
rec := get_employee(7902);
DBMS_OUTPUT.PUT_LINE(rec.ename);
END;However,
1. Unless there is a column named "ROWID" in the table (unlikely since it would conflict with the actual rowid and is therefore not allowed), the type will not have an attribute named "rowid".
2. "1" would not be a valid rowid anyway.
3. %ROWTYPE defines a PL/SQL type, which will not be recognised in other environments such as SQL and Java. -
Dear all,
Can someone say the best way writing below stored proc:
procedure missing_authorized_services is
v_truncate_sql varchar2(200);
v_sql varchar2(2000);
BEGIN
v_truncate_sql := 'truncate table missing_authorized_services';
execute immediate v_truncate_sql;
commit;
v_sql := 'INSERT into missing_authorized_services select distinct trim(service_group_Cd) as service_group_Cd, trim(service_cd) as service_cd from stage_1_mg_service_request
where (service_group_cd, service_cd) not in (
select distinct service_group_cd, service_cd from stage_3_servcd_servgrp_dim)';
execute immediate v_sql;
commit;
END missing_authorized_services;
/* I am doing select from table and then try to Insert into a different table the result set */
Please guide,
Thanks
JHi,
The best way to write PL/SQL (or any code) is in very small increments.
Start with a very simple procedure that does something (anything), just enough to test that it's working.
Add lots of ouput statments so you can see what the procedure is doing. Remember to remove them after testing is finished.
For example:
CREATE OR REPLACE procedure missing_authorized_services IS
v_truncate_sql VARCHAR2 (200);
BEGIN
v_truncate_sql := 'truncate table missing_authorized_services';
dbms_output.put_line ( v_truncate_sql
|| ' = v_truncate_sql inside missing_authorized_services'
END missing_authorized_services;If you get any errors (for example, ORA-00955, becuase you're trying to give the same name to a procedure that you're already using for a table), then fix the error and try again.
When it worls perfectly, then add another baby step. For example, you might add the one line
EXECUTE IMMEDIATE v_truncate_sql;and test again.
Don't use dynamic SQL (EXECUTE IMMEDIATE) unless you have to.
Is there any reason to use dynamic SQL for the INSERT? -
How to define a function that returns a void?
Hi all,
How can I define a custom function that returns a void?
My understanding is simple UDF can only return a string.
Is there any way around this limitation?
Thanks.
Ron> Hi,
> User Defined Function in XI always return a String.
>
> If you requirement is that you want to perfrom some
> operation in an user defined function, one option is
> to move it to the Java Section in your mapping and do
> it in the intialization / clean up section.
>
> Else, wite a UDF that will return a Blank string as
> the output, and map it to the root node of the
> target.
>
Hi all,
Thank you all for your kind responses.
The scenario I have is I need to insert the value of a particular field into a database table. E.g. MessageId, to keep track of the messages going through XI.
Naturally, such operations return void. These operations are already encapsulated in a custom jar file.
My purpose of using a UDF is solely to invoke the operation.
But I realized I each UDF has to have a return type, and the output of this UDF must be mapped to a node in the outgoing message.
Currently, my UDF returns an empty string, by using the implementation as below, I manage to perform my desired operation without affecting the result:
MessageId -- UDF -- CONCAT -
InstitutionCD_Transformed
InstitutionCode_____
But as you can see, this is not an elegant way of doing things.
That's why I'm seeking alternative solutions for this problem.
Bhavesh, you mentioned something about doing the operation in the initialization/cleanup section.
Can you please explain more?
Thanks.
Ron -
Which is the best way for a called function to identify caller class name.
Which is the best way for a called function to identify the caller class name .
1)Using sun.reflect.Reflection from called function
Class caller = Reflection.getCallerClass(2);
System.out.println("Caller Class Name ::"+caller.getName());2) Analyzing current threads stack trace from called function
StackTraceElement[] stElements=Thread.currentThread().getStackTrace();
System.out.println("Caller Class Name ::"+stElements[3].getClassName());Is there any alternate ways to achieve the same .Which is the best way ?
Called function doesn’t have any arguments, I don’t want t pass any arguments from caller function to called function.
Plz help.
With kind regards
Paul798185 wrote:
Which is the best way for a called function to identify the caller class name .
Is there any alternate ways to achieve the same.SecurityManager
// 0 is the anonymous SecurityManager class
// 1 is this class (also works in static context)
// 2 is calling class
static Class getClass(int i) {
return new SecurityManager() {
protected Class[] getClassContext() {
return super.getClassContext();
}.getClassContext(); -
Problem with a simple function that return a number
Hi,
I'm writing a function that return a number. I receive always an error (invalid number) but I don't understand why.
The function is this:
>
FUNCTION COUNT_OBJECT(workflow_name_p IN NUMBER, object_type_p IN VARCHAR2) RETURN NUMBER
IS
object_inserted NUMBER;
BEGIN
object_inserted := 0;
IF workflow_name_p = 'AIMDailyIngestorWorkflow'
THEN
object_inserted := 1;
else
object_inserted := 100;
END IF;
RETURN object_inserted;
END COUNT_OBJECT;
I have left only an if and an assignment because I do not find the error.
When I execute the function I receive always this error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
There are not operation, only assignment I cannot understand.
Could someone help me?
Thanks bye bye.I changed in this way:
FUNCTION COUNT_OBJECT(workflow_name_p IN VARCHAR2, object_type_p IN VARCHAR2) RETURN NUMBER
IS
object_present NUMBER;
object_inserted NUMBER;
table_name_p VARCHAR2(4000);
query_stmt VARCHAR2(4000);
BEGIN
IF workflow_name_p = 'AIMDailyIngestorWorkflow'
THEN
SELECT SUM(B.STOREDOBJS) INTO object_present
FROM DPCTJOBTYPESTATS B
WHERE B.WORKFLOW_NAME = workflow_name_p AND B.OBJTYPE=object_type_p;
SELECT 'AIM.'||B.TABLE_NAME INTO table_name_p
FROM DPCTSWOBJTYPE B
WHERE B.OBJTYPE=object_type_p AND SOFTWARE='AIM';
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || table_name_p || ';' INTO object_inserted;
object_inserted := object_inserted - object_present;
END IF;
RETURN object_inserted;
END COUNT_OBJECT;and now the error is:
ORA-00911: invalid character
ORA-06512: at "INFRA.WORKFLOW_STATISTICS", line 504
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
but the line 504 is the blank line higlighted with the ***.
Why this error? The code seems correct.
Thanks, bye bye. -
Best way to write this sql ?
Please let me know best way to write this SQL.
select col1, count(*)
from TableA
group by col1
having count(*) =
(select max(vals)
from
select col1, count(*) as vals
from TableA
group by col1
having count(*) > 1
)post EXPLAIN PLAN
SELECT col1,
COUNT(*)
FROM tablea
GROUP BY col1
HAVING COUNT(*) = (SELECT MAX(vals)
FROM (SELECT col1,
COUNT(*) AS vals
FROM tablea
GROUP BY col1
HAVING COUNT(*) > 1)) -
What is a best way to write SQL ?
Sample Case
drop table t;
drop table b;
create table t ( a varchar2(4), b number, c varchar2(1));
insert into t values ('A00', 10, 'R');
insert into t values ('A01', 11, 'R');
insert into t values ('A02', 12, 'R');
insert into t values ('A03', 13, 'R');
insert into t values ('A00', 10, 'P');
insert into t values ('A01', 11, 'P');
insert into t values ('A02', 12, 'P');
insert into t values ('A03', 13, 'P');
commit;
create table b ( j varchar(4), k varchar2(1), l varchar2(5), m number(3), n varchar2(5), o number(3));
insert into b values ('A00', 'P', 'FIXED', 100, 'FLOAT', 60);
insert into b values ('A01', 'P', 'FIXED', 101, 'FIXED', 30);
insert into b values ('A02', 'R', 'FLOAT', 45, 'FLOAT', 72);
insert into b values ('A03', 'R', 'FIXED', 55, 'FLOAT', 53);
commit;
10:19:13 SQL> select * from t;
A B C
A00 10 R
A01 11 R
A02 12 R
A03 13 R
A00 10 P
A01 11 P
A02 12 P
A03 13 P
8 rows selected.
10:19:19 SQL> select * from b;
J K L M N O
A00 P FIXED 100 FLOAT 60
A01 P FIXED 101 FIXED 30
A02 R FLOAT 45 FLOAT 72
A03 R FIXED 55 FLOAT 53
1/ In table t each reference having 2 records one with P another is with R
2/ In table b each refrence merged into single record and there are many records which are not existing in table t
3/ both t and j tables can be joined using a = j
4/ If from table t for a reference indicator is 'P' then if have to pick up l and m columns, if it is 'R' then I have to pick up n and o columns
5/ I want output in following format
A00 P FIXED 100
A00 R FLOAT 60
A01 P FIXED 101
A01 R FIXED 30
A02 P FLOAT 72
A02 R FLOAT 45
A03 P FLOAT 53
A03 R FIXED 55
6/ Above example is a sample ouput, In above example I have picked up only l,m,n,o columns, but in real example there are many columns ( around 40 ) to be selected. ( using "case when" may not be practical )
Kindly suggest me what is a best way to write SQL ?
thanks & regards
pjpIs this?
select b.j,t.c as k,decode(t.c,'P',l,n) as l,decode(t.c,'P',m,o) as m
from t,b
where t.a=b.j
order by j,k
J K L M
A00 P FIXED 100
A00 R FLOAT 60
A01 P FIXED 101
A01 R FIXED 30
A02 P FLOAT 45
A02 R FLOAT 72
A03 P FIXED 55
A03 R FLOAT 53
8 rows selected.
or is this?
select b.j,t.c as k,decode(t.c,b.k,l,n) as l,decode(t.c,b.k,m,o) as m
from t,b
where t.a=b.j
order by j,k
J K L M
A00 P FIXED 100
A00 R FLOAT 60
A01 P FIXED 101
A01 R FIXED 30
A02 P FLOAT 72
A02 R FLOAT 45
A03 P FLOAT 53
A03 R FIXED 55
8 rows selected. -
I get an error when i try to execute a function that returns a date value
Hi,
I'm new in ODP.NET, i'm make a package that contains a function that returns a date i.e.
Package Body General_pkg is
Function Get_Day Return Date is
vd_day date;
Begin
select sysdate into vd_day from dual;
return vd_day;
end Get_Day;
End General_pkg;
i use the next code to execute the function:
OracleCommand cmdData = new OracleCommand("General_pkg.Get_Day", cnx);
cmdData.CommandType = Commandtype.StoredProcedure;
OracleParameter PRM;
PRM = new OracleParameter();
PRM.ParameterName = "VDATE";
PRM.OracleDbType = OracleDbType.Date;
PRM.Direction = ParameterDirection.ReturnValue;
cmdData.Parameters.Add(PRM);
try
cmdData.ExecuteNonQuery();
catch(OracleException e);
When i execute this code, i have and exception, which say "identifier GENERAL_PKG.Get_Day must be declare..."
Obviously the package is correctly created in ORACLE i tested first in sql plus and it works but in ODP.NET they don't recognize the package or the function i don't know... please help!!!OOOOOPSS.... i forgot that i change the user in my conecction string that's why it didn't work...
i guess i had to much beer last night.;. :)
thanks.
Maybe you are looking for
-
Sample Code for CRM enhancement in BADI
hi, can anybody please give me sample code for BADI for CRM enhancement. i have added couple of z field in a extract structure. now i have to write code in BADI to populate those fields. please do not send code for user exit. Regards Subrata
-
ITunes no longer syncs to iPhone or iPad after iOS security update
I did the iOS security update on my iPhone 5 and my iPad Air. Now, iTunes will not sync with either device - neither one. In both cases, I get the error message "iPhone5 (or iPad) can not be synced. An unknown error occured (1140)." I then did the
-
Process Flow - Executing a shell Script
I have a simple shell script being called at the start of a process flow, up until a few days ago this worked fine across DEV, TEST and PROD. It's now stopped working in PROD. It executes through the command line fine as an os user and when you inspe
-
How can I make a 3 fold leaflet in photoshop?
Hi I am very new to photoshop but would like to create a professional looking A4 3 fold leaflet (takeaway-leaflet-type). Any idea's how I can create such thing, or any other application I can use? Thanks in advance
-
[NIO] custom messages from the netwerk
Hi you all, Yes this is another NIO question :) Ok I have the following situation, I've got a NIO server which reads custom messages from the netwerk. These messages consists of an interger and then a variable byte array. int -> length of the message