Tom Kyte

Subscribe to Tom Kyte feed Tom Kyte
These are the most recently asked questions on Ask Tom
Updated: 9 hours 39 min ago

Choosing a view based on result of view

Tue, 2021-01-19 20:46
Hi all, I am having a struggle with a view. The outcome of the view can be 1 row of 3 different views with the same fields. Can someone point me in the right direction how to think please? :) Very basic, this is my problem: <code> /* MAINVIEW */ SELECT * FROM (SELECT * FROM VIEW1) --returns n rows /* IF VIEW1 IS EMPTY THEN SELECT ALL FROM VIEW2 */ (SELECT * FROM VIEW2) -- returns n rows /* IF VIEW2 IS EMPTY THEN SELECT VIEW3 (=HAS ALWAYS DATA) */ (SELECT * FROM VIEW3) -- returns 1 row </code> I don't need full code, just a hint in the right direction ;) . Thanks in advance.
Categories: DBA Blogs

Question about sequence with lower nextval than column

Tue, 2021-01-19 20:46
Hi, I have a sequence name s1 start with 1 and incremented by 1 and end with 999. I am inserting these sequence value into one table name 'e'. E table contains eno (pk) column. insert into e values(s1.nextval); I inserted 9 rows. sequence current value is 9 and 10 is already inserted in backend. I try to insert into e values(s1.nextval); then it will come pk violation error. In this case i want to insert 11 in place of 10
Categories: DBA Blogs

The JSON query results does not return the full path

Tue, 2021-01-19 20:46
Hi, I'm try to evaluate the Json query. e.g I have following json string in the table <code>{ "WO": [ { "id": "1", "name": "WO1", "action": "add", "items": [ { "id": "1", "author": "Tom Ask" }, { "id": "2", "author": "Lee Frik" } ] }, { "id": "3", "name": "WO1", "action": "add", "items": [ { "id": "1", "author": "John A" }, { "id": "2", "author": "Jean Pontus" } ] } ] }</code> <code> select d.wo_data.WO.items.id from workorder_json d;</code> I get following results: <code>["Tom Ask","Lee Frik","John A","Jean Pontus"] </code> What I hope is return with full path like following <code>{ "WO": [ "items": [ { "author": "Tom Ask" }, { "author": "Lee Frik" } ], "items": [ { "author": "John A" }, { "author": "Jean Pontus" } ] ] } </code> Is there anyway to return like above?
Categories: DBA Blogs

Advanced Queueing

