Oracle - check if queue table exists before flushing it - oracle11g

How do you check if the queue table exist before flushing it.
I have used the below syntax to create queue table
CREATE type Message_typ as object (
subject VARCHAR2(30),
text VARCHAR2(80));
BEGIN
dbms_aqadm.CREATE_QUEUE_TABLE (
queue_table => 'XX'
,queue_payload_type => 'Message_typ'
);
END;
And the below code to purge the flush table:
declare
l_type dbms_aqadm.aq$_purge_options_t;
begin
l_type.block := true;
l_type.delivery_mode := dbms_aq.buffered;
dbms_aqadm.purge_queue_table(queue_table => 'XX',
purge_condition => '',
purge_options => l_type);
l_type.block := true;
l_type.delivery_mode := dbms_aq.persistent;
dbms_aqadm.purge_queue_table(queue_table => 'XX',
purge_condition => '',
purge_options => l_type);
end;
Please guide how to check if the queue table exists and if exists do a purge.
Thanks.

Related

DBMS Job with function

I have this DBMS Job that run at the end of day to clean the data and I have this function available CLEAN_SNAPSHOT_DATA_F(7).
I try running this script below.
begin
sys.dbms_job.submit(job => :job,
what => 'select OPTIEXEC_ADMIN.CLEAN_SNAPSHOT_DATA_F(7) from dual',
next_date => to_date('11-06-2016', 'dd-mm-yyyy'),
interval => 'SYSDATE + 24/24');
commit;
end;
But the error return ORA-01008: not all variables bound
Can you advice what the issue about the script?
Try this . Hope it helps.
DECLARE
jb_av NUMBER;
BEGIN
sys.dbms_job.submit(job => jb_av,
what => 'DECLARE lv_var VARCHAR2(32676); BEGIN select OPTIEXEC_ADMIN.CLEAN_SNAPSHOT_DATA_F(7) INTO lv_var from dual;END;',
next_date => to_date('11-06-2016', 'dd-mm-yyyy'),
interval => 'SYSDATE + 24/24');
COMMIT;
END;

How to create a procedure to run through table and send an email in Oracle Apex?

