Collection of the dynamic query
Hi All-
I'm trying to get the value of the collection through the dynamic query but I am facing some problem please let me know that I hurt.
Created a function like below to run the dynamic query to select statement
create or replace FUNCTION rfunGetColumnValue ( ColumnName VARCHAR2, TableName VARCHAR2, DefaultValue OUT VARCHAR2, Criteria VARCHAR2) RETURN VARCHAR2 IS ReturnValue VARCHAR2 (32767 byte) ; Stmt VARCHAR2 (32767 byte) ; BEGIN stmt := 'begin select '|| ColumnName || ' into :1 from table(:2) ' ||NVL ( Criteria, ' ') || ' Fetch First Row only ; EXCEPTION WHEN OTHERS THEN :2 := SQLERRM; end;' ; dbms_output.put_line (stmt) ; EXECUTE IMMEDIATE stmt USING OUT ReturnValue, OUT DefaultValue; --select Valueinto into Returnvalue from dual; RETURN ReturnValue; END;
Now, I created a folder in the Package
create or replace PACKAGE Collection_PKG is type Bank_rec is RECORD ( SNO NUMBER(10), BANKID NUMBER(5), BANKNAME VARCHAR2(50), BANKSC VARCHAR2(50), ADDEDIT varchar2(1), COMPID number(5), ISBULK number(1), ROWNO number(10), ERROR VARCHAR2(500) ); TYPE Bank_tbl IS TABLE OF Bank_rec; --type Bank_cur is ref cursor return Bank_rec; end Collection_PKG; end Collection_PKG; end Collec tion_PKG;
Now, when I'm Trying the code below
DECLARE V_EXECQUERYPARAM XMLTYPE:= XMLTYPE('<QueryParam> <BankXML> <Bank> <BankID>0</BankID> <BankSC><![CDATA[RCB]]></BankSC> <BankName><![CDATA[Royal challenger Bank]]></BankName> <IsBulk>0</IsBulk> <AddEdit>A</AddEdit> </Bank> </BankXML> </QueryParam>'); BEGIN Rspbanksave( v_SPParamList => V_EXECQUERYPARAM ); END;
create or replace PROCEDURE rspBankSave ( v_SPParamList XMLTYPE DEFAULT NULL) IS V_Addedit VARCHAR2 (1 CHAR) ; Bank_tbl Collection_PKG.BANK_TBL := Collection_PKG.BANK_TBL () ; BEGIN SELECT Row_number () OVER (ORDER BY 1), XT.BankID, XT.BankName, XT.BankSC, XT.AddEdit, v_CompID, XT.IsBulk, CAST (0 AS NUMBER (5)), CASE WHEN MBank.BankID IS NOT NULL THEN CASE WHEN XT.BankSC = MBank.BankSC AND XT.BankName = MBank.BankName THEN 'R104|Entry Already Exist,R114|Short Code Already Exist' WHEN XT.BankSC = MBank.BankSC THEN 'R114|Short Code Already Exist' WHEN XT.BankName = MBank.BankName THEN 'R104|Entry Already Exist' END ELSE NULL END Bulk collect INTO Bank_tbl FROM XMLTABLE ('//QueryParam/BankXML/Bank' PASSING v_SPParamList COLUMNS BankID NUMBER (5) PATH 'BankID', BankName VARCHAR2 ( 50) PATH 'BankName', BankSC VARCHAR2 ( 50) PATH 'BankSC', AddEdit VARCHAR2 ( 1) PATH 'AddEdit', IsBulk NUMBER (1) PATH 'IsBulk') XT LEFT JOIN MBank ON XT.BankID != MBank.BankID AND v_CompID = MBank.CompID AND (XT.BankSC = MBank.BankSC OR XT.BankName = MBank.BankName) ; v_TotalRowCount := SQL%ROWCOUNT; IF (v_TotalRowCount > 0) THEN BEGIN V_Addedit:=rfunGetColumnValue (ColumnName=> 'Upper(AddEdit)',TableName=>'table(Bank_tbl)',DefaultValue=>'',Criteria =>''); END; --SQL Code here END IF; END rspBankSave;
As I've suggested before:
To get the name of the table the column
and as others have:
Type global temporary Tables vs. table
If you give more context, more information about the bigger picture, more information about what you're trying to do and why you're going down the road you go down, you can return more useful information. But you seem reluctant to do.
For the moment, your recent posts seem to just raise the same type of question - why are you doing this?
The normal way to return data to a client is a refcursor.
It is the most effective way.
Not through collections that you seem to be put on the must-do approach.
Tags: Database
Similar Questions
-
I wonder why the dynamic value query is executed before the boot process? Logically, it makes sense to run after them.
For example, I test a stored procedure that is supposed to delete a record, and I'd like to create this test report should be deleted as part of the startup process before execution of the stored procedure call to delete this test record. Apparently the dynamic query of value not returns not the test report in as long as the query parameter to call the stored procedure under test, which makes me think that is executed before the startup process of design...
Please advise...
Thank you
Val
As this thread does no traction/attention of the team of SQL Developer for a while, I had to submit a request for formal improvement on metalink:
RE: 19834977 - IN THE UNIT TEST REQUEST TO ALLOW TO CHANGE THE ORDER OF EXECUTION OF THE START OF THE PROCESS
Thank you
Val
-
Get the dynamic query alias name
Hi all
I have a plsql function using a dynamic query.
And the function takes an entire sql query as a parameter.
The main problem is that the function must get what aliases or columns were interviewed.
For example,.
FUNCTION_GET_QUERY_ALIAS ('SELECT 1 AS col1, col2 FROM DUAL 2 AS')
Inside the function, he must find the alias name COL1 and COL2.
I would be grateful for any help.I modified print_table as fact and function to meet your needs.
SQL> CREATE OR REPLACE TYPE my_column_object AS OBJECT(ruw_number integer, column_name VARCHAR2(1000), column_val VARCHAR2(1000)) 2 / Type created. SQL> CREATE OR REPLACE TYPE my_table_type AS TABLE OF my_column_object 2 / Type created. SQL> CREATE OR REPLACE FUNCTION print_table( p_query in varchar2 ) RETURN my_table_type PIPELINED 2 AS 3 l_theCursor INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR; 4 l_columnValue VARCHAR2(4000); 5 l_status INTEGER; 6 l_descTbl DBMS_SQL.DESC_TAB; 7 l_colCnt NUMBER; 8 l_rcount INTEGER := 0; 9 BEGIN 10 DBMS_SQL.PARSE( l_theCursor, p_query, dbms_sql.native ); 11 12 DBMS_SQL.DESCRIBE_COLUMNS( l_theCursor, l_colCnt, l_descTbl ); 13 14 FOR i IN 1 .. l_colCnt 15 LOOP 16 DBMS_SQL.DEFINE_COLUMN(l_theCursor, i, l_columnValue, 4000); 17 end loop; 18 19 l_status := DBMS_SQL.EXECUTE(l_theCursor); 20 21 WHILE ( DBMS_SQL.FETCH_ROWS(l_theCursor) > 0 ) 22 LOOP 23 l_rcount := l_rcount + 1; 24 FOR i IN 1 .. l_colCnt 25 LOOP 26 DBMS_SQL.COLUMN_VALUE( l_theCursor, i, l_columnValue ); 27 28 PIPE ROW(my_column_object(l_rcount,l_descTbl(i).col_name,l_columnValue)); 29 END LOOP; 30 END LOOP; 31 32 RETURN; 33 end; 34 / Function created. SQL> select * from table(print_table('select * from emp')) 2 / RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 1 EMPNO 7369 1 ENAME SMITH 1 JOB CLERK 1 MGR 7902 1 HIREDATE 17-DEC-80 1 SAL 800 1 COMM 1 DEPTNO 20 1 DIV 10 2 EMPNO 7499 2 ENAME ALLEN RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 2 JOB SALESMAN 2 MGR 7698 2 HIREDATE 20-FEB-81 2 SAL 1600 2 COMM 300 2 DEPTNO 30 2 DIV 10 3 EMPNO 7521 3 ENAME WARD 3 JOB SALESMAN 3 MGR 7698 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 3 HIREDATE 22-FEB-81 3 SAL 1250 3 COMM 500 3 DEPTNO 30 3 DIV 10 4 EMPNO 7566 4 ENAME JONES 4 JOB MANAGER 4 MGR 7839 4 HIREDATE 02-APR-81 4 SAL 2975 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 4 COMM 4 DEPTNO 20 4 DIV 10 5 EMPNO 7654 5 ENAME MARTIN 5 JOB SALESMAN 5 MGR 7698 5 HIREDATE 28-SEP-81 5 SAL 1250 5 COMM 1400 5 DEPTNO 30 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 5 DIV 10 6 EMPNO 7698 6 ENAME BLAKE 6 JOB MANAGER 6 MGR 7839 6 HIREDATE 01-MAY-81 6 SAL 2850 6 COMM 6 DEPTNO 30 6 DIV 10 7 EMPNO 7782 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 7 ENAME CLARK 7 JOB MANAGER 7 MGR 7839 7 HIREDATE 09-JUN-81 7 SAL 2450 7 COMM 7 DEPTNO 10 7 DIV 10 8 EMPNO 7788 8 ENAME SCOTT 8 JOB ANALYST RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 8 MGR 7566 8 HIREDATE 19-APR-87 8 SAL 3000 8 COMM 8 DEPTNO 20 8 DIV 10 9 EMPNO 7839 9 ENAME KING 9 JOB PRESIDENT 9 MGR 9 HIREDATE 17-NOV-81 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 9 SAL 5000 9 COMM 9 DEPTNO 10 9 DIV 10 10 EMPNO 7844 10 ENAME TURNER 10 JOB SALESMAN 10 MGR 7698 10 HIREDATE 08-SEP-81 10 SAL 1500 10 COMM 0 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 10 DEPTNO 30 10 DIV 10 11 EMPNO 7876 11 ENAME ADAMS 11 JOB CLERK 11 MGR 7788 11 HIREDATE 23-MAY-87 11 SAL 1100 11 COMM 11 DEPTNO 20 11 DIV 10 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 12 EMPNO 7900 12 ENAME JAMES 12 JOB CLERK 12 MGR 7698 12 HIREDATE 03-DEC-81 12 SAL 950 12 COMM 12 DEPTNO 30 12 DIV 10 13 EMPNO 7902 13 ENAME FORD RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 13 JOB ANALYST 13 MGR 7566 13 HIREDATE 03-DEC-81 13 SAL 3000 13 COMM 13 DEPTNO 20 13 DIV 10 14 EMPNO 7934 14 ENAME MILLER 14 JOB CLERK 14 MGR 7782 RUW_NUMBER COLUMN_NAME COLUMN_VAL ---------- -------------------- -------------------- 14 HIREDATE 23-JAN-82 14 SAL 1300 14 COMM 14 DEPTNO 10 14 DIV 10 126 rows selected. SQL>
Thank you
Knani.Published by: Karthick_Arp on September 23, 2008 12:11 AM
-
Cannot generate the dynamic query - ora-06502
Hi friends,
during execution of code below I get the error message: ora-06502 pl/sql numeric or value error on line 11
I'm not able to open a SQL session table logging.
DECLARE
T_Participants TYPE TABLE IS NUMBER;
c_Participant_Id t_Participants: = t_Participants();
CLOB V_SQL;
BEGIN
Select the participantid COLLECT in BULK IN c_Participant_Id in t_roster_detail
where rosterid = 10654
and ba = "MD";
I'm IN 1.c_Participant_Id.COUNT LOOP
V_SQL: = V_SQL | "SELECT p.participantid,.
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10331, p.Current_Data, NULL), null) "10331_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10331, p.Goal_Data, NULL), null) "10331_Goal."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 9640, p.Current_Data, NULL), null) "9640_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 9640, p.Goal_Data, NULL), null) "9640_Goal."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 9643, p.Current_Data, NULL), null) "9643_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 9643, p.Goal_Data, NULL), null) "9643_Goal."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10332, p.Current_Data, NULL), null) "10332_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10332, p.Goal_Data, NULL), null) "10332_Goal."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10721, p.Current_Data, NULL), null) "10721_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10721, p.Goal_Data, NULL), null) "10721_Goal."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10701, p.Current_Data, NULL), null) "10701_CURRENT."
Decode (p.Participantid, ' | c_Participant_Id (i) |', decode (p.Measureid, 10701, p.Goal_Data, NULL), null) '10701_Goal '.
OF t_sce_msr_output_data p
WHERE IN (SELECT T_PC_AXIS_DEFINITION CREATES CREATES
WHERE PLANCOMPONENTID IN (SELECT PLANCOMPONENTID FROM T_PLAN_COMPONENT WHERE PLANID = 10702))
UNION ";
END LOOP;
DELETE FROM T_LOGGING_SQL;
INSERT INTO T_LOGGING_SQL VALUES (V_SQL);
COMMIT;
END;
SQL > desc t_sce_msr_output_data
Name Null? Type
----------------------------------------- -------- ------------
IDSCENARIO NOT NULL NUMBER
CREATES NOT NULL NUMBER
PARTICIPANTID NOT NULL NUMBER
NUMBER OF BASELINE_DATA
NUMBER OF CURRENT_DATA
NUMBER OF GOAL_DATA
You are way more complicate things here. First of all, there is no need at all for dynamic sql because the wy you are querying is incorect.
Even if you do not have an error, which you query will do is run the query on t_sce_msr_output_data once for each row returned by the cursor. Each iteration of the query will return all matching rows (i.e. lines for all the participantid in the t_sce_msr_output_data table), then the external decoding will be will force all other NULL columns for all the participantid that do not correspond to the "current" participantid
The query can be simplified to:
Select p.participantid,
Decode (p.Measureid, 10331, p.Current_Data, NULL) "10331_CURRENT."
Decode (p.Measureid, 10331, p.Goal_Data, NULL) "10331_Goal."
Decode (p.Measureid, 9640, p.Current_Data, NULL) "9640_CURRENT."
Decode (p.Measureid, 9640, p.Goal_Data, NULL) "9640_Goal."
Decode (p.Measureid, 9643, p.Current_Data, NULL) "9643_CURRENT."
Decode (p.Measureid, 9643, p.Goal_Data, NULL) "9643_Goal."
Decode (p.Measureid, 10332, p.Current_Data, NULL) "10332_CURRENT."
Decode (p.Measureid, 10332, p.Goal_Data, NULL) "10332_Goal."
Decode (p.Measureid, 10721, p.Current_Data, NULL) "10721_CURRENT."
Decode (p.Measureid, 10721, p.Goal_Data, NULL) "10721_Goal."
Decode (p.Measureid, 10701, p.Current_Data, NULL) "10701_CURRENT."
Decode (p.Measureid, 10701, p.Goal_Data, NULL) '10701_Goal '.
of t_sce_msr_output_data p
where in (select creates creates
of t_pc_axis_definition
where plancomponentid in (select plancomponentid
of t_plan_component
where planid = 10702)) and
participantid in (select participantid
of t_roster_detail
where rosterid = 10654 and
BA = "MD");
You probably want to pivot these results by participant, if so look in the note from the FAQ for the columns of the rows.
John
-
Hey everyone, have a quick question. I just started using ColdFusion week last to my current job and I do a database work. One of the scripts I'm working on that is dynamically allow a user to enter information for a source of data, their username, password, database, then an operation of query-by-example-esc on this database. However a question that I am currently running shows these data. So say I got into a simple HTML form and it is passed in as the name "Colonne_1". So when I go to access my SQL statement, I'd be using:
#Form.Column_1 #.
My problem comes when I am printing these data. Normally I use the following code:
< cfoutput query = "GetResults" >
#username #.
< / cfoutput >Where # is the column #username I was looking for. How would I be able to have dynamically it out everything that needs to be entered as column name in colonne_1? So, something like:
< cfoutput query = "GetResults" >
# #Form.Column_1 # #.
< / cfoutput >Thank you!
table notation is your friend.
QueryName ["columname'] [line number]
or, in your case,
QueryName [' #variable for columname # "] [row number]
-
Access the results of the dynamic query database...
CF colleagues:
I need to execute a loop of 4 times, to access the four distinct values a data offline in a test results table (tbl_results), where all four are in line with the student (WHERE studentid = etc.).
It looks like this...
< cfset variables. TimeTotals = 0 >
< cfloop from '1' to step "4" = = = '1' index = "timrec" >
< cfquery datasource = "MyDSN" name = "rs_time" >
SELECT Btime #timrec #.
OF tbl_results
WHERE studentid = 'jack '.
< / cfquery >
< cfset variables. TimeTotals = variable. TimeTotals + (? rs_time.) BTime #timrec # value?) >
< / cfloop >
QUESTION: How should I address this #timrec # value Btime? Not that way, I know, but how?
Looking forward to your wisdom, with gratitude,
Hans
There are two ways to attack this, but you're close enough. One way is to simply include a column alias in the query, and then use it to make reference to results:
SELECT Btime #timrec # LIKE feeling
OF tbl_results
WHERE studentid = 'jack '.
-
How to set the result of the dynamic query in a refcursor ON param.
Hello
I created the following package.
CREATE OR REPLACE package LGIS
Is
Type T_Cust_Acct_Arr is table of number;
Type Invc_Base_Rec_Type is (Record
Invoice_Control_No Customer_History.Invoice_Control_No%Type,
Billing_Cycle_Id Eb_Oor_Cust_Online.Billing_Cycle_Id%Type,
Bill_Cycl_Occr_No Eb_Oor_Cust_Online.Bill_Cycl_Occr_No%Type,
Invoice_Dt Bill_Cycl_Occr_Inf.Invoice_Date%Type,
Invc_Acct_Id Eb_Oor_Cust_Online.Customer_Acct_Id%Type,
Acct_Id Eb_Oor_Cust_Online.Customer_Acct_Id%Type
);
Type Invc_Base_Refcur_Type is Ref Cursor
Return Invc_Base_Rec_Type;
Procedure P_Get_Approved_Whole_Invcs)
In_From_Invc_Dt by Date,
In_To_Invc_Dt by Date,
In_Cust_Acct_List in T_Cust_Acct_Arr,
Out_Eb_Invcs on Invc_Base_Refcur_Type
On_Error_No number
);
End LGIS;
/
CREATE OR REPLACE PACKAGE BODY Lgis
IS
PROCEDURE P_Get_Approved_Whole_Invcs)
In_From_Invc_Dt BY DATE,
In_To_Invc_Dt BY DATE,
In_Cust_Acct_List IN T_Cust_Acct_Arr,
Out_Eb_Invcs on Invc_Base_Refcur_Type
On_Error_No NUMBER
)
IS
S_Query VARCHAR2 (4000);
Invalid_Exception EXCEPTION;
BEGIN
On_Error_No: = 0;
IF In_Cust_Acct_List.COUNT > 0 THEN
FORALL i IN 1.In_Cust_Acct_List.COUNT
INSERT INTO EB_CUST_ACCT_WHOLE_TMP (Cust_Acct_Val)
VALUES (In_Cust_Acct_List (i));
END IF;
BEGIN
S_Query: = 'Select Ch.Invoice_Control_No,';
S_Query: = S_Query | ' Eoco.Billing_Cycle_Id,';
S_Query: = S_Query | ' Eoco.Bill_Cycl_Occr_No,';
S_Query: = S_Query | ' Bcoi.Invoice_Date,';
S_Query: = S_Query | ' Eoco.Customer_Acct_Id as Invc_Acct_Id';
S_Query: = S_Query | ' Eoco.Customer_Acct_Id as Acct_Id ';
S_Query: = S_Query | "Of Eb_Oor_Cust_Online Eoco,';
S_Query: = S_Query | "Customer_History Ch';
S_Query: = S_Query | ' Bcoi Bill_Cycl_Occr_Inf,';
S_Query: = S_Query | "Audit_Var_Log Avl,';
S_Query: = S_Query | "Audit_Var_Def Avd,';
S_Query: = S_Query | "Eb_Cust_Acct_Whole_Tmp Tmp';
S_Query: = S_Query | "Where Avd.Audit_Var_Cd ="CYCLE_APPROVAL"';
S_Query: = S_Query | ' And Avl.Audit_Var_Sub_Grp_Val = 0';
S_Query: = S_Query | "And Avl.Audit_Var_Char_Val =" COMPLETE "';
S_Query: = S_Query | ' And Eoco.Customer_Acct_Id = Tmp.Cust_Acct_Val';
S_Query: = S_Query | ' And Eoco.Customer_Acct_Id = Ch.Customer_Acct_Id';
S_Query: = S_Query | ' And Eoco.Billing_Cycle_Id = Ch.Billing_Cycle_Id';
S_Query: = S_Query | ' And Eoco.Bill_Cycl_Occr_No = Ch.Bill_Cycl_Occr_No';
S_Query: = S_Query | ' And Eoco.Billing_Cycle_Id = Bcoi.Billing_Cycle_Id';
S_Query: = S_Query | ' And Eoco.Bill_Cycl_Occr_No = Bcoi.Bill_Cycl_Occr_No';
S_Query: = S_Query | ' And Avd.Audit_Var_Id = Avl.Audit_Var_Id';
S_Query: = S_Query | ' And Avl.Audit_Var_Grp_Id = Bcoi.Audit_Var_Grp_Id';
IF (In_From_Invc_Dt IS NULL AND In_To_Invc_Dt IS NOT NULL) THEN
S_Query: = S_Query | "And Bcoi.Invoice_Date < = In_To_Invc_Dt';
END IF;
IF (In_To_Invc_Dt IS NULL AND In_From_Invc_Dt IS NOT NULL) THEN
S_Query: = S_Query | ' And Bcoi.Invoice_Date > = In_From_Invc_Dt';
END IF;
IF (In_From_Invc_Dt IS NOT NULL AND In_To_Invc_Dt IS NOT NULL) THEN
S_Query: = S_Query | ' And Bcoi.Invoice_Date > = In_From_Invc_Dt';
S_Query: = S_Query | "And Bcoi.Invoice_Date < = In_To_Invc_Dt';
END IF;
RUN IMMEDIATELY S_Query IN Out_Eb_Invcs;
EXCEPTION
WHILE OTHERS THEN
On_Error_No: = - 1;
RAISE Invalid_Exception;
END;
Exception
When Invalid_Exception then
On_Error_No: = - 1;
Return;
While others then
On_Error_No: = - 1;
END P_Get_Approved_Whole_Invcs;
Lgis END;
/
It compiles without error. But at runtime, I get the following error:
ORA-06504: PL/SQL: return variables of the game results or the query types do not match
Please help solve this problem.
Thank you
OlivierMaybe it will work for you too->
CREATE OR REPLACE Package Body Lgis Is Procedure P_Get_Approved_Whole_Invcs ( In_From_Invc_Dt In Date, In_To_Invc_Dt In Date, In_Cust_Acct_List In T_Cust_Acct_Arr, Out_Eb_Invcs Out Invc_Base_Refcur_Type, On_Error_No Out Number ) Is S_Query Varchar2(4000); Invalid_Exception Exception; Begin On_Error_No := 0; If In_Cust_Acct_List.Count > 0 Then Forall I In 1..In_Cust_Acct_List.Count Insert Into Eb_Cust_Acct_Whole_Tmp (Cust_Acct_Val) Values (In_Cust_Acct_List (I)); End If; Begin S_Query := ' Select Ch.Invoice_Control_No Invoice_Control_No, ' ||' Eoco.Billing_Cycle_Id Billing_Cycle_Id, ' ||' Eoco.Bill_Cycl_Occr_No Bill_Cycl_Occr_No, ' ||' Bcoi.Invoice_Date Invoice_Dt, ' ||' Eoco.Customer_Acct_Id As Invc_Acct_Id,' ||' Eoco.Customer_Acct_Id As Acct_Id ' ||' From Eb_Oor_Cust_Online Eoco, ' ||' Customer_History Ch, ' ||' Bill_Cycl_Occr_Inf Bcoi, ' ||' Audit_Var_Log Avl, ' ||' Audit_Var_Def Avd, ' ||' Eb_Cust_Acct_Whole_Tmp Tmp ' ||' Where Avd.Audit_Var_Cd = '||chr(39)||'CYCLE_APPROVAL'||chr(39) ||' And Avl.Audit_Var_Sub_Grp_Val = 0 ' ||' And Avl.Audit_Var_Char_Val = '||chr(39)||'COMPLETE'||chr(39) ||' And Eoco.Customer_Acct_Id = Tmp.Cust_Acct_Val ' ||' And Eoco.Customer_Acct_Id = Ch.Customer_Acct_Id ' ||' And Eoco.Billing_Cycle_Id = Ch.Billing_Cycle_Id ' ||' And Eoco.Bill_Cycl_Occr_No = Ch.Bill_Cycl_Occr_No ' ||' And Eoco.Billing_Cycle_Id = Bcoi.Billing_Cycle_Id ' ||' And Eoco.Bill_Cycl_Occr_No = Bcoi.Bill_Cycl_Occr_No ' ||' And Avd.Audit_Var_Id = Avl.Audit_Var_Id ' ||' And Avl.Audit_Var_Grp_Id = Bcoi.Audit_Var_Grp_Id ' If (In_From_Invc_Dt Is Null And In_To_Invc_Dt Is Not Null) Then S_Query:=S_Query||' And Bcoi.Invoice_Date <= In_To_Invc_Dt '; End If; If (In_To_Invc_Dt Is Null And In_From_Invc_Dt Is Not Null) Then S_Query:=S_Query||' And Bcoi.Invoice_Date >= In_From_Invc_Dt '; End If; If (In_From_Invc_Dt Is Not Null And In_To_Invc_Dt Is Not Null) Then S_Query:=S_Query||' And Bcoi.Invoice_Date >= In_From_Invc_Dt ' ||' And Bcoi.Invoice_Date <= In_To_Invc_Dt '; End If; Dbms_Output.Put_Line ('Test'); Open Out_Eb_Invcs For S_Query; Dbms_Output.Put_Line ('Test1'); Loop Fetch Out_Eb_Invcs Into Ibrt_Type; Exit When Out_Eb_Invcs%notfound; Dbms_Output.Put_Line ('Invoice_Control_No: ' || Ibrt_Type.Invoice_Control_No); Dbms_Output.Put_Line ('Billing_Cycle_Id: ' || Ibrt_Type.Billing_Cycle_Id); Dbms_Output.Put_Line ('Bill_Cycl_Occr_No: ' || Ibrt_Type.Bill_Cycl_Occr_No); Dbms_Output.Put_Line ('Invoice_Dt: ' || Ibrt_Type.Invoice_Dt); Dbms_Output.Put_Line ('Invc_Acct_Id: ' || Ibrt_Type.Invc_Acct_Id); Dbms_Output.Put_Line ('Acct_Id: ' || Ibrt_Type.Acct_Id); End Loop; Dbms_Output.Put_Line ('Test2'); Close Out_Eb_Invcs; /* Exception When Others Then On_Error_No := -1; Raise Invalid_Exception; */ End; Exception When Invalid_Exception Then On_Error_No := -1; Return; /* When Others Then On_Error_No := -1; */ End P_Get_Approved_Whole_Invcs; End Lgis;
Your problem is now fixed.
Kind regards.
LOULOU.
-
How to create the table dynamically in the procedure, I tried below to how his error getting.
SQL > create or replace procedure tablecreation (tablename varchar2, varchar2 col1, col2 varchar2) is
stmt 2 varchar2 (2000);
3. start
stmt 4: = ' create table: 1 (: 2 varchar2 (20): varchar2 (20)) 3';
5. perform immediate stmt using tablename, col1, col2;
6 end;
7.
Created procedure.
SQL > exec tablecreation ('test', 'col1', 'col2');
BEGIN tablecreation ('test', 'col1', 'col2'); END;
*
ERROR on line 1:
ORA-00903: invalid table name
ORA-06512: at "SCOTT. TABLECREATION', line 5
ORA-06512: at line 1This means that the privilege is assigned to you by using a role. You must have the privilege assigned directly to the user in order to to use a procedure.
-
30EA2 - UNIT TESTING - value dynamic query truncating dates time.
So I use the installation of SQL Developer Unit Testing (very nice by the by) for a bit now and just recently upgraded to the latest beta version (3.0.02.83). Looks like a bug has been introduced by which the dynamic query of value are truncate all time of all DATE data type information.
This causes sadness serious for my current test cases and for me since I have to go back to the previous beta version :)I noticed that, in the new 30EA2 parameter:
AddVMOption - Doracle.jdbc.mapDateToTimestamp = false
has been introduced by default in
[SQLDEveloper_install_dir]/sqldeveloper/bin/sqldeveloper.conf
Perhaps it has something to do with the behavior of incorrect date, try to remove the parameter and check again.
-
Hello
I have a table1 as below
ID (NUMBER), keyword (VARCHAR2)
----------------------------------------------------
1, HAS
2, B
3, C
Another table2
Name (VARCHAR2)
--------------------------
Alice
Bob
Jack
I need to write a stored procedure to check if the 2 mactched of the char keyword table1 table name. How to write the dynamic query in my PL/SQL in a stored procedure so that I can generate query similar to
SELECT * from table2 where name like '%x %' where I need to replace X by the tank in table1
Help, please.
Thanks in advance,
MarryI need to write a stored procedure to check if the 2 mactched of the char keyword table1 table name
You could simply join the tables:
SQL> with table1 as ( select 1 id, 'A' keyword from dual union all select 2 id, 'B' keyword from dual union all select 3 id, 'C' keyword from dual ), table2 as ( select 'Alice' name from dual union all select 'Bob' name from dual union all select 'Jack' name from dual ) -- -- select * from table1, table2 where name like '%' || keyword || '%' ID KEYWORD NAME ---------- ------- ----- 1 A Alice 2 B Bob 2 rows selected.
-
Rewrite the static query in dynamic SQL
Can someone please help me write a dynamic query in pl/sql to SQL next?
It's the game ('25', ' 04 "). the number of list might be n.
The column_type varies from(1..6) and
the from(1..6) of column_amt1 lines
Thanks in advanceSELECT t.claim_no, t.cert_no, t.rec_code, (NVL(decode(t.column_type1,'25',NVL (t.column_amt1, 0)),0)+ NVL(decode(t.column_type1,'04',NVL(t.column_amt1, 0)),0 ) ) + (NVL(decode(t.column_type2,'25',NVL (t.column_amt2, 0)),0)+ NVL(decode(t.column_type2,'04',NVL(t.column_amt2, 0)),0 ) ) + (NVL(decode(t.column_type3,'25',NVL (t.column_amt3, 0)),0)+ NVL(decode(t.column_type3,'04',NVL(t.column_amt3, 0)),0 ) ) + (NVL(decode(t.column_type4,'25',NVL (t.column_amt4, 0)),0)+ NVL(decode(t.column_type4,'04',NVL(t.column_amt4, 0)),0 ) ) + (NVL(decode(t.column_type5,'25',NVL (t.column_amt5, 0)),0)+ NVL(decode(t.column_type5,'04',NVL(t.column_amt5, 0)),0 ) ) + (NVL(decode(t.column_type6,'25',NVL (t.column_amt6, 0)),0)+ NVL(decode(t.column_type6,'04',NVL(t.column_amt6, 0)),0 )) amt from test_detail t;
HenaBut in fact, you should not dynamic SQL at all. What you could do is create a SQL type and use MEMBER OF:
SQL> CREATE OR REPLACE 2 TYPE test_detail_type_tbl 3 AS 4 TABLE OF varchar2(2) 5 / Type created. SQL> select case_id, 2 item, 3 code, 4 case 5 when type1 member of test_detail_type_tbl('25','04') then nvl(amt1,0) 6 else 0 7 end + 8 case 9 when type2 member of test_detail_type_tbl('25','04') then nvl(amt2,0) 10 else 0 11 end + 12 case 13 when type3 member of test_detail_type_tbl('25','04') then nvl(amt3,0) 14 else 0 15 end + 16 case 17 when type4 member of test_detail_type_tbl('25','04') then nvl(amt4,0) 18 else 0 19 end + 20 case 21 when type5 member of test_detail_type_tbl('25','04') then nvl(amt5,0) 22 else 0 23 end + 24 case 25 when type6 member of test_detail_type_tbl('25','04') then nvl(amt6,0) 26 else 0 27 end copay 28 from test_detail 29 / CASE_ID ITEM CO COPAY ---------- --------- -- ---------- EML3371015 133761570 10 355 EML3371015 133761570 10 20 EML3371015 133761570 10 5 EMC6369600 140328551 10 54 EMH6353995 140328551 11 26.04 SQL> select case_id, 2 item, 3 code, 4 case 5 when type1 member of test_detail_type_tbl('25') then nvl(amt1,0) 6 else 0 7 end + 8 case 9 when type2 member of test_detail_type_tbl('25') then nvl(amt2,0) 10 else 0 11 end + 12 case 13 when type3 member of test_detail_type_tbl('25') then nvl(amt3,0) 14 else 0 15 end + 16 case 17 when type4 member of test_detail_type_tbl('25') then nvl(amt4,0) 18 else 0 19 end + 20 case 21 when type5 member of test_detail_type_tbl('25') then nvl(amt5,0) 22 else 0 23 end + 24 case 25 when type6 member of test_detail_type_tbl('25') then nvl(amt6,0) 26 else 0 27 end copay 28 from test_detail 29 / CASE_ID ITEM CO COPAY ---------- --------- -- ---------- EML3371015 133761570 10 300 EML3371015 133761570 10 20 EML3371015 133761570 10 5 EMC6369600 140328551 10 0 EMH6353995 140328551 11 5.52 SQL>
SY.
-
Dynamic display of columns in the pivot query
I have a table called STUDENT_SCORE. I need to display the avarage score/month for 2 years and the monthly score until today "month.
Then assume that it is April 2009. The report will look like
Roll_id 2007avg 2008avg Jan09-Mar09 Feb 09
101 80.9 70.9 89.7 90.9 56.8
102 70.9 23.9 87.2 90.0 76.8
I tried a bit, but can't fix the monthly score display dynamically.
DDL/DML is as below
create the table STUDENT_SCORE
(number of roll_id,
date of mth_id,
The partition number);
insert into STUDENT_SCORE values
(101, to_date (January 1, 2006 ', ' dd/mm/yyyy'), 67.5);
insert into STUDENT_SCORE values
(101, to_date (February 1, 2006 ', ' dd/mm/yyyy'), 77.5);
insert into STUDENT_SCORE values
(101, to_date (March 1, 2006 ', ' dd/mm/yyyy'), 87.5).
insert into STUDENT_SCORE values
(101, to_date (April 1, 2006 ', ' dd/mm/yyyy'), 27.5);
insert into STUDENT_SCORE values
(101, to_date (May 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (June 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (July 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (August 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (September 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (October 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (November 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (December 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (January 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (February 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (March 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (April 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (May 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (June 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (July 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (August 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (September 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (October 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (November 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (December 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (1 January 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (February 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (March 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (April 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (May 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (June 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (July 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (August 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (September 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (October 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (November 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (December 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(101, to_date (January 1, 2009 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (January 1, 2006 ', ' dd/mm/yyyy'), 67.5);
insert into STUDENT_SCORE values
(102, to_date (February 1, 2006 ', ' dd/mm/yyyy'), 77.5);
insert into STUDENT_SCORE values
(102, to_date (March 1, 2006 ', ' dd/mm/yyyy'), 87.5).
insert into STUDENT_SCORE values
(102, to_date (April 1, 2006 ', ' dd/mm/yyyy'), 27.5);
insert into STUDENT_SCORE values
(102, to_date (May 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (June 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (July 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (August 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (September 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (October 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (November 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (December 1, 2006 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (January 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (February 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (March 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (April 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (May 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (June 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (July 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (August 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (September 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (October 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (November 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (December 1, 2007 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (1 January 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (February 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (March 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (April 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (May 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (June 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (July 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (August 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (September 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (October 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (November 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (December 1, 2008 ', ' dd/mm/yyyy'), 57.5);
insert into STUDENT_SCORE values
(102, to_date (January 1, 2009 ', ' dd/mm/yyyy'), 57.5);Hello
The script below produces what you want (as far as I can tell, without your desired results).
Student_score_smry subquery Gets the year and monthly averages of student_score.
It is completely static: it always produces 11 monthly columns (from January to November) and the names of columns hardcoded (m01 through m11).
For dynamic column names you need, I used SQL * COLUMN more orders.
To get the variable number of monthly columns you need, SELECT from the main query clause uses a script under (dynamic_month.sql) to only show the correct number (which can be 0).The student_score_wk_smry of subquery retrieves the weekly averages of the student_score_wk.
He, too, is completely static: it always produces 6 weekly columns with names hardcoded (w1 through w6).There are four preliminary motions, that write two scripts under:
Prelimiary C1, C2 and C3 queries write orders of COLUMN for annual, monthly and weekly columns in dynamic_columns.sql
Preliminary motion M written between 0 and 11 items in SELECT-clause in sub-script dynamic_month.sqlThe main script is:
SET FEEDBACK OFF SET LINESIZE 200 SET PAGESIZE 0 SET VERIFY OFF SPOOL dynamic_column.sql -- Preliminary query C1: Columns for last 2 years SELECT 'COLUMN y' || LEVEL || ' HEADING "' || TO_CHAR ( ADD_MONTHS ( &target_month , 12 * (-3 + LEVEL) ) , 'YYYY' ) || 'avg"' FROM dual CONNECT BY LEVEL <= 2 ; -- Preliminary query C2: 11 prior months in this year SELECT 'COLUMN m' || TO_CHAR (LEVEL, 'fm00') || ' HEADING "' || TO_CHAR ( ADD_MONTHS ( TRUNC (&target_month, 'YYYY') , LEVEL - 1 ) , 'MonYY' ) || '"' FROM dual CONNECT BY LEVEL <= 11 ; -- Preliminary query C3: 6 weeks before target_month WITH got_w_num AS ( SELECT SUBSTR (week_desc, 1, 6) AS mon_dd , ROW_NUMBER () OVER ( ORDER BY week_id DESC ) AS w_num FROM week WHERE TO_DATE (week_desc, 'Mon DD, YYYY') < &target_month ) SELECT 'COLUMN w' || TO_CHAR (7 - w_num) || ' HEADING "Week of ' || mon_dd || '"' FROM got_w_num WHERE w_num <= 6 ORDER BY w_num DESC ; SPOOL OFF -- Preliminary Query M: 0-11 months (in SELECT clause) SPOOL dynamic_month.sql SELECT ', m' || TO_CHAR (LEVEL, 'fm00') FROM dual WHERE MONTHS_BETWEEN (&target_month, TRUNC (&target_month, 'YYYY')) >= 1 CONNECT BY LEVEL <= MONTHS_BETWEEN (&target_month, TRUNC (&target_month, 'YYYY')) ; SPOOL OFF -- Restore SQL*Plus features suppressed earlier SET FEEDBACK ON SET LINESIZE 80 SET PAGESIZE 50 -- COLUMN Commands @dynamic_column.sql -- Main Query WITH student_score_smry AS ( SELECT roll_id , AVG (CASE WHEN TRUNC (mth_id, 'YYYY') = TRUNC (ADD_MONTHS (&target_month, -24), 'YYYY') THEN score END) AS y1 , AVG (CASE WHEN TRUNC (mth_id, 'YYYY') = TRUNC (ADD_MONTHS (&target_month, -12), 'YYYY') THEN score END) AS y2 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = TRUNC (&target_month, 'YYYY') THEN score END) AS m01 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 1) THEN score END) AS m02 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 2) THEN score END) AS m03 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 3) THEN score END) AS m04 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 4) THEN score END) AS m05 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 5) THEN score END) AS m06 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 6) THEN score END) AS m07 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 7) THEN score END) AS m08 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 8) THEN score END) AS m09 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 9) THEN score END) AS m10 , AVG (CASE WHEN TRUNC (mth_id, 'MM') = ADD_MONTHS (TRUNC (&target_month, 'YYYY'), 10) THEN score END) AS m11 FROM student_score GROUP BY roll_id ) , got_w_num AS ( SELECT week_id , ROW_NUMBER () OVER ( ORDER BY week_id DESC ) AS w_num FROM week WHERE TO_DATE (week_desc, 'Mon DD, YYYY') < &target_month ) , student_score_wk_smry AS ( SELECT roll_id , AVG (CASE WHEN W_NUM = 6 THEN score END) AS w1 , AVG (CASE WHEN W_NUM = 5 THEN score END) AS w2 , AVG (CASE WHEN W_NUM = 4 THEN score END) AS w3 , AVG (CASE WHEN W_NUM = 3 THEN score END) AS w4 , AVG (CASE WHEN W_NUM = 2 THEN score END) AS w5 , AVG (CASE WHEN W_NUM = 1 THEN score END) AS w6 FROM student_score_wk s JOIN got_w_num w ON s.wk_id = w.week_id WHERE w_num <= 6 GROUP BY roll_id ) SELECT NVL (ss.roll_id, ssw.roll_id) AS roll_id , y1, y2 @dynamic_month.sql , w1, w2, w3, w4, w5, w6 FROM student_score_smry ss FULL OUTER JOIN student_score_wk_smry ssw ON ss.roll_id = ssw.roll_id ORDER BY roll_id ;
Sub-script dynamic_column.sql might look like this:
COLUMN y1 HEADING "2007avg" COLUMN y2 HEADING "2008avg" COLUMN m01 HEADING "Jan09" COLUMN m02 HEADING "Feb09" COLUMN m03 HEADING "Mar09" COLUMN m04 HEADING "Apr09" COLUMN m05 HEADING "May09" COLUMN m06 HEADING "Jun09" COLUMN m07 HEADING "Jul09" COLUMN m08 HEADING "Aug09" COLUMN m09 HEADING "Sep09" COLUMN m10 HEADING "Oct09" COLUMN m11 HEADING "Nov09" COLUMN w1 HEADING "Week of Dec 21" COLUMN w2 HEADING "Week of Dec 28" COLUMN w3 HEADING "Week of Jan 04" COLUMN w4 HEADING "Week of Jan 11" COLUMN w5 HEADING "Week of Jan 18" COLUMN w6 HEADING "Week of Jan 25"
Other sub-script, dynamic_month.sql, might look like this:
, m01 , m02 , m03
-
How to dynamically change the generated query through the "query by example".
Hi all. I'm relatively new to the ADF, so I've been beating my head against a wall trying to figure out how to do this... Any help would be greatly appreciated!
What I try to do is to make a custom search (query) against a set of tables in the database. Normally, I would use just the ability to "filter" the af: table to do this (query by example). However, the query I need to run has three specific requirements:
1. I need to change the 'WHERE' clause differently according to the field in which the user enters criteria in. For example:
-If the user enters something in the field 'name' of filter, the default ADF BC will do is add '%' to the value entered, turning it into a query "begins by". I also want to prepend a '%' to the input value, (for example "doe %'), it becomes a query 'contains '. I don't want the user to see the '%', I put at the beginning of the field in the UI, however.
-If the user enters a value of 9 digits in the field 'SSN', do a query of "accurate". If they penetrate only 4 digits, do a query "ends by". (I could always just a query "ends by" - for example "% of values" - even if they have put in 9 digits, and that would work.) However, an exact match will run faster because of the index on the field, so I want to use that if we put 9-digit.)
2. I also need to change the SELECT part of the query based on which fields are used in the filter.
3. If the user enters the filter values in several fields, I need 'OR' them together. The default functionality, it's that they are 'linked' together.
Can someone point me in the right direction? My thought was somehow replace the manner in which the query is generated when the user presses ENTER to run the filter. But, I can't seem to understand where and how to connect it to this process to customize the SELECTION and WHERE the parts of the generated query. I'm sure that there is a way, and I hope more experienced ADF'ers can direct me to the solution.
Thank you!
Published by: 926392 on June 25, 2012 15:39
Published by: 926392 on June 25, 2012 15:40Check this post in the ADF Code corner for the necessary information:
http://www.Oracle.com/technetwork/developer-tools/ADF/learnmore/30-table-filter-queries-169172.PDFThank you
Nini -
Sort on dynamic query problem!
Hello
I have a dynamic query written in pl/sql, when I check "Sort" for each field in the report attribute, error message resurrected as "ORA-01785: ORDER BY item must include the number of an expression in the SELECT list.
If I do not check sort, it works very well. In my applications, I need all the fields sorted by user, how do I solve this problem?
My query as below:
declare
Ask varchar2 (2000): = "select";
s_class varchar2 (1000);
cursor c1 is select * from demo_preference;
Start
for c1_val looping c1
If c1_val.login is not null then
query: query = | » ' || 'login ' | ',';
end if;
If c1_val.id is not null then
query: query = | » ' || 'id ' | ',';
end if;
.......
end loop;
query: = SUBSTR (query, 1, length (query)-1);
s_class: = ' (NVL (: P2_class, "%" |)) ''null%'') = ''%'' || "zero percent" OR
EXISTS (SELECT 1 FROM apex_collections WHERE collection_name = "P2CLASSCOL" AND class = c001))';
query: query = | » ' || ' from ming.reg_report_view1 where '.
|| ' ' || s_class;
Return (Query);
end;Maybe the internal column used when you clicked the sort is not indicated in the report. Try to use aliases when you build the query string, it might help apex internally to identify a column even if its order is changed to another user. After all, the order of the columns in the code is dynamic and I assume that even the number of displayed columns can vary that could sort on a column that is identified by a number not valid.
How about somewhere, displaying the report query so that you know what is the exact query processing, it could give you the best information on the problem.
If the problem persists, use a collection that is extracted these record using the same query string, then replace the report to view the collection and then set the sort column on. This way Summit could get confused about which columns are being sorted and it would just sort on a c001... C050 column as if it were a string (Yes problems with the number of sort columns when you do this).
-
Estimation of lines wrong with the size of the dynamic partition
Hi all
Version 11.1. RAC 4 knots. I met a sql that runs slowly because the bad LINES estimate. Here's the exectuion its plan.
Please note that the plan is just a part of the plan real exectuion. And I have collected statistics for all the tables involved, so I think that statistics should be updated.
Please see below. CBO estimated that only 640 lines for step 5 and 115 for this SQL totall. But the real result was almost 14K lines.
I don't know if the CBO made this mistake because of the size of the dynamic partition?
But I made another simple test, in which I just used a predict as partition_key =: b. the exectuion plan resemblesPLAN_TABLE_OUTPUT Plan hash value: 2984909170 -------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | -------------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 115 | 33005 | 2801 (8)| 00:00:09 | | | | 1 | NESTED LOOPS | | 115 | 33005 | 2801 (8)| 00:00:09 | | | | 2 | PARTITION LIST ALL | | 1 | 201 | 1482 (5)| 00:00:05 | 1 | 30 | |* 3 | TABLE ACCESS FULL | OPT_ACCT_ASDN_TYPE2_DIM | 1 | 201 | 1482 (5)| 00:00:05 | 1 | 30 | | 4 | PARTITION LIST ITERATOR| | 640 | 55040 | 1319 (12)| 00:00:05 | KEY | KEY | |* 5 | TABLE ACCESS FULL | OPT_FUND_GEN_SPNDG_FCT | 640 | 55040 | 1319 (12)| 00:00:05 | KEY | KEY | --------------------------------------------------------------------------------------------------------------------
The estimate is more reasonable this time. Can someone show me why CBO made this mistake or why think CBO, there are only 600 lines while in reality there are more than 20 k lines.PLAN_TABLE_OUTPUT Plan hash value: 1115434777 ---------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ---------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 980K| 80M| 1390 (17)| 00:00:05 | | | | 1 | PARTITION LIST SINGLE| | 980K| 80M| 1390 (17)| 00:00:05 | KEY | KEY | |* 2 | TABLE ACCESS FULL | OPT_FUND_GEN_SPNDG_FCT | 980K| 80M| 1390 (17)| 00:00:05 | KEY | KEY | ----------------------------------------------------------------------------------------------------------------
Best regards
Leon
Published by: user12064076 on March 2, 2011 12:10 AM
Published by: user12064076 on March 2, 2011 01:01
Published by: user12064076 on March 2, 2011 01:11The 'A-Time' is known to be wrong sometimes. However, the "buffers" are correct.
So the reading of the partition of the fact Table was 14Mo scan (1804 puts buffer @8 KB each). Which is really not much.
Yes, the cardinality estimate is off, but for a scan of 14Mo Oracle effort is not very high.Cardinality for the recovery of the fact table is based on the statistics of column for the BUS_UNIIT_SKID and the ACCT_SKID. Because Oracle does not know what BUS_UNIT_SKID and ACCT_SKID values will be extracted from the Dimension table that results in the query, it can't really come up with an accurate estimate - even more if the data are biased.
From the column of table-level statistics, the optimizer knows how many distinct values of BUS_UNIT_SKID exist in the table.
From the column of table-level statistics, the optimizer knows how many distinct values of ACCT_SKID exist in the table.But for a lost combination (BUS_UNIT_SKID, ACCT_SKID) in any unknown partition (the ' unknown' because it does not know the values or the partition during the query optimization) you can calculate an estimate only - the combined cardinality estimate is generally
(somewhat simplified)
Estimated_Number_of_Rows_of_Unknown_Partition X (1/Distinct_BUS_UNIT_SKIDs X 1/Distinct_ACCT_SKIDs) OR Estimated_Number_of_Rows_of_Unknown_Partition X (5% estimate X 5% estimate)
that will be very low.
You can run a plan to explain the "simplified" with the 10053 EVENT query
alter session set events '10053 trace name context forever, level 1'; explain plan for SELECT ...... exit
and display the trace file generated for more information on how Oracle considers the cardinality.
Your problem with the complex query is not likely here. You have to restyle the entire query instead of focusing on the estimation of cardinality for this fact table.
The query can be modified to help identify the Partition - Partition pruning - advance statistics?
The query can be modified in order to carry out the operations in a different sequence - get first performance and the effort of each step of the query Plan.(BTW: the Dimension table reading was the most expensive part of this performance)
Hemant K Collette
Published by: Hemant K Collette on March 3, 2011 11:50
Maybe you are looking for
-
On my computer starts, when I opened firefox, everything was different (at the time I installed Lavafox and darker Google) home page has not been loaded (instead, it was the new tab page with recent history pages), the address bar was visible, but di
-
Hello world I am currently working with tiara and I a few beginner questions. Call PicLoad ("Template") Call GraphObjNew ("2D-Axis", "2DAxis1") Call GraphObjOpen ("2DAxis1") Call GraphObjNew("2D-Curve","New_Curve") ' creates a new curve Call GraphObj
-
where to download the windows xp home edition cd
My OS has been corrupted, and I could not repair the installation. I do not have the cd with me, but the license is in my laptop. I want to download the cd (or iso image) so that I can fix my laptop OS
-
Setting System Restore Point works only with Win XP SP2
I use Windows XP Professional SP2. My System Restore Wizard does not work. I am not able to set a Point of restoring using the wizard. A suggested fix was to disable the system restore and then turn it back on. This has created more problems bec
-
Windows 7 Professional accelerometer error message
I started to get the accelerometer on my computer Windows 7 professional error message. The only change on my computer is that I downloaded and reinstalled a program I have used for years developed a glitch; and I downloaded and installed an enhanced