Mon, 2021-01-18 08:26
Environment: Oracle 18XE 64-bit for Windows. I have a question about dequeueing an array of messages from persistent queue. It?s a simple point-to-point messaging. Queue is ?single_consumer?, without propagation. I registered my PL/SQL callback function. I need to know an exact size of array of messages to dequeue in every call of my callback function from Oracle AQ internal job. And I found the only legal way how to have done it. And this way is to register callback with <b>qosflags</b> parameter of <b>sys.aq$reg_info</b> equal to <i><u>dbms_aq.NTFN_QOS_PAYLOAD</u></i>. Here is the registration PL/SQL block: <code>declare v_qosflags number := dbms_aq.NTFN_QOS_PAYLOAD; r_info SYS.AQ$_REG_INFO; begin r_info := SYS.AQ$_REG_INFO( 'STERN.FOUNDERS_QUEUE', DBMS_AQ.NAMESPACE_AQ, 'plsql://stern.dosomecalc', HEXTORAW('FF') ); r_info.qosflags := v_qosflags; r_info.ntfn_grouping_class := dbms_aq.NTFN_GROUPING_CLASS_TIME ; r_info.ntfn_grouping_value := 60; r_info.ntfn_grouping_type := dbms_aq.NTFN_GROUPING_TYPE_SUMMARY ; DBMS_AQ.REGISTER ( SYS.AQ$_REG_INFO_LIST( r_info ), 1 ); end;</code> Here is the declaration of callback procedure. It is a standard declaration: <code>create or replace procedure dosomecalc (context RAW ,reginfo SYS.AQ$_REG_INFO ,descr SYS.AQ$_DESCRIPTOR ,payload raw ,payloadl NUMBER)</code> Now, thankfully to <b>qosflags</b> parameter initialized with <i><u>dbms_aq.NTFN_QOS_PAYLOAD</u></i> ,my callback function is registered in such a way that I always can see real size of messages to dequeue in callback session. It may be evaluated as counting size of <b>descr.msgid</b>_array part of descr parameter. Without setting of <b>qosflags</b> during registration to some value - this part of descr parameter always comes empty to callback procedure call. Once I know the real size of messages array , I can use it in <code>Dbms_aq.dequeue_array(?, array_size => descr.msgid_array.count,?) /*dequeuing call*/.</code> inside my callback function. Than, after analyze of contents of descr parameter, I found in it an <b>ntfnsRecdInGrp</b> element, and decided that <b>ntfnsRecdInGrp </b>is always equal to <b>descr.msgid_array.count</b>, and just made for programmer?s convenience, just for duplicate <b> descr.msgid_array.count</b>. AQ documentation says: <code>msgid_array - Group notification message ID list ntfnsRecdInGrp - Notifications received in group</code> That was why I decided that they are equal by value. It was a my mistake. When I use callback with array size equal to<b> descr.msgid_array.count</b> ? everything is OK. With n<b>tfnsRecdInGrp</b> ? no. Sometimes <b>descr.msgid_array.count</b> and <b>ntfnsRecdInGrp</b> equal to each other, sometimes not. Now the question is: What is the meaning of <b>ntfnsRecdInGrp</b> part of descr parameter? Why it is not the same as <b>Msgid_array.count</b>? <u></u>
Categories: DBA Blogs

How to allow in-progress client work survive database corruptions that don't require a database restart.

Mon, 2021-01-18 08:26
Good Morning, It seems like no time is spent during development efforts to assure that an application is able to survive database corruptions that don't require a database restart like say a datafile corruption or even a block corruption. If a corruption of a datafile occurs, for instance, the DBA can restore and recover the datafile, but I don't think the clients or patch processes using the application that access the corrupt datafile will survive the operation and/or the 15-60 minutes it may take to recover. I would assume that the application developers would need to do additional work on their end to even have a chance. Is there any Oracle documentation or a book that you can point me to that an organization can use to make their applications bullet proof against database corruptions that don't require shutting down the database? I would hate to have to try to reinvent this wheel by figuring out all possible Oracle error codes, timeout settings, etc. since it seems like this is something that should be done by lots of application development efforts, but my guess is that 98%+ of the time, nothing is done to try to have in-progress client work survive database corruptions that don't require a database restart. Thank you
Categories: DBA Blogs

audit once per session (unified auditing)

Mon, 2021-01-18 08:26
Hi, is it possible to audit (unified auditing) access to tables only once per session? I don't need to catch all selects from audited tables in a session, I just want to to know if a table was at querried at least once. Currently it generates huge amount of audit data, from which only fraction is needed. Thanks, Julius
Categories: DBA Blogs

Understanding reused values for sql_id, address, hash_value, plan_hash_value in v$sqlarea

Thu, 2021-01-14 12:46
good evening, I have a sql statement with the following information in v$sqlarea <code>select sql_id, address, hash_value, plan_hash_value from v$sqlarea where sqltext=<string to identify my query> sql_id |address |hash_value|plan_hash_value cv65zdurrtfus|00000000FCAA9560|2944187224|3149222761</code> I remove this object from the shared pool with the command because I want to recompute the exec plan for my sql statement <code>exec sys.dbms_shared_pool.purge('00000000FCAA9560,2944187224','c');</code> I redo my previous select statement on v$sqlarea and it retuns 0 row so I'm happy with that. Then I execute my original sql and last I redo my select statement on v$sqlarea and it returns one row with the same values <code>sql_id |address |hash_value|plan_hash_value cv65zdurrtfus|00000000FCAA9560|2944187224|3149222761</code> I was wondering how identical ids were generated, i was expecting new values even though at the end I have the expected result. Thanks for your feedback. Simon
Categories: DBA Blogs

