вторник, 25 ноября 2014 г.

Extract duplicate keywords from string

SQL example using wm_concat function:

 
  1. select wm_concat(distinct keyword) keywords
  2. from (
  3. SELECT LEVEL AS keyword_no
  4. , REGEXP_SUBSTR(keywords, '[^,]+', 1, LEVEL) AS keyword
  5. --, str
  6. FROM (
  7. SELECT ROWNUM AS id
  8. , 'SCOTT,ALLEN,KING,SCOTT,12345,SCOTT,12345,ALLEN,SCOTT' as keywords
  9. FROM dual
  10. )
  11. CONNECT BY
  12. -- INSTR for compatibility with Oracle 10g
  13. --INSTR(keywords, ',', 1, LEVEL-1) > 0
  14. -- From Oracle 11g may used regexp_count
  15. LEVEL <= regexp_count(keywords,',') + 1
  16. AND id = PRIOR id
  17. AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
  18. )
  19. ;
  20.  
  21. KEYWORDS
  22. --------------------------------------------
  23. 12345,ALLEN,KING,SCOTT
  24. 1 row selected.
  25.  

SQL example using listagg function:

 
  1. select listagg(keyword, ',') WITHIN GROUP (ORDER BY keyword) keywords
  2. from (
  3. select distinct keyword
  4. from (
  5. SELECT LEVEL AS keyword_no
  6. , REGEXP_SUBSTR(keywords, '[^,]+', 1, LEVEL) AS keyword
  7. --, str
  8. FROM (
  9. SELECT ROWNUM AS id
  10. , 'SCOTT,ALLEN,KING,SCOTT,12345,SCOTT,12345,ALLEN,SCOTT' as keywords
  11. FROM dual
  12. )
  13. CONNECT BY
  14. -- INSTR for compatibility with Oracle 10g
  15. --INSTR(keywords, ',', 1, LEVEL-1) > 0
  16. -- From Oracle 11g may used regexp_count
  17. LEVEL <= regexp_count(keywords,',') + 1
  18. AND id = PRIOR id
  19. AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
  20. )
  21. )
  22. ;
  23.  
  24. KEYWORDS
  25. --------------------------------------------------------------------------------
  26. 12345,ALLEN,KING,SCOTT
  27. 1 row selected.

Using PL/SQL function example:

 
  1. DECLARE
  2. l_keywords varchar2(32767) := '10,20,30,40, 10, 20,SCOTT,ALLEN,KING,SCOTT,12345,SCOTT,12345,ALLEN,SCOTT';
  3. function extract_duplicate_keywords(p_keywords varchar2) return varchar2
  4. is
  5. l_result varchar2(32767);
  6. cnt pls_integer;
  7. --l_str varchar2(4000);
  8. TYPE nested_nbr_type IS TABLE OF varchar2(4000);
  9. nnt2 nested_nbr_type := nested_nbr_type();
  10. --nnt3 nested_nbr_type;
  11. answer nested_nbr_type;
  12. j pls_integer;
  13. BEGIN
  14. nnt2 := nested_nbr_type();
  15. cnt := regexp_count(p_keywords, '[^,]+');
  16. --dbms_output.put_line('Count: ' || cnt);
  17. for i in 1 .. cnt + 1
  18. loop
  19. nnt2.extend;
  20. nnt2(nnt2.last) := trim(REGEXP_SUBSTR(p_keywords,'[^,]+', 1, i));
  21. --dbms_output.put_line(i || ': ' || l_str);
  22. end loop;
  23. --nnt3 := nnt2;
  24. answer := nnt2 MULTISET INTERSECT DISTINCT nnt2;
  25. j := answer.first;
  26. while j is not null
  27. loop
  28. if answer(j) is not null then
  29. l_result := l_result || ',' ||answer(j);
  30. --dbms_output.put_line(answer(j));
  31. end if;
  32. j := answer.next(j);
  33. end loop;
  34. return ltrim(l_result, ',');
  35. END;
  36. BEGIN
  37. l_keywords := extract_duplicate_keywords(l_keywords);
  38. dbms_output.put_line(l_keywords);
  39. END;

вторник, 18 февраля 2014 г.

How to connect SQLPlus without tnsnames.ora

Connect using one of service_name:
  1. sqlplus username/password@//host[:port][/service_name]

where service_name can be one of simple name or domain name specified in service_names parameter:
  1. show parameter service_names
  2. ALTER SYSTEM SET service_names = 'ORCL', 'us.acme.com' COMMENT='Add new services' SCOPE=BOTH;

понедельник, 17 февраля 2014 г.

Procedure to write to alert.log

Link to original article Stored Proc to write to database alert logs author Charles Kim.

From the application or from database triggers, if I want to record anything to the alert log, I can simply invoke this stored procedure and write error messages to the alert log.

Most of the DBAs have implemented alert log scanners and will send alert notifications if new ORA- error messages pop up in the alert log file.
 
  1. create or replace procedure write_msg_to_alert_log (
  2. p_message varchar2,
  3. p_oranum number default 20001
  4. )
  5. is
  6. /*
  7. create procedure under system user
  8. */
  9. begin
  10. sys.dbms_system.ksdddt;
  11. sys.dbms_system.ksdwrt(2, 'ORA-' || to_char(p_oranum) || ': ' || p_message);
  12. end;
  13. /
  14. --grant execute on write_msg_to_alert_log to public;
  15. grant execute on write_msg_to_alert_log to scott;
  16.  
  17. create public synonym write_msg_to_alert_log for system.write_msg_to_alert_log;