I'm currently using Oracle Apex 4.2 and I need a way to have the program run through a table and send a email if it has not been sent yet. What is the best way to do this and how?
It sounds like you just want something like this (I'm speculating about your table definition, the names of your columns, etc.)
FOR m IN (SELECT *
FROM email_table
WHERE sent_yet = 'N')
LOOP
apex_mail.send( p_to => m.to,
p_from => <<your email address>>,
p_body => m.text_of_email,
p_subj => m.subject );
UPDATE email_table
SET sent_yet = 'N'
WHERE primary_key = m.primary_key;
END LOOP;
The apex_mail.send procedure documentation shows more options that you can pass in. Note that if you're not already sending email in this environment using apex_mail, you probably want to have a separate job that actually sends the queued up emails. For example, this will submit a job that sends queued up emails every minute.
DECLARE
l_jobno pls_integer;
BEGIN
dbms_job.submit( l_jobno,
'begin apex_mail.push_queue( <<smtp server>>, <<port>> ); end;',
sysdate + interval '1' minute,
q'{sysdate + interval '1' minute}' );
commit;
END;
I figured out what was the issue. I need a line of code to allow apex to send emails through my application.
BEGIN
wwv_flow_api.set_security_group_id;
for rst in (
select aud.LAN_ID, aud.ID, sent.TEMPLATE_TEXT, temp.TEMPLATE_NAME from EMAIL_Audit aud
INNER JOIN EAMAIL_SENT sent
ON aud.TEMP_TEXT_ID = sent.id
INNER JOIN EMAIL_TEMP temp
ON sent.PROCESS_ID = temp.id
WHERE SENT_IND is null
)
loop
APEX_MAIL.SEND(
p_to => 'someEmail#email.com',
p_from => 'otherEmail#email.com',
p_body => rst.TEMPLATE_TEXT,
p_subj => rst.TEMPLATE_NAME );
END loop;
APEX_MAIL.PUSH_QUEUE;
END;​

PLS-00306: wrong number or types of arguments in call to 'select_s'

Just calling this directly from the editor (Toad). No idea why getting the above error having checked the function definition and variable types repeatedly. All of the information available online seems to be for stored procedures - which I don't believe are used here
DECLARE
TYPE attrs_type is VARRAY(10) of STRING(10);
l_ldap_host VARCHAR(255) := 'SERVERNAME';
l_ldap_port INT := 389;
l_ldap_user VARCHAR(255) := 'USERNAME';
l_ldap_passwd VARCHAR(255) := 'PASSWORD';
l_ldap_base VARCHAR(255) := 'l=something,dc=something';
l_session DBMS_LDAP.session;
l_retval NUMBER;
l_entry VARCHAR(255);
l_attrs attrs_type;
l_message VARCHAR(255) := null;
l_filter VARCHAR(255) := 'objectclass=*';
BEGIN
l_session := DBMS_LDAP.init(hostname => l_ldap_host,
portnum => l_ldap_port);
l_retval := DBMS_LDAP.simple_bind_s(ld => l_session,
dn => l_ldap_user,
passwd => l_ldap_passwd);
l_attrs(1) := '*'; -- retrieve all attributes
l_retval := DBMS_LDAP.search_s(
ld => l_session,
base => l_ldap_base,
scope => DBMS_LDAP.SCOPE_SUBTREE,
filter => l_filter,
attrs => l_attrs,
attronly => 0,
res => l_message);
DBMS_OUTPUT.put_line(l_message);
-- code to do stuff
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.put_line (SQLERRM);
END
You have to define l_attrs as dbms_ldap.string_collection, not your own type. Even if your type is defined in the same way, it will not be interchangeable with another apparently-similar type. To Oracle, your attrs_type is not the same as string_collection. Hence the error you're getting - you are indeed using the wrong type for that argument.
From the documentation:
A collection type defined in a package specification is incompatible with an identically defined local or standalone collection type.

Storing Files in Oracle DB's CLOB field

I have a column in my oracle table with CLOB datatype.
How do I store .txt file in this column and how can I retrieve the same file?
Below is the table definition
fileID Number
logFile CLOB
Thanks in advance
Loading a file into a CLOB in PL/SQL is pretty easy-- you just need to use the DBMS_LOB.LoadCLOBFromFile procedure
CREATE DIRECTORY file_dir
AS <<path on database server file system>>;
GRANT read, write
ON file_dir
TO your_user_name;
DECLARE
l_src_file bfile := BFileName( 'FILE_DIR', <<name of file>> );
l_dest_lob clob;
l_dest_offset integer := 1;
l_src_offset integer := 1;
l_lang_context number := dbms_lob.default_lang_ctx;
l_warning number;
BEGIN
dbms_lob.open( l_src_file, dbms_lob.lob_readonly );
INSERT INTO some_table( fileID, logFile )
VALUES( fileIDSeq.nextval, empty_clob() )
RETURNING logFile
INTO l_dest_lob;
dbms_lob.LoadCLOBFromFile(
dest_lob => l_dest_lob,
src_bfile => l_src_file,
amount => dbms_lob.getLength( l_src_file ),
dest_offset => l_dest_offset,
src_offset => l_src_offset,
bfile_csid => dbms_lob.default_csid,
lang_context => l_lang_context,
warning => l_warning );
dbms_lob.close( l_src_file );
END;
/
Writing the file from the CLOB to the file system again is a bit more involved. I would use something like Tom Kyte's clob_to_file procedure

What is the syntax for creating a queue subscriber in PL/SQL?

I'm trying to create a queue and a callback that triggers when a message is queued, but I can't get the callback to trigger. What am I doing wrong?
I have a trigger that enqueues a message, and I can see it on the queue message table, and I can dequeue it by hand and process it, I just can't get the callback to fire on enqueue.
BEGIN
DBMS_AQADM.CREATE_QUEUE_TABLE (
queue_table => 'queue_message_table',
queue_payload_type => 'queue_message_type',
multiple_consumers => TRUE);
DBMS_AQADM.CREATE_QUEUE (
queue_name => 'message_queue',
queue_table => 'queue_message_table');
DBMS_AQADM.START_QUEUE (queue_name => 'message_queue');
END;
CREATE OR REPLACE PROCEDURE queue_callback(
context RAW, reginfo SYS.AQ$_REG_INFO, descr SYS.AQ$_DESCRIPTOR, payload RAW, payloadl NUMBER) AS
queue_options DBMS_AQ.DEQUEUE_OPTIONS_T;
message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
my_message queue_message_type;
ret varchar2(200);
message_id RAW(16);
BEGIN
DBMS_OUTPUT.PUT_LINE('Callback');
queue_options.msgid := descr.msg_id;
queue_options.consumer_name := descr.consumer_name;
DBMS_AQ.DEQUEUE(
queue_name => descr.queue_name,
dequeue_options => queue_options,
message_properties => message_properties,
payload => my_message,
msgid => message_id );
ret := handle_message(my_message);
commit;
END;
BEGIN
DBMS_AQADM.ADD_SUBSCRIBER (queue_name => 'message_queue',
subscriber => SYS.AQ$_AGENT('queue_subscriber', 'message_queue',NULL));
DBMS_AQ.REGISTER (
SYS.AQ$_REG_INFO_LIST(
SYS.AQ$_REG_INFO(
'MESSAGE_QUEUE:QUEUE_SUBSCRIBER',
DBMS_AQ.NAMESPACE_AQ,
'plsql://QUEUE_CALLBACK',
HEXTORAW('FF')
)
), 1
);
END;
At first glance, it appears you're neither starting the queue (dbms_aqadm.start_queue), neither are you enqueueing anything to it (dbms_aq.enqueue).
I'd recommend following this demo.
You need to be careful with the database version. some bugs has been reported about issues with Oracle Aq.
In particular I've followed this link to built my own sample, executing the demo in a Oracle 11gR2 enterprise database. I was abled to enqueue, dequeue, purge the queue but the listener created with Dbms_Aq.Register didn't work.
I ran the same example downloading a Oracle 11g R2 xe database and it worked.
The same example was runned in a Oracle 10gR2 instance and it works perfectly.
There are some things that you need to be careful on using aq:
use the appropriate parameters adding the subscriber
use the appropriate namespace registering the listener with Dbms_Aq.Register
use the multiple consumers flag declaring the queue table
use the appropriate permissions to packages and to handle the queues
use the qualified name of the queues in some cases if it didn't works.
'
First create the schema
connect / as sysdba
-- #?/rdbms/admin/dbmsaqad.sql --(install if you don't have aq installed yet)
-- create the user and permissions
create user aqadmin identified by aqadmin default tablespace users temporary tablespace temp;
GRANT create session TO aqadmin;
grant connect, resource to aqadmin;
GRANT aq_administrator_role TO aqadmin IDENTIFIED BY aqadmin;
GRANT execute ON dbms_aq TO aqadmin;
GRANT execute ON dbms_aqadm TO aqadmin;
Create the ddl objects
CREATE TABLE demo_queue_message_table
( message VARCHAR2(4000) );
Create the aq-objects
create or replace type demo_queue_payload_type as object(message varchar2(4000)) ;
/
begin
DBMS_AQADM.CREATE_QUEUE_TABLE (queue_table => 'demo_queue_table', queue_payload_type => 'demo_queue_payload_type',multiple_consumers => TRUE);
DBMS_AQADM.CREATE_QUEUE (queue_name => 'demo_queue', queue_table => 'demo_queue_table');
DBMS_AQADM.START_QUEUE('demo_queue');
end;
/
CREATE or replace PROCEDURE demo_queue_callback_procedure(
context RAW,
reginfo SYS.AQ$_REG_INFO,
descr SYS.AQ$_DESCRIPTOR,
payload RAW,
payloadl NUMBER
) AS
r_dequeue_options DBMS_AQ.DEQUEUE_OPTIONS_T;
r_message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_message_handle RAW(16);
o_payload demo_queue_payload_type;
BEGIN
r_dequeue_options.msgid := descr.msg_id;
r_dequeue_options.consumer_name := descr.consumer_name;
DBMS_AQ.DEQUEUE(
queue_name => descr.queue_name,
dequeue_options => r_dequeue_options,
message_properties => r_message_properties,
payload => o_payload,
msgid => v_message_handle
);
INSERT INTO demo_queue_message_table ( message )
VALUES ( 'Message [' || o_payload.message || '] ' ||
'dequeued at [' || TO_CHAR( SYSTIMESTAMP,
'DD-MON-YYYY HH24:MI:SS.FF3' ) || ']' );
COMMIT;
END;
/
BEGIN
DBMS_AQADM.ADD_SUBSCRIBER (
queue_name => 'demo_queue',
subscriber => SYS.AQ$_AGENT(
'demo_queue_subscriber',
NULL,
NULL )
);
DBMS_AQ.REGISTER (
SYS.AQ$_REG_INFO_LIST(
SYS.AQ$_REG_INFO(
'DEMO_QUEUE:DEMO_QUEUE_SUBSCRIBER',
DBMS_AQ.NAMESPACE_AQ,
'plsql://DEMO_QUEUE_CALLBACK_PROCEDURE',
HEXTORAW('FF')
)
),
1
);
END;
/
And finally test the queue
DECLARE
r_enqueue_options DBMS_AQ.ENQUEUE_OPTIONS_T;
r_message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_message_handle RAW(16);
o_payload demo_queue_payload_type;
BEGIN
o_payload := demo_queue_payload_type(
TO_CHAR(SYSTIMESTAMP, 'DD-MON-YYYY HH24:MI:SS.FF3' )
);
DBMS_AQ.ENQUEUE(
queue_name => 'demo_queue',
enqueue_options => r_enqueue_options,
message_properties => r_message_properties,
payload => o_payload,
msgid => v_message_handle
);
COMMIT;
END;
/

Resources