Get date filter from a table in Oracle?

Thu, 2021-01-14 12:46
I would like to know how to access date column from a table and use it as date filter for another large volume table.. I have the following query that currently uses a date in the filter criteria and it gets completed in about 10 to 15 minutes. <code>select a,b,datec, sum(c) from table1 where datec = date '2021-01-12' group by a,b,datec</code> I'm trying to replace the hard coded date with a date from another table called table2. It's a small table with 1600 rows that just returns latest cycle completion date (one value) which is typically today's date minus one day for most days except for holidays when the cycle doesn't run.table1 is a view and it returns millions of rows. I tried the following queries in order to get the date value in the filter condition: <code>select a,b,datec, sum(c) from table1 t1, table2 t2 where t1.datec = t2.pdate and t2.prcnm = 'TC' group by a,b,datec select a,b,datec, sum(c) from table1 t1 inner join table2 t2 on datec = t2.pdate and t2.prcnm = 'TC' group by a,b,datec select a,b,datec, sum(c) from table1 t1 where t1.datec = (SELECT t2.date FROM table2 t2 WHERE prcnm = 'TC') group by a,b,datec</code> I also tried this hint: <code>select a,b,datec, sum(c) from table1 t1 where t1.datec = (SELECT /*+ PRECOMPUTE_SUBQUERY */ t2.date FROM table2 t2 WHERE prcnm = 'TC') group by a,b,datec</code> The above queries take too long and eventually fail with this error message - "parallel query server died unexpectedly" I am not even able to get 10 rows returned when I use the date from table2. I confirmed that table2 returns only one date and not multiple dates. Can you please help me in understanding why the query works when hard coded date is used, but not when a date from another table is used? thank you.
Categories: DBA Blogs

Requirements to set up an Oracle Directory for WRITE access

Thu, 2021-01-14 12:46
We have several existing Oracle Directories set up to allow reading CSV files that work fine, and a couple of them work OK to Write new files. I have been trying to add a new Directory definition pointing to a different path and cannot get it to work. I am in a corporate environment where I don't have access to the System accounts and cannot see the instance startup file, and don't have direct access to the Linux operating system, so I don't know what setup has been done for the previous Directories. One of the existing Directories that works for both read and write is defined as: <code>CREATE OR REPLACE DIRECTORY RED AS '/red/dev';</code> for the above directory, the following test code works fine to create an output file: <code>DECLARE v_file UTL_FILE.FILE_TYPE; BEGIN v_file := UTL_FILE.FOPEN(location => 'RED', filename => 'test.csv', open_mode => 'w', max_linesize => 32767); UTL_FILE.PUT_LINE(v_file, 'A,123'); UTL_FILE.FCLOSE(v_file); END; </code> I want to write some files to a subdirectory under the above path, and have found that Oracle will only allow WRITE to a named-Oracle Directory for security reasons. A new Directory I want to create is defined as: <code>CREATE OR REPLACE DIRECTORY RED_OUTPUT AS '/red/dev/OUTPUT';</code> But changing the code above to use RED_OUTPUT as the "location" or directory, results in "ORA-29283: invalid file operation: cannot open file". The '/red/dev/OUTPUT' directory location exists on the external NAS filesystem and appears to have the same permissions as the parent '/red/dev' directory (as best I can tell by using Windows Explorer to look at the directory security properties). I have read various posts online indicating things like the Oracle instance must be restarted after defining a new Oracle Directory, or that every path specified by an Oracle Directory must have a separate "mount point" on the Oracle Linux server, but I don't have easy access to do those things. The RED_OUTPUT directory can be currently used to READ an existing file if I copy one to that location using Windows Explorer. What is likely the issue with not being able to WRITE to this new RED_OUTPUT directory, and are any of these additional steps (restart, mounting, etc) necessary to make this work?
Categories: DBA Blogs

