How to use dynamic SQL in this case for best performance
I have the table with following columns
ID NUMBER,
DATA LONG,
TAG VARCHAR2(255)
Records in this table will be like following
1 this is an abstract ABSTRACT
1 this is author AUTHOR
1 100 PRICE
2 this is an abstract ABSTRACT
2 this is author AUTHOR
3 contract is this CONTRACT
Basically all the records with the same number constitute 1 record for another table. Tag in the above table indicates that what column it is and DATAwill have the actual data for that column. I need to populate the second table based an the above table but will not get the same number of TAGS all the time. I need to insert the values only for the columns provided in the TAG field. How will I accomplish this by dynamic sql. Do I create a loop and create two strings one with columns and one with values and then combine them and use execute immediate to insert into table? Is there an easier way to do this??
Please respond quickly.
Thanks
Bhawna
null
> so which collection should i use to perform it..
so that performance is best......
Program to interfaces. That way, you can switch out implementations and test for yourself which performance is best in an actual production context. But first, write your program so that it works. Worry about refactoring for performance once your program is written and it works.
> plz send me the logic....
Give it a shot on your own first; we can help if you get stuck.
~
Similar Messages
-
Which collection to use in this case for best performance....
i have two text files having 10,0000(aprox)
phone numbers , i wish to compare both the files
and write the matching numbers in third file..
so which collection should i use to perform it..
so that performance is best......
plz send me the logic....> so which collection should i use to perform it..
so that performance is best......
Program to interfaces. That way, you can switch out implementations and test for yourself which performance is best in an actual production context. But first, write your program so that it works. Worry about refactoring for performance once your program is written and it works.
> plz send me the logic....
Give it a shot on your own first; we can help if you get stuck.
~ -
Can anybody please send me a small program on How to Use Dynamic SQL.
How to execute and run give details.
Thanks
nullYou can certainly use the INTO (and USING) clauses of EXECUTE IMMEDIATE to pass in and return data, i.e.
EXECUTE IMMEDIATE sqlStmt
USING variable1, variable2
INTO output1, output2The more complex the statement, however, the more appropriate DBMS_SQL is. DBMS_SQL also has the potential to allow you to use bind variables rather than reparsing the statement many times.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
How to use Dynamic SQL in ABAP
If I have mara table and I have an selection screen where I have allowed user to select the fields of MARA,Now What I want is depending on his selection we should be able to execute the sql command.
E.g.
Tables: mara.
selection-screen
MATNR , ERSDA , ERNAM , MTART , MEINS.
NOW THE OUTPUT SHOULD BE ABLE TO DISPLAY ONLY THE SELECTED FIELD VALUE.Hello Nandan,
Try the following code. It works.
tables: mara.
data: tablename type tabname,
fname type fieldname,
fieldnames type fieldname occurs 0.
field-symbols : <fs_tableline> type any,
<fs_fieldval> type any.
initialization.
tablename = 'MARA'.
assign (tablename) to <fs_tableline> .
fname = 'MATNR'.
append fname to fieldnames.
fname = 'MTART'.
append fname to fieldnames.
select (fieldnames)
from (tablename)
up to 20 rows
into corresponding fields of <fs_tableline>.
loop at fieldnames into fname.
assign component fname of structure <fs_tableline> to <fs_fieldval>.
write : <fs_fieldval>.
endloop.
new-line.
endselect.
The <b>tables</b> statement is very important. You should at least have the list of the tables which you expect the user to enter.
Please do get back if you have any doubts. If you think the question is answered, please reward the points to the useful answers and close the thread.
Regards,
Anand Mandalika. -
How to use the List in this case.
I got sample code from http://developer.java.sun.com/developer/technicalArticles/ThirdParty/WebCrawler/. But when I compiled it, there was error message like "webcrawler.java:16: reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match List listMatches; "
I think it might be because it was written by JDK1.1.3. I am using JDK1.4 to compile it. Can any body tell me how to make it can be compiled by JDK1.4?
Thanks a lot.Hi olivia16
There is an interface called java.util.List and a class java.util.List :-)
To correct source code just put java.util. prefix at lines 16 and 76:
java.awt.List listMatches; // 16
listMatches = new java.awt.List(10); // 76
Regards. -
How to use analytical function in this case
SELECT COUNT (rms.status_code) rms_status_count,
rms.status_name rms_status_name,
TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date, 'YYYY') month_year,
MAX (rtd.add_date) date_for_sort
FROM ri_mast_status rms, ri_tran_data rtd
WHERE rtd.status_code = rms.status_code
AND TRUNC (MONTHS_BETWEEN (SYSDATE, rtd.add_date)) < 36
AND NVL (rtd.delete_flg, '0') = '0'
GROUP BY TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date, 'YYYY'),
rms.status_name
ORDER BY MAX (rtd.add_date);
it gives output for the last 3 years based on month and year.r you trying this ?
select *from
select rms.*,
row_number() over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY') order by rtd.add_date) RN,
MAX(rtd.add_date) over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY') order by rtd.add_date) date_for_sort,
COUNT(rms.status_code) over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY') order by rtd.add_date) rms_status_count
FROM ri_mast_status rms, ri_tran_data rtd
WHERE rtd.status_code = rms.status_code
AND TRUNC (MONTHS_BETWEEN (SYSDATE, rtd.add_date)) < 36
AND NVL (rtd.delete_flg, '0') = '0'
where rn=1 -
Give me hints how to re-create this table for best performance
Dear Friends,
I do not have any experience in good database desiginig practices. please advise me on this issue.
i have this table here, it gets only data inserts mostly 99%, no deletes, and very very few updates that is neglizable, please suggest me best storage parmteres,physical attributes. how much PCT FREE, PCT USED,INIT TRANS, MAX TRANS etc, all the i didnt use this storage clauses when i design tables. just a plain table design only. please give me idea, so that i can undesratand with this example and i can use for other tables in my application, our database is Oracle 8i.
this is OLTP application this table is importnat for all transactions.
this table gets 3000 records per day, but the number will increase in future.
CREATE TABLE ticket_master(ticket_no VARCHAR2(20) CONSTRAINT tick_mas_pkey PRIMARY KEY,
name_of_the_passenger VARCHAR2(50), /* 10 to 15 characters max */
nationality VARCHAR2(50), /* 10 to 15 max */
passport_id_no VARCHAR2(50), /* 10 max */
place_of_issue VARCHAR2(50), /* 12 max */
issued_by VARCHAR2(50), /* 12 max */
date_of_issue DATE,
trip_id VARCHAR2(20) CONSTRAINT tick_mas_trip_rkey REFERENCES trip_master(trip_id),
seat_no VARCHAR2(5), /* 2 only */
class VARCHAR2(50), /* 3 to 5 max */
fare NUMBER(10,2),
payment_instrument_no VARCHAR2(20),
boarding_from VARCHAR2(50), /* 10 to 15 max */
dropping_point VARCHAR2(50), /* 10 to 15 max */
journey_from VARCHAR2(50), /* 10 to 15 max */
journey_to VARCHAR2(50), /* 10 to 15 max */
paid_at VARCHAR2(50), /* 10 to 15 max */
gender CHAR(1));
and do you want me keep only this table into a seperate table space if so, give me a CREATE command for creating table space with best paramaters as a example.
thanks in advance.My $0.02
1) Since there are no deletes from the table, PCTUSED isn't going to be much of a factor.
2) The default PCTFREE of 10% will probably work just fine.
3) The table design doesn't have any not null constraints. Certainly the primary key and your FK trip_id should be not null. Things like date_of_issue and gender are probably mandatory columns also. The existance of not-null constraints help the CBO in optimizing queries.
4) It sure looks like the table could be normalized. Particurlarly the nationality column could be brought out into a nation_master table. The resultant FK could then be a number(5,0) which would make the table quite smaller (thus faster to query)...
5) Keep the table stats up-to-date as the table grows. Let the CBO take care of the rest... -
Does running this command helps sql server 2008 r2 for best performance
Sir,
1)DBCC SQLPERF('sys.dm_os_wait_stats', CLEAR)
2)DBCC FREESYSTEMCACHE ('ALL')
3)DBCC FREESESSIONCACHE
4)DBCC FREEPROCCACHE
5)DBCC DROPCLEANBUFFERS
If not the commands then what will be best idea to make server Retrieve data from Tables.
And , retrieving data from stored procedures to dashboarr.
Please , do me a need full.
Thanks in advance....Sir,
1)DBCC SQLPERF('sys.dm_os_wait_stats', CLEAR)
2)DBCC FREESYSTEMCACHE ('ALL')
3)DBCC FREESESSIONCACHE
4)DBCC FREEPROCCACHE
5)DBCC DROPCLEANBUFFERS
If not the commands then what will be best idea to make server Retrieve data from Tables.
And , retrieving data from stored procedures to dashboarr.
Above commands are no where related to retreiving data from procedure above commands will clears your waits stats information stored and also will clear cache plans which as Latheesh pointed out can cause performance issue.New plan needs to be created for each
new query hitting the database.
Your question seems misterious to me.
Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers -
How to use a CTE in a CASE statement?
Hi everyone,
I'm trying to use a CTE in a CASE statement and getting error. In fact, I'm not sure how to use a CTE in this case and this is the first time I'm using a CTE. I managed to build a CTE successfully but, failed to incorporate it in the CASE statement.
SELECT wbs,
MIN(srt),
MAX(srt)
CASE ust
WHEN
WITH CTE AS
SELECT p_id + p_name + ps_name AS colg, p_id
FROM pd_info
WHERE s_desc = 'warehouse'
OR p_desc = 'Originated from warehouse'
OR p_desc = 'Originated from a partner store'
SELECT colg FROM CTE
WHERE cte.colg IN (SELECT p_id + p_name + ps_name AS c
omb from pd_info)
OR cte.p_id = 'xxxxxxxx' THEN 'invalid'
ELSE 'valid'
END,
p_name,
sub_name,
ps_name,
FROM pd_info;
Could experts here please help me?
Thank you,
Bangaaram
Known is a DROP, Unknown is an OCEAN.With the help from some experts, I've figured out that CTE doesn't work in this case. Here are the queries that I'm using for the above. Either of them is working good so far.
SELECT Wbs
, MIN ( Srt )
, MAX ( Srt )
, CASE
WHEN S_Desc = 'warehouse'
OR P_Desc = 'Originated from warehouse'
OR P_Desc = 'Originated from a partner store'
OR P_Id = 'xxxxxxxx'
THEN 'invalid'
ELSE 'valid'
END AS Ust
, P_Name
, Sub_Name
, Ps_Name
FROM Pd_Info
GROUP BY Wbs
, CASE
WHEN S_Desc = 'warehouse'
OR P_Desc = 'Originated from warehouse'
OR P_Desc = 'Originated from a partner store'
OR P_Id = 'xxxxxxxx'
THEN 'invalid'
ELSE 'valid'
END
, P_Name
, Sub_Name
, Ps_Name;
SELECT wbs,
MIN(srt),
MAX(srt)
CASE
WHEN cte.p_id = 'xxxxxxxx'
or exists
(select *
from pd_info p2
WHERE (s_desc = 'warehouse'
OR p_desc = 'Originated from warehouse'
OR p_desc = 'Originated from a partner store' )
and p2.p_id = p1.p_id and p2.p_name = p1.p_name and p2.ps_name = p1.ps_name
THEN 'invalid'
ELSE 'valid'
END ust,
p_name,
sub_name,
ps_name,
FROM pd_info p1;
Thank you,
Bangaaram
Known is a DROP, Unknown is an OCEAN. -
How to use bind variables in this procedure
Hi Experts,
How to use bind variables in this procedure for static queries.
PROCEDURE DELETE_MER_PROC (M_id IN NUMBER)
IS
BEGIN
V_date DATE;
SELECT PD_DATE INTO v_date FROM PD_MAINTAIN;
DELETE FROM MER_CLEAR
WHERE MER_DT < v_date
AND ID = M_ID;
COMMIT;
END;
How to use v_date and m_id as bind variables in this procedure to avoid hard parsing.
Please help me.
Thanks.976208 wrote:
How to use v_date and m_id as bind variables in this procedure to avoid hard parsing.
You cannot avoid hard parsing - as the 1st time a SQL statement (like the SELECT or DELETE statements in your code) is encountered, it does not reside in the server's Shared Pool, and needs to be added into the pool via a hard parse.
Bind variables does not prevent hard parsing. Hard parsing happens when the SQL statement (with or without bind variables) is a brand new statement encountered by the server.
Bind variables enables the same SQL cursor to be reused, by simply changing the bind variable value.
Not using bind variables means that each SQL statement is unique and not shareable - as the value is hardcoded into the statement and cannot be changed via a bind value. This typically means LOTS of different SQL statements (where the only difference is the changed value in the statement) are created - with each statement being a new statement not seen before in the Shared Pool and needing to be hard parsed.
One does not design one's code not to be hard parsed. There ALWAYS will be a hard parse in order to get a SQL statement into the Shared Pool. One designs one's code to REUSE cursors in the Shared Pool. -
Hi experts, how to use open sql to read data from one " maintenance view"?
i want to use this part of data within report ,so how to use open sql statement to read data from one " maintenance view"?
Hi
You can't use OPEN SQl statements to fetch data from maintenance view
You have to use only Database views
see the different types of views and the difference
The followings are different types of views:
- Database View (SE11)
Database views are implement an inner join, that is, only records of the primary table (selected via the join operation) for which the corresponding records of the secondary tables also exist are fetched. Inconsistencies between primary and secondary table could, therefore, lead to a reduced selection set.
In database views, the join conditions can be formulated using equality relationships between any base fields. In the other types of view, they must be taken from existing foreign keys. That is, tables can only be collected in a maintenance or help view if they are linked to one another via foreign keys.
- Help View ( SE54)
Help views are used to output additional information when the online help system is called.
When the F4 button is pressed for a screen field, a check is first made on whether a matchcode is defined for this field. If this is not the case, the help view is displayed in which the check table of the field is the primary table. Thus, for each table no more than one help view can be created, that is, a table can only be primary table in at most one help view.
- Projection View
Projection views are used to suppress or mask certain fields in a table (projection), thus minimizing the number of interfaces. This means that only the data that is actually required is exchanged when the database is accessed.
A projection view can draw upon only one table. Selection conditions cannot be specified for projection views.
- Maintenance View ( SE54 )
Maintenance views enable a business-oriented approach to looking at data, while at the same time, making it possible to maintain the data involved. Data from several tables can be summarized in a maintenance view and maintained collectively via this view. That is, the data is entered via the view and then distributed to the underlying tables by the system.
Please have a look at below link. It will help you.
http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ed06446011d189700000e8322d00/frameset.htm
for more detailed info look on:
http://www.sap-img.com/abap/what-is-the-different-types-and-usage-of-views.htm
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/abap+dictionary&
Reward points for useful Answers
Regards
Anji -
Getting error while using DYNAMIC SQL
Hi Team,
I am Oracle DBA. I have limited knowledge on PL/SQL. I used below PL/SQL code to drop 50 partitons from one of the table.
I used Dynamic SQL EXECUTE IMMEDIATE to drop partions. But error occured. If I commented EXECUTE IMMEDIATE, procedure executed successfully.
Please suggest me, where i did the mistake. Also please suggest for better code than my code. please find below code and error details.
SQL> ed
Wrote file afiedt.buf
1 DECLARE
2 CURSOR DROP_PARTITON IS select partition_name from user_tab_subpartitions where PARTITION_NAME<='ABCD_2011_04';
3 BEGIN
4 for curr IN DROP_PARTITON LOOP
5 DBMS_output.put_line(curr.partition_name);
6 execute immediate(Alter table Table_Name drop partition curr.partition_name);
7 end loop;
8* END;
SQL> /
execute immediate(Alter table BILLCHRG drop partition curr.partition_name);
ERROR at line 6:
ORA-06550: line 6, column 19:
PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternative
SQL> ed
Wrote file afiedt.buf
1 DECLARE
2 CURSOR DROP_PARTITON IS select partition_name from user_tab_subpartitions where PARTITION_NAME<='ABCD_2011_04';
3 BEGIN
4 for curr IN DROP_PARTITON LOOP
5 DBMS_output.put_line(curr.partition_name);
6 --execute immediate(Alter table TABLE_NAME drop partition curr.partition_name);
7 end loop;
8* END;
SQL> /
ABCD_2009_06
ABCD_2009_06
ABCD_2009_06
BILLCHRG_2011_04
PL/SQL procedure successfully completed.PL/SQL code runs on the server, inside an Oracle process - thus PL/SQL code cannot dynamically write and display messages to the client. That server process is not connected to any keyboard, mouse or display.
DBMS_OUTPUT can be used. This is a PL/SQL buffer area in that server process that code can write lines of text too. When the server process informs the client that it has completed, the client can now request the contents of the DBMS_OUTPUT buffer and the client can display it on the client device.
This is what set serveroutput on in SQL*Plus does - tell the sqlplus client to request the DBMS_OUTPUT buffer after each Oracle server call made and to display the contents locally.
So to display the SQL command can be done using DBMS_OUTPUT. E.g.
declare
dropPart varchar2(32767);
begin
for c in (select...) loop
dropPart := 'alter table my_tab drop partition '||c.partition_name';
--// write the SQL command to DBMS_OUTPUT
DBMS_OUTPUT.put_line( dropPart );
--// execute the SQL using a begin..end block in order to catch error
begin
execute immediate dropPart;
DBMS_OUTPUT.put_line( 'command completed successfully' );
exception when OTHERS then
DBMS_OUTPUT.put_line( 'command failed with: '||SQLERRM(SQLCODE) );
end;
end loop;
end;So after this code block has been executed and partitions dropped, sqlplus will display the DBMS_OUTPUT generated by this code block. -
Using dynamic sql in triggers with :OLD values
i need to record all deleted rows from an entire schema in a single table. for that matter i created a function that receives a table name and generate an insert command according to it's primary key columns. i call this function in the table triggers. in order to insert the old values before the delete i use :OLD with "execute immediate" as followed :
create or replace trigger trg_some_tbl_bd
before delete on some_tbl
for each row is
declare
v_sql varchar2(4000);
begin
v_sql := generate_insert_command('some_table');
execute immediate v_sql;
end;
the return value from "generate_insert_command" function is the string:
insert into deleted_table (table_name , date , pk1 , pk2) values
('some_table' , sysdate , :OLD.pk1 , :OLD.pk2)
the execute immediate command notice the :OLD and looks for bind variables.
i need to know i can i bypass that. i tried looking for escape characters but couldent find any...
i would appriciate any help , it's kynda urgent
Thanks !I don't believe this is going to work. Even if you could get around the fact that :old looks like a bind variable, the :old values are not visible to the dynamic SQL statement, they're like local variables in that respect.
If you wanted to pass old values in, those values would have to be passed in as bind variables, i.e.
EXECUTE IMMEDIATE v_sql USING :old.pk1, :old.pk2which defeats the purpose of using dynamic SQL.
Since you have to create a trigger for each table, I don't see why you would bother with dynamic SQL inside the trigger-- your table structure is fixed when the trigger is created. You could write dynamic SQL that generated the triggers in the first place, but the code inside the trigger should be dynamic.
As an aside, you realize that logging every audit record into a single table creates rather massive contention issues, right? And have you considered how painful it is to query this sort of table? Have you considered other options for maintaining history like Workspace Manager? Or at least separate history tables for each table?
Justin -
How to use dynamic parameter when a report is created using Stored Procedures
Hi all,
any one have the idea of how to use dynamic parameter in crystal report XI R2
when report is created using Stored Procedure.
Regards
shashi kant chauhanHi
You can create an SQL command in Database Expert > Expand your datasource > Add command
Then enter the SQL query that will create the list of values to supply to the user
eg select field1,field2 from table
Then edit the parameter of the report. These will be the SP parameters adn can be seen in field explorer.
Change the parameter type to Dynamic
Under the word Value click on Click here to add item
Scroll down to your Command and select one of the values that you want to appear in the list
e.g field1
Then click on the Parameters field - this is essential to create the param
You can edit other options as required
That should do it for you.
I must say that i use CR 2008 connected to Oracle 10g SP, but i reckon this will work for SQL DB and CR XI as well
Best of luck -
How to use dynamic select query which queries from 3 different table.
Hi All,
I am new to Toplink, i would like to use a named query to select some of the columns from 3 different tables with dynamic where clause.
I have used the following lines. Please tell me how to get code for the dynamic where clause.
First try :
Vector objPersons = (Vector)session.executeQuery("findPersonByGlobalID",Person.class,vQueryArguments);
The above method is not returning the vector or collection.
Second Try:
ReadAllQuery query = new ReadAllQuery(Person.class);
query.useCollectionClass(LinkedList.class);
LinkedList person = (LinkedList) session.executeQuery(query);
The second try is returning the collection but, this fetches all the record from the table.
1. How to query for range of records?
2. How to query from multiple tables for some range. How to use dynamic range values ?
Please reply with your answers or some pointers would help.
Thanks and Regards,
Vijay.BHi,
Did you try using a SQLCall ? It might be able to satisfy you requirements .. :-
Employee employee = (Employee) session.executeSelectingCall(
new SQLCall("SELECT * FROM EMPLOYEE WHERE EMP_ID = 44")
);You can get more information here :-
http://www.oracle.com/technology/products/ias/toplink/doc/1013/MAIN/_html/qrybas004.htm
Regards,
Sandeep
Maybe you are looking for
-
Problem with XSU and DataSources with nested cursors?
We are having a problem with the xsu libraries (OracleXMLQuery) using nested cursor syntax. The class returns nicely nested xml document when a database connection is established in the traditional/legacy manner of "class.forName(); DriverManager.reg
-
ITunes no longer syncs "Notes" and "Reminders" to the iPod.
I bought a new MacBook Pro and I find iTunes no longer has that option. Is that correct?
-
Creating Recover Disc - HP Pavilion 17 - I'm a novice at this and really need support
I downloaded information and directions for doing this procedure from the HP website. I have a couple of questions to clarify some of my concerns. 1. Using dvd - is the writable dvd the same one you would use for burning videos or different? 2. I ju
-
SAXParser noSuchMethodError occurs in execution, not in debug with Forte
Hi gurus Im having some trouble with parsing an XML file using xerces. Basically all i want to do is take a list of XML files from a JFileChooser and then change the list to display the value of an attribute within that file instead of the original f
-
Complex animated rollover using AS3
Flash newbie here. I've been able to create a button symbol with an embedded movie clip that animates in the rollover state. The problem is that the animation is slightly complex - it runs for 20 frames - and when the user rolls out of the button, it