Dynamic query for delete statement
Hi friends,
I have a table hierarchy where suppose i have tables with following hierarchy
Level1
level2 (child of level1)
level3 (child of level2)
level4 (child of level3)
Now i want to delete data from lowest level table depending on level one table value which is provided to me.
I have a requirement where i want to create this delete query dynamically.
Any help in this concern...
Thanks,
Sachin
Yon can use on delete cascade as satyaki suggested like
SQL> select * from level1;
ID DESCRIPTION
1 L1_1
2 L1_2
4 L1_3
5 L1_4
4 rows selected.
SQL> select * from level2;
ID DESCRIPTION L1_FK
1 L2_1 1
2 L2_2 1
4 L2_3 2
5 L2_4 2
4 rows selected.
SQL> delete from level1 where id =1;
1 row deleted.
SQL> select * from level1;
ID DESCRIPTION
2 L1_2
4 L1_3
5 L1_4
3 rows selected.
SQL> select * from level2;
ID DESCRIPTION L1_FK
4 L2_3 2
5 L2_4 2
2 rows selected.
SQL>
Similar Messages
-
How to use dynamic query for this ??
hi , i am new to ABAP. i got a requirement to write dynamic query for the following code.
kindly address. two set of queries are same.but condition is different.
.IF p_psd EQ ' '.
*C--End of change DF 1232137- (Transport # :CIDK980530 )
*C--FETCH THE Deliverd Quantiity and Material Number
SELECT aufnr "ORDER number
wemng "Quantity of goods received for the order item
matnr "MATERIAL NUMBER
pwerk "PLANT
dauat "Order Type
FROM afpo
INTO TABLE t_afpo
WHERE aufnr IN s_order
AND wemng IN s_dqt
AND matnr IN s_matnr
AND pwerk IN s_plant
AND dauat = c_ro.
IF sy-subrc = 0.
SORT t_afpo BY aufnr matnr pwerk.
*C--FETCH THE OBJECT NUMBER
SELECT aufnr "ORDER number
objnr "Object number
FROM aufk
INTO TABLE t_aufk
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr.
IF sy-subrc = 0.
SORT t_aufk BY aufnr objnr.
*C--FETCH THE Target Quantiity
SELECT aufnr "ORDER number
gamng "Total order quantity target quantity
FROM afko
INTO TABLE t_afko
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr
AND gamng IN s_tqt.
IF sy-subrc = 0.
SORT t_afko BY aufnr .
ENDIF.
ENDIF.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
*C--Begin of change DF 1232137- (Transport # :CIDK980530 )
ENDIF.
IF p_psd EQ c_x.
SELECT aufnr "ORDER number
wemng "Quantity of goods received for the order item
matnr "MATERIAL NUMBER
pwerk "PLANT
dauat "Order Type
FROM afpo
INTO TABLE t_afpo
WHERE aufnr IN s_order
AND wemng > 0
AND matnr IN s_matnr
AND pwerk IN s_plant
AND dauat = c_ro.
if sy-subrc = 0.
*C--FETCH THE OBJECT NUMBER
IF NOT t_afpo[] IS INITIAL.
SORT t_afpo BY aufnr matnr pwerk.
SELECT aufnr "ORDER number
objnr "Object number
FROM aufk
INTO TABLE t_aufk
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr.
IF sy-subrc = 0.
SORT t_afko BY aufnr gamng.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
IF NOT t_afpo[] IS INITIAL.
*C--FETCH THE Target Quantiity
SELECT aufnr "ORDER number
gamng "Total order quantity target quantity
FROM afko
INTO TABLE t_afko
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr
AND gamng <> t_afpo-wemng .
IF sy-subrc = 0.
SORT t_afko BY aufnr gamng.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
Edited by: Thomas Zloch on Jan 5, 2011 1:30 PM please use code tagsHi friend,
Try using MACRO and dynamic WHERE condition.
Group simialr Select statements under a Macro.
Build a dynamic where by checking conditions
Call macro passing dynamic where condition.
TABLES afpo.
DATA: str TYPE string.
*Macro definition
DEFINE operation.
select single *
from afpo into afpo
where (&1). " Dynamic condition
END-OF-DEFINITION.
*Build dynamic WHERE by checking some conditions
*If conditon
CONCATENATE 'AUFNR = ''000000700008''' 'AND POSNR = ''0001''' INTO str SEPARATED BY space.
*Else
CONCATENATE 'AUFNR = ''000000700008''' 'AND POSNR = ''0002''' INTO str SEPARATED BY space.
*Endif.
*Call Macro passing dynamic WHERE condition
operation str. -
Query for deleting the minimum updated record.
Hello Everybody,
I have table USER_RECENT_PROJECTS which has SIX columns USER_NAME,PROJECT_ID,CREATED_BY,CREATED_ON,UPDATED_BY
and UPDATED_ON.The purpose of having this table to get 5 recent PROJECTS
on which user has worked on.
I have trigger called RECENT_PRJ_TRIGG which IS FIRED when the data is inserted or updated on PROJECT table.After this trigger calls procedure PROC_USER_RECENT_PRJ and that procedure puts the data in this table.
It is inserting the data upto 5 records when the six records comes it deleting the record which is least UPDATED_ON from the table USER_RECENT_PROJECTS but the problem is it is deleting
the record from other user that i don't want.I want to delete the the record which is
least UPDATED_ON from particular user.
Please help me on this issue.
Here is the trigger
CREATE TRIGGER RECENT_PRJ_TRIGG
AFTER INSERT OR UPDATE ON PROJECT
FOR EACH ROW
DECLARE
NUMBER_OF_PROJECTS NUMBER:=0;
EXISTING_PROJECT_ID NUMBER:=0;
BEGIN
SELECT COUNT(*) INTO NUMBER_OF_PROJECTS FROM USER_RECENT_PROJECTS WHERE USER_NAME=:NEW.UPDATED_BY;
SELECT PROJECT_ID INTO EXISTING_PROJECT_ID FROM USER_RECENT_PROJECTS WHERE PROJECT_ID=:NEW.PROJECT_ID AND USER_NAME=:NEW.UPDATED_BY;
NVLX.PROC_USER_RECENT_PRJ(NUMBER_OF_PROJECTS,:NEW.PROJECT_ID,EXISTING_PROJECT_ID,:NEW.UPDATED_BY,:NEW.CREATED_BY,:NEW.CREATED_ON);
EXCEPTION
WHEN NO_DATA_FOUND THEN
NVLX.PROC_USER_RECENT_PRJ(NUMBER_OF_PROJECTS,:NEW.PROJECT_ID,0,:NEW.UPDATED_BY,:NEW.CREATED_BY,:NEW.CREATED_ON);
END;
And this is the procedure which is inserting the data
CREATE OR REPLACE PROCEDURE PROC_USER_RECENT_PRJ (
NUMBER_OF_PROJECTS IN NUMBER,
NEW_PROJECT_ID IN PROJECT.PROJECT_ID%TYPE,
EXISTING_PROJECT_ID IN USER_RECENT_PROJECTS.PROJECT_ID%TYPE,
USER_NAME IN CONTENT_USER.USER_NAME%TYPE,
CREATED_BY IN PROJECT.CREATED_BY%TYPE,
CREATED_ON IN PROJECT.CREATED_ON%TYPE)
IS
MAX_RECENT_PROJECTS NUMBER := 5;
BEGIN
IF NUMBER_OF_PROJECTS<=MAX_RECENT_PROJECTS AND EXISTING_PROJECT_ID=NEW_PROJECT_ID THEN
UPDATE USER_RECENT_PROJECTS SET USER_RECENT_PROJECTS.UPDATED_ON=SYSDATE,USER_RECENT_PROJECTS.UPDATED_BY=USER_NAME WHERE PROJECT_ID=EXISTING_PROJECT_ID
AND USER_RECENT_PROJECTS.USER_NAME=USER_NAME;
ELSE IF NUMBER_OF_PROJECTS<MAX_RECENT_PROJECTS AND EXISTING_PROJECT_ID!= NEW_PROJECT_ID THEN
INSERT INTO USER_RECENT_PROJECTS VALUES (USER_NAME,NEW_PROJECT_ID,CREATED_BY,CREATED_ON,USER_NAME,SYSDATE);
ELSE IF NUMBER_OF_PROJECTS=MAX_RECENT_PROJECTS AND EXISTING_PROJECT_ID!= NEW_PROJECT_ID THEN
DELETE FROM USER_RECENT_PROJECTS WHERE USER_RECENT_PROJECTS.PROJECT_ID IN(
SELECT PROJECT_ID FROM USER_RECENT_PROJECTS
WHERE UPDATED_ON=(SELECT MIN(UPDATED_ON) FROM USER_RECENT_PROJECTS
WHERE USER_RECENT_PROJECTS.USER_NAME=USER_NAME));
INSERT INTO USER_RECENT_PROJECTS VALUES (USER_NAME,NEW_PROJECT_ID,CREATED_BY,CREATED_ON,USER_NAME,SYSDATE);
END IF;
END IF;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NVLX.PROC_USER_RECENT_PRJ(NUMBER_OF_PROJECTS,NEW_PROJECT_ID,0,USER_NAME,CREATED_BY,CREATED_ON);
END PROC_USER_RECENT_PRJ;
Please help me on this issue.
Thanks in advance.....Thanks for your suggestion....
I am using the trigger for populating the data in USER_RECENT_PROJECTS.This
trigger is fired when data is INSERTED or UPDATED in PROJECT table.And that trigger will call the procedure PROC_USER_RECENT_PRJ.This is used
to put data in USER_RECENT_PROJECTS table.I am getting the problem in procedure the problem is upto five records it is inserting the record when i go for insert sixth record it should delete least UPDATED_ON and insert new record.But it is deleting the record from other user that i don't want.I want to delete the record from the paarticular user.I am using this query for deleting the record..
DELETE FROM USER_RECENT_PROJECTS WHERE USER_RECENT_PROJECTS.PROJECT_ID=(
SELECT PROJECT_ID FROM USER_RECENT_PROJECTS
WHERE UPDATED_ON=(SELECT MIN(UPDATED_ON) FROM USER_RECENT_PROJECTS WHERE USER_RECENT_PROJECTS.USER_NAME=USER_NAME)
AND USER_RECENT_PROJECTS.USER_NAME=USER_NAME
) AND USER_RECENT_PROJECTS.USER_NAME=USER_NAME;
when i fire this query individually it is deleting the proper record,but when i use it
inside procedure it is creating the problem it is deleting the record from other user.
Please suggest me other query for deletion.
Thanks in advance....... -
DYNAMIC QUERY FOR INSERT USING SELECT
Hi All,
I am facing an issue...I am un bale to do an dynamic query. i have explained my code and the sample create, insert statements. I shud insert values in a table using a select statement dynamically..
create table listing (tel_no varchar2(16), list_key varchar2(30));
create table dummy_extract ( extract_name varchar2(10),extract_query varchar2(400),extract_date date );
create table dummy_query (tel_no varchar2(16), list_key varchar2(30));
insert into listing (tel_no ,header_key) values ('123456','123.23');
insert into dummy_extract (extract_name,extract_query,extract_date) values ('SP','SELECT TEL_NO, LIST_KEY FROM LISTING','10-DEC-2010');
SET SERVEROUTPUT ON
declare
CURSOR CUR_QUERY IS
SELECT * FROM DUMMY_EXTRACT ;
V_STMT VARCHAR2(4000);
BEGIN
FOR I IN CUR_QUERY LOOP
V_STMT := 'INSERT INTO DUMMY_QUERY VALUES ' ||(I.EXTRACT_QUERY ) ;
EXECUTE IMMEDIATE V_STMT using i.extract_query;
DBMS_OUTPUT.PUT_LINE (V_STMT);
END LOOP;
END ;Hi Blue shadow and Saubik,
I tried my query.It shows missing expression...Could you tell me the reason...
SET SERVEROUTPUT ON
declare
CURSOR CUR_QUERY IS
SELECT * FROM DUMMY_EXTRACT ;
V_STMT VARCHAR2(4000);
BEGIN
FOR I IN CUR_QUERY LOOP
--V_STMT := 'INSERT INTO'|| DUMMY_QUERY ||'VALUES ' ||(I.EXTRACT_QUERY ) ;
EXECUTE IMMEDIATE 'INSERT INTO DUMMY_QUERY VALUES ' ||(I.EXTRACT_QUERY ) ;
DBMS_OUTPUT.PUT_LINE (V_STMT);
END LOOP;
END ;
Error report:
ORA-00936: missing expression
ORA-06512: at line 14
00936. 00000 - "missing expression"
*Cause:
*Action: -
Generating Dynamic Query for Ad-Hoc Reports
Hello,
What is the best way to create a dynamic query to generate ad-hoc reports? I have a couple of ideas but not sure which would work best and be most flexible.
I would like our users to be able to check checkboxes for what data they would like returned in their report. The data would be pulled from a number of tables, 10+, depending on what pieces of data they checked.
Should I write one dynamic query, with a bunch of IF statements in it?
Should I write many individual queries and then join them together in a temp query?
Should I create a temp table on our SQL server which contains the data from all of the tables; then query that? (I am worried about data being out-of-date with this option.)
Which one of these solutions should I go with or is there a better solution that I am not considering?
Thanks for the help.
JoshDo you mean a Stored Procedure? Would the Stored Procedure then have one query on it with a bunch if IF statements? Maybe a bad example, but something like this?
Yep.
I haven't written a proc for a coupla years (I have DB people to do that for me now, bless 'em), but every DB is different, so the approach will vary depending on what DB you have. It's perhaps time to buy a book or do a google or something.
Adam -
Prallel query and Delete statements
Hi Gurus, need your help in understanding parallel execution
We are noticing that delete statements are executing very slowly when parallel query is forced. Is this expected?
All the literature that I read says that parallel query has no impact on DML statements. Yet, the query plan on the delete statement shows that it will be executed in parallel mode. This could mean that the scan portion is happening in parallel but the delete operation itself is happening in serial, correct?
I tested in various servers and multiple tables before posting my question here. They all seem to show consistent results. Delete statements are twice slower when parallel query is forced. The same happens when parallel degree is set in table definition.
For your information, we are running 10g on windows server with 15 million rows in table, 5 million rows being deleted with the statement. There is one index on the table and it doesn’t match the columns in query. Query plan shows full table scan. Table is not portioned.
Thanks for your help in advanceParallel DML is supported by Oracle. Obviously when enabled, it can impact a DML statement.. (what literature have you read that said otherwise?)
The delete operation itself is done in parallel using rowid ranges (e.g. each PQ slave process does a distinct physical "piece" of the table).
Parallel DML should typically speed up the process. Why? Because I/O itself has latency. The process needs to wait (idle CPU time) for the I/O operation to complete before continuing.
So let's assume the process can only do a 100 deletes per second. The actual I/O channel is capable of a 1000 I/O's per second. But due to inherant latency, the "max delete speed limit" for a procces is a 100 I/O's per second. The full capacity of the I/O channel is thus not used (and cannot be used by a single process).
Parallel Query enables more processes to do I/O in order to utilise this "max speed limit".
Why would you see a degradation in performance? It could be due to overutilising the I/O channels (attempting to go faster than the speed limit so to say).
It could be due to some other contention in Oracle or even the o/s. You will need to investigate the wait state and events of the PQ processes to try and determine the probable cause. -
Re:Query for Stock Statement for a particular item..!!!!
Dear SAP Experts,
I need a stock statement query for a particular item which contains:
1.ItemCode
2.Item Description
3.Item Price
4.Opening
5.Receipts
6.Issues
7.Closing
8. Value
The selection criteria are:
1.Warehouse code
2.Item Name
3.Posting Date
Plz give me a good solution for this issue.
With Regards,
RevathyCheck this
Declare @FromDate Datetime
Declare @ToDate Datetime
set @FromDate =
(Select min(S0.Docdate) from OINM S0 where S0.Docdate >='[%0]')
set @ToDate =
(Select max(S1.Docdate) from OINM S1 where S1.Docdate <='[%1]')
select * from
SELECT T0.itemcode,
min(T0.Dscription) as 'Item Description',
min(B1.ItmsGrpNam) as 'Item Group', W1.Whscode, C1.Location,
(isnull((
Select sum(isnull(inqty,0))
from OINM O1
where O1.itemcode=T1.itemcode
and O1.Warehouse=W1.Whscode
and O1.docdate<@FromDate ),0)-
isnull((
Select sum(isnull(outqty,0))
from OINM O1
where O1.itemcode=T1.itemcode
and O1.Warehouse=W1.Whscode
and O1.docdate<@FromDate),0)
) as [Opening Stock],
isnull((
Select sum(isnull(inqty,0))
from OINM O1
where O1.itemcode=T1.itemcode
and O1.Warehouse=W1.Whscode
and O1.docdate>=@FromDate
and O1.docdate<=@ToDate and O1.inqty>0
and O1.transtype in (20,18)),0
) as [Purchase Quantity],
isnull((
Select sum(isnull(outqty,0))
from OINM O1
where O1.itemcode=T1.itemcode and O1.Warehouse=W1.Whscode
and O1.docdate>=@FromDate and O1.docdate<=@ToDate
and O1.outqty>0 and O1.transtype in (21,19)),0
) as [Purchase Return Quantity],
isnull((
Select sum(isnull(outqty,0))
from OINM O1
where O1.itemcode=T1.itemcode and O1.Warehouse=W1.Whscode
and O1.docdate>=@FromDate and O1.docdate<=@ToDate and O1.outqty>0
and O1.transtype in (13,15)),0
) as [sale Quatity],
(isnull
Select sum(isnull(inqty,0))
from OINM O1
where O1.itemcode=T1.itemcode and O1.Warehouse=W1.Whscode
and O1.docdate<=@ToDate),0
isnull((
Select sum(isnull(outqty,0))
from OINM O1
where O1.itemcode=T1.itemcode and O1.Warehouse=W1.Whscode
and O1.docdate<=@ToDate),0)
) as [Closing Stock]
FROM OINM T0
INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OITW T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITB B1 ON T1.ItmsGrpCod=B1.ItmsGrpCod
INNER JOIN OWHS W1 ON T2.WhsCode = W1.WhsCode
INNER JOIN OLCT C1 ON W1.Location=C1.Code
Group by T1.itemcode, T0.Itemcode, W1.WhsCode, C1.Location
) a
where (a.[Opening Stock]
+a.[Purchase Quantity]
+ a.[Purchase Return Quantity]
+a.[sale Quatity]+a.[Closing Stock]
) !=0
Regards,
Bala -
How to use dynamic query for Result table
Hello Experts,
I want to use dynamic query and then display the result in the assignment block.
Using dynamic query BTQAct and BTQRAct and base on some search criteria i want tofilter and then append the result in the result table of that custom context node, and then it should display the result in the view in UI.
SO can you please provide me the samplle code on how to use the dynamic query and append in the result table.
Regards.Hi,
Please find below sample code:
data: query TYPE REF TO cl_crm_bol_dquery_service,
result TYPE REF TO if_bol_bo_col.
DATA: lt_params TYPE crmt_name_value_pair_tab,
lwa_params TYPE crmt_name_value_pair.
query = cl_crm_bol_dquery_service=>get_instance( 'BTQAct' ). " Get instance of dynamic query
Set general query parameter for maximum number of hits
lwa_params-name = 'MAX_HITS' .
lwa_params-value = '50'.
APPEND lwa_params TO lt_params.
query->set_query_parameters( it_parameters = lt_params ).
query->add_selection_param( iv_attr_name = 'OBJECT_ID'
iv_sign = 'I'
iv_option = 'EQ'
iv_low = <lv_objectid>
iv_high = '' ). " Set your search criteria. Repeat this code if you have multiple parameters
"You can find possible search options for a query object in GENIL_BOL_BROWSER
result ?= query->get_query_result( ). " Get result from your search query
me->typed_context-> <your result context node>->set_collection( result ).
Here you will have to create a context node in your view which would refer to query result object like for BTQAct its BTQRAct
Hope this helps.
e Regards,
Bhushan -
Hi to all.........
Plz tell me the ABAP query for how to delete the duplicate posting months from dfkkop & after
then the query to count the no of rows of dfkkop ??
Thanks & Regards........
Rajeev Shrivastavahi check this..
tables:marc .
data: it_marc like marc occurs 0.
select-options:s_matnr for marc-matnr .
data: v_count type i .
select matnr
werks
from
marc into table it_marc
where matnr in s_matnr .
delete adjacent duplicates from it_marc comparing werks .
describe table it_marc lines v_count . -
Dynamic Query and Collect Statement.
Hi Gurus....
Please explain me how to write dynamic Query,,, Pl. give me with example.
Also would like to know basics of Collect statement and it's use..
Thanks
RiteshHi Ritesh,
COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab . If all non-numeric fields are same in the internal table then it will add numeric fields and maintains a single entry
If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.
Check this link to know about COLLECT statement
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm
Dynamic query can be built in SAP with the help of () values.
DATA:
V_TABNAME TYPE DDO2L-TABNAME,
V_CONDTION TYPE STRING.
V_TABNAME = 'MARA'.
V_CONDTION = 'MATNR LIKE 'S*'.
SELECT MATNR
FROM <b>(V_TABNAME)</b>
INTO TABLE ITAB
WHERE <b>(V_CONDTION)</b>.
Thanks,
Vinay -
Query for deleting rows older than 25?
Am trying to delete rows older than 25... How would the query for this be like? I tried this "DELETE FROM DB LIMIT 25,100" But getting errors...
what is older than 25? Does it mean with id greater then 25?
then you should have a query like
DELETE FROM tableName WHERE id > 25 -
Hi,
I have to generate a report which contains a query with database link. This database link is not hard coded and would be picked up from another report which contans a link to this report. So this database link value for the query could be any. Can anyone guide me hot to write a dynamic select statement so that i could append database link value at run time.
Thanks
SalmanHi,
You can not directly use a PL/SQL query to create an Interactive Report - that is not yet available.
What you could do is create a collection from your query and then use that for the report. Have a look at: [http://download-uk.oracle.com/docs/cd/B32472_01/doc/appdev.300/b32471/advnc.htm#BABGHJFD].
In there, you will see APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY - this allows you to create a collection from a string. You could add a PL/SQL process to your page that runs "On Load: Before Header" that actually creates the collection. Something like:
BEGIN
IF APEX_COLLECTION.COLLECTION_EXISTS('MYCOLLECTIONNAME') THEN
APEX_COLLECTION.DELETE_COLLECTION('MYCOLLECTIONNAME');
END IF;
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY('MYCOLLECTIONNAME','MY SQL QUERY STRING');
END;Your Interactive Report can then be based on:
SELECT SEQ_ID, C001, C002, C003, ...
FROM APEX_COLLECTIONS
WHERE COLLECTION_NAME = 'MYCOLLECTIONNAME'The tricky part is the headings as these would tend to be the SEQ_ID, C001, C002, C003, etc column names from the collection itself.
To get around this, make sure that the report's sql statement includes as many Cnnn columns as you will need for the widest of the reports. Before doing anything else, run the report once to make sure that all these columns are included in the output.
Then, through Shared Components, Application Items, create items that will hold heading names - for example, G_HEADING1, G_HEADING2 etc - one for each possibile column.
Now, update the process to:
BEGIN
IF APEX_COLLECTION.COLLECTION_EXISTS('MYCOLLECTIONNAME') THEN
APEX_COLLECTION.DELETE_COLLECTION('MYCOLLECTIONNAME');
END IF;
IF ..test1.. THEN
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY('MYCOLLECTIONNAME','MY SQL QUERY STRING 1');
:G_HEADING1 := 'ColumnHeading1Value';
:G_HEADING2 := 'ColumnHeading2Value';
:G_HEADING3 := NULL; -- column not used for this query, so set it to null
ELSIF ..test2 .. THEN
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY('MYCOLLECTIONNAME','MY SQL QUERY STRING 2');
:G_HEADING1 := 'ColumnHeading1Value';
:G_HEADING2 := 'ColumnHeading2Value';
:G_HEADING3 := 'ColumnHeading3Value';
ELSIF ...etc...
.. etc ...
END IF;
END;Finally, on the report itself, set the column headings to *&G_HEADING1.*, *&G_HEADING2.* etc. Then, on each column, set a Condition of "Value of Item in Expression 1 is NOT NULL" and in Expression 1, enter in the application item for that column - eg, G_HEADING1
I have done all that here: [http://apex.oracle.com/pls/otn/f?p=16338:5] - select either Departments or Employees from the list and the report will change.
Andy -
Dynamic sql for select statement
Hi,
Please help me with the below code:
It is updating null if the select statment returns one values. If it fetches multiple values then it throughs the below error msg when running the program.
Please let me know how to modify the below code. Or let me know is there anyother way to write this code.
Logic for development:
* View name should be passed as dynamic.
CREATE OR REPLACE
PROCEDURE "PKEP_LOAD_SO_EU" (p_var1 IN VARCHAR2)
IS
lv_sql VARCHAR2 (4000);
lv_name VARCHAR2 (20);
BEGIN
lv_sql :=
' SELECT DISTINCT LEVEL2
FROM BIIO_SALES_OB_IMPORT_'|| p_var1;
EXECUTE IMMEDIATE lv_sql
INTO lv_name;
BEGIN
UPDATE BIIO_SALES_OB_IMPORT_NA2 --table name for updation
SET SOB_1 = NULL,
SOB_DATE_1 = NULL
WHERE level2 = lv_name; -- validation ( eg. Select distinct level2 from biio_sales_ob_import_'||p_var1)
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
NULL;
END;
END;
BEGIN
PKEP_LOAD_SO_EU ('NA1');
end;
Error report:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "DEMANTRA.PKEP_LOAD_SO_EU", line 11
ORA-06512: at line 2
01422. 00000 - "exact fetch returns more than requested number of rows"
*Cause: The number specified in exact fetch is less than the rows returned.
*Action: Rewrite the query or change number of rows requested
Thanks.
PadmaPadu wrote:
Please let me know whether is anyother way to write this code as im a beginner for SQl.You dont need to have a seperate SELECT statement. Just a single UPDATE will do the job for you.
create or replace procedure pkep_load_so_eu
p_var1 in varchar2
as
begin
lSql := ' update biio_sales_ob_import_na2 ' ||
' set sob_1 = null ' ||
' , sob_date_1 = null ' ||
' where level2 in ( ' ||
' select level2 ' ||
' from biio_sales_ob_import_' || p_var1 ||
execute immediate lSql;
end; -
Need to execute Dynamic query for the report
I was wondering if we can have the option of choosing at runtime from the report builder parameter so that if the end-user selects 'Yes' then the report will run one specific query and if he chooses 'No' then another particular query is invoked. What I mean to say that by choosing between Yes and No at runtime one should have the option of returning query results based on the same table but having different columns selected via the SELECT statement
means we have two querires on our report & upon the chosen parameter we should execute only one of them ... wht should i do to apply tht ???As the previous post noted lexicals are a terrific way to accomplish dynamic queries in Oracle reports.
Another way you could accomplish this would be to use a UNION, whereas the first select in the UNION represents one user option, then second query represents the second option.
For example:
SELECT customer_name name, customer_address address
FROM customer_table
WHERE :p_choice = 'Customer Info'
UNION
SELECT employee_name name, employee_address address
FROM employee_table
WHERE :p_choice = 'Employee Info'If when running the report, the user chooses 'Customer Info' the first query would return results, if the user chose 'Employee Info' then just information from the second query would return information.
So there are different ways to accomplish what you are looking for, you just need to find which is going to work best for you.
Hope this helps! -
Syntax for delete statement within a procedure
within a procedure i tried to delete the table using
delete table tablename;
but it is showing error that
PL/SQL: SQL Statement ignored
PL/SQL: ORA-00903: invalid table name
whether we have to use execute immediate delete table tablename;
or this syntax is correctHi ,
i think if you want to use the delete
then it shld be delete from tablename --> this'll delete all rows
but if u want to use execute immediate then you might as well use TRUNCATE --> provided you need not have these data written to the log file (if you need for recovery later better use DELETE)
i.e EXECUTE IMMEDIATE ('TRUNCATE TABLE tbl_name');
hope this helps
Maybe you are looking for
-
Using wildcard in If statement
Hi. I have 2 part numbers I'm trying to find and I just want to display the text below on these 2 parts only. The 2 part numbers are: PL-PLCC44-S-01TR PL-PLCC68-S-01TR This is not on a search page, so the Like command does not work.I created this If
-
Hi All I have created a website and have uploaded it into /Library/WebServer/Documents/. I have turned on web sharing and then opened up safari and directed it to my computer name which is {apple.local} wothout the brackets, instead of it bringing up
-
HT201263 iPhone update ends with unknown error occured (14)
And not only this - it keeps showing the USB - iTunes picture on the screen of the phone. I can not go any forward nor backward. Totally stuck. My first negatuve experience with Apple is really frustrating. I have tried also all of the recommendation
-
Firefox causing high GPU usage and fluctuations
I have an NVIDIA GTX 440 graphics card and I like to monitor its speeds and tempratures. Ive noticed whenever using firefox even when im just scrolling down a page my gpu clock will shoot up to max (822 mhz) and this will of course cause the fan to s
-
My ipad is disabled because of a forgotten passcode. How can I restore to factory settings when the computer won't register the ipad as being connected because its disabled