Is there a view that a DBA can query to find out if "ORA-02393: exceeded call limit on CPU usage"

Thu, 2021-01-14 12:46
Greetings, I've seen when "cpu_per_call" limit is reached. ORA-02393 is sent to the SQL Plus. Is there a view that a DBA can query to find out if "ORA-02393: exceeded call limit on CPU usage" occurs to applications using the database since it isn't written to alert log. Thanks, John
Categories: DBA Blogs

DIFFERENCE BETWEEN ANALYZE AND DBMS_STATS

Thu, 2021-01-14 12:46
DIFFERENCE BETWEEN ANALYZE AND DBMS_STATS
Categories: DBA Blogs

How to pass a parameter to a GET Handler in APEX?

Thu, 2021-01-14 12:46
Hello, I created a PL/SQL function that returns a list of open balances as a table result, where all amounts are converted to the currency provided as an input parameter: <code>function my_pkg.my_func (pi_currency in NUMBER default NULL) return amount_tab pipelined; </code> I created an Oracle REST Data Service with only GET handler: <code>select * from table(my_pkg.my_func(:to_currency)) ;</code> I tested it by Advanced REST Client and it is working as expected with an additional header for the to_currency parameter. In APEX I declared a REST Data Source related to the above REST service, then I made an APEX page with IG region based on the above REST source and it is working well as long as I am not trying to provide a parameter, i.e. until to_currency is null. When I try to populate <b>{"to_currency":"USD"}</b> in the External Filter attribute, this causes the application crash. I googled the problem but found nothing. Is any other standard way to pass the non-column parameter to the GET handler in APEX or I should write my own procedure to call REST service, e.g. by using APEX_EXEC? Thank you and best regards, Alex
Categories: DBA Blogs

blob to clob on ORDS Handler Definition

Thu, 2021-01-14 12:46
Hi! I'm trying to send a post request with json: <code> { "id": 12344444, "email": "ppppoddddddppp@gmail.com", "first_name": "", "last_name": "", "billing": { "first_name": "22222", "last_name": "", "company": "", "address_1": "", "address_2": "", "city": "", "postcode": "", "country": "", "state": "", "email": "", "phone": "" } } </code> I'm trying to use apex_json to extract information like: ?company? that is in ?billing? I read the following guide:https://oracle-base.com/articles/misc/apex_json-package-generate-and-parse-json-documents-in-oracle#parsing-json and it works but not inside ORDS Handler Definition.... I'm trying to use the following code ... but it's not insert the data and return "201": <code> DECLARE l_json_payload clob; l_blob_body blob := :body; l_dest_offset integer := 1; l_src_offset integer := 1; l_lang_context integer := dbms_lob.default_lang_ctx; l_warning PLS_INTEGER := DBMS_LOB.warn_inconvertible_char; BEGIN if dbms_lob.getlength(l_blob_body) = 0 then :status_code := 400; --error :errmsg := 'Json is empty'; return; end if; dbms_lob.createTemporary(lob_loc => l_json_payload ,cache => false); dbms_lob.converttoclob( dest_lob => l_json_payload ,src_blob => l_blob_body ,amount => dbms_lob.lobmaxsize ,dest_offset => l_dest_offset ,src_offset => l_src_offset ,blob_csid => dbms_lob.default_csid ,lang_context => l_lang_context ,warning => l_warning); APEX_JSON.parse(l_json_payload); INSERT INTO ACCOUNTS ( wp_id , name , email , f_name , l_name , wp_role , wp_username , woo_is_paying_customer , woo_billing_first_name ) VALUES ( :id, :first_name || ' ' || :last_name, :email, :first_name, :last_name, :role, :username, decode(:is_paying_customer,'false', 'N', 'Y'), APEX_JSON.get_varchar2(p_path => 'billing.first_name') ); :status_code := 201; --created EXCEPTION WHEN OTHERS THEN :status_code := 400; --error :errmsg := SQLERRM; END; </code> updating: After testing - the problem is in this line: <code> l_blob_body blob := :body; </code> When I enter it, it does not insert anything into a database update 2: after testing... I realized that it is not possible to combine: : body and other bind value, so APEX_JSON.get_varchar2 should be used instead (p_path => 'billing.first_name') So the problem was solved
Categories: DBA Blogs

MATERIALIZED VIEW Performance Issue!

Thu, 2021-01-14 12:46
I have created a MV on UAT server and my MV view using a query which has remote connectivity to PROD and select only rights to these tables which has millions of rows around 10 lakhs in each table but after calculation output of query is 139-150 rows only. query alone without MViews is taking 60 seconds but when I use CREATE MATERIALIZED VIEW NOCOMPRESS NOLOGGING BUILD IMMEDIATE USING INDEX REFRESH FORCE ON DEMAND NEXT null USING DEFAULT LOCAL ROLLBACK SEGMENT USING ENFORCED CONSTRAINTS DISABLE QUERY REWRITE as "query" mview creation happens in one hour and after that refresh time is 20-30 minutes ? which is surely not acceptable as this data is being used for dashboard with 3 minutes delay which MV should take time to refresh! I don't have privilege to anything to check on prod DB but on UAT I have sufficient access! I have tried many option but didn't work so please help me to know what is solution and if no solution what is reason behind this? in addition when my mview refresh it shows in explain plan " INSERT /*+ BYPASS_RECURSIVE_CHECK */ INTO abc". Please help me! I am really stuck here and tried my hard to get it resolved or finding a reason where I can explain to relevant team! Please help! 1. I have tried create table with same query and it took less than a minute. 2. Insert statement also working fine taking same time. 3. I tried MV view refresh option with atomic_refresh=false as well but it didn't work and actually it will not help! Please let me know if u have any info required! Note: My mv view query using prod tables(approx 4 tables) with db link from UAT.Prod server has one separate user which has been given below table rights select count(*) from abc@prod; --800000 select count(*) from abc1@prod; --700000 select count(*) from abc2@prod; --200000
Categories: DBA Blogs

Oracle update statement

Fri, 2020-12-11 22:06
I have a table with 4 columns and am passing the inputs of these 4 columns with old and new values to update them as shown below. _________________________________________ Create table (column1 IN VARCHAR2, column2 IN VARCHAR2, column3 IN VARCHAR2, column4 IN VARCHAR2, ) insert into table values ('as','af','gh','kl'); insert into table values ('ss','sf','sh','sl'); insert into table values ('s1','ssf','sfh','sasfl'); procedure test1( oldcolumn1 IN VARCHAR2, oldcolumn2 IN VARCHAR2, oldcolumn3 IN VARCHAR2, oldcolumn4 IN VARCHAR2, newcolumn1 IN VARCHAR2, newcolumn2 IN VARCHAR2, newcolumn3 IN VARCHAR2, newcolumn4 IN VARCHAR2) ________________________________________ Requirement here is to update these 4 columns with the new values where the actual column values areequal to oldcolumn inputs.But there is no guarantee that user may pass all 4 column values everttime. If they pass oldcoulumn1 value in that case need to update only the column1. If they pass oldcolumn4 values in that case need to update only the column4. if they pass olculmn1 and oldcolumn5 then need to update column1 and column5. Can you please suggest a way how to write update statements in such case?
Categories: DBA Blogs

using of binding variable in ( IN clause ) with null condition check

Fri, 2020-12-11 22:06
Hi Tom, I am struggling with a simple but tricky logic during SQL. Requirement :- I want to call SQL from shell script passing character variable as input which could be null also <code>my sql :- select * from scott.emp where ename in (&1) &1 possible values :- 1. 'JOHN' 2. 'JOHN','ROCKY' 3. Null ( NOthing to enter on user input) </code> while input values 1 and 2 are working fine, when NULL is entered , it should not check any where condition and should display all records from emp table i.e. 1=1 I am facing problem with null handling in "IN" clause and bypassing the where clause. Please help ....
Categories: DBA Blogs

Does instant client 19 works with Oracle Database 12c

Fri, 2020-12-11 03:46
We would like to know if client 19 is compatible with database Oracle 12 ?
Categories: DBA Blogs

Want to delete data from apex mail queue

Fri, 2020-12-11 03:46
Hi, How can I delete the data from apex_mail_queue using dbms_scheduler? I am trying this for creation of job- begin dbms_scheduler.create_job ( job_name => 'xyz', job_type => 'STORED_PROCEDURE', job_action => 'rescheduler', start_date => SYSTIMESTAMP, repeat_interval => 'FREQ=MINUTELY; INTERVAL=30', enabled => TRUE, auto_drop => FALSE); END; and for preocedure- CREATE OR REPLACE PROCEDURE rescheduler AS BEGIN insert into dummy_mail select * from apex_mail_queue; delete from apex_mail_queue; END; Data inserted in the dummy_mail table but not deleted from apex_mail_queue. Please help me regarding this.
Categories: DBA Blogs

PL/SQL Java Mail

Fri, 2020-12-11 03:46
We are required to use TLS for outgoing emails from our applications, we currently use a PL/SQL Javamail implementation to send out emails from the database. ( Typically alerts generated by the application ) We were told that to use TLS we need to update the JavaMail library to at least 1.6, As TLS 1.2 is supported by Java Mail 1.6 and higher. The Java Mail jar file shipped with our databases are version 1.4 or lower. How do I update the mail.jar or javax.mail.jar file in my database home ? Thanks BC
Categories: DBA Blogs

Email Through Oracle.

Fri, 2020-12-11 03:46
Hi Tom : How are U . I have writtern one procedure for sending Email through Oracle. This Code is working fine. I am able to send Mail with CC option. On that code , I wan't some further enhancement. They are as follow. 1. I am unable to send BCC. ( I need BCC option also) 2. If i am giving wrong Email address which is nopt exist then , Procedure should give some Error. 3. I need Sent Confirmation also. If abc is sending mail to xyz then after sending mail , abc should get send receipt. 4. I need Read Confirmation also. That when xys will read the email , abc should get read receipt. Please gothrough the code & let me give ur feedback on same. CREATE OR REPLACE PROCEDURE mail ( sender IN VARCHAR2, recipient IN VARCHAR2, ccrecipient IN VARCHAR2, subject IN VARCHAR2, message IN VARCHAR2 ) IS crlf VARCHAR2(2):= UTL_TCP.CRLF; connection utl_smtp.connection; mailhost VARCHAR2(30) := 'MyMailHost.com'; header VARCHAR2(1000); BEGIN -- Start the connection. connection := utl_smtp.open_connection(mailhost,25); header:= 'Date: '||TO_CHAR(SYSDATE,'dd Mon yy hh24:mi:ss')||crlf|| 'From: '||sender||''||crlf|| 'Subject: '||subject||crlf|| 'To: '||recipient||crlf|| 'CC: '||ccrecipient; -- Handshake with the SMTP server utl_smtp.helo(connection, mailhost); utl_smtp.mail(connection, sender); utl_smtp.rcpt(connection, recipient); utl_smtp.rcpt(connection, ccrecipient); utl_smtp.open_data(connection); -- Write the header utl_smtp.write_data(connection, header); utl_smtp.write_data(connection, crlf ||message); utl_smtp.close_data(connection); utl_smtp.quit(connection); EXCEPTION WHEN UTL_SMTP.INVALID_OPERATION THEN dbms_output.put_line(' Invalid Operation in SMTP transaction.'); WHEN UTL_SMTP.TRANSIENT_ERROR THEN dbms_output.put_line(' Temporary problems with sending email - try again later.'); WHEN UTL_SMTP.PERMANENT_ERROR THEN dbms_output.put_line(' Errors in code for SMTP transaction.'); END; SQL> execute mail(' "Parag" ','xyz.com','abc.com','Thanks Tom' , 'Tom is my last hope for any Oralce Problem...'); PL/SQL procedure successfully completed. Tom : Thaks a Lot. Regards - Parag
Categories: DBA Blogs

Pages