2

Below Query will insert two rows which are 999,666 and Oracle will give you logs as 2 Rows Inserted.

  insert into departments(department_id, department_name, manager_id,location_id)

            select 999,'Administration',200,1700 from dual
union all   select 666,'Marketing'     ,201,1800 from dual

How do i get logs like "Rows 999 and 666 has been inserted".. by putting logs statement in query itself..(User defined logs)..Like in java we are using log4j for the same..

9
  • @Tim thanks for the response but the duplicate which you marked will give you the trace option and which queries has been executed on DB.. My requirement is like i need to put the log statement for the query result...Correct me if my understanding is wrong Commented Dec 13, 2016 at 10:27
  • Update your question by showing exactly what you want logged. We can't read your mind, and you should be assuming this. Commented Dec 13, 2016 at 10:28
  • @Tim I am sorry but i need a logs like "Row 999 and 666 has been inserted" for the above query Commented Dec 13, 2016 at 10:29
  • 1
    Why do you need logs? You know the values you inserted, so you know that if there were no exceptions, then they got inserted correctly. If you absolutely must know which rows were added as part of an insert, make sure there's a unique identifier for each insert (some sort of run_number or timestamp or something) and then you can query the table for the rows you inserted. I mean, what happens if you have millions of rows to be inserted in one go; do you really want to have to do that row-by-row just to get the logging info back? Commented Dec 13, 2016 at 11:12
  • @Boneist I am building SQL script from java and script is bigger its just one example.. and those records are dynamic so i need logs for the same also thanks for the idea of unique identifier Commented Dec 13, 2016 at 11:32

2 Answers 2

3

If you don't want to modify the code you use to insert, you could define a trigger on the table to log your operations.

For example:

create or replace trigger departmentsAfterInsert
after insert
on departments
for each row
begin
    dbms_output.put_line('Inserted department ' || :new.department_id);
end;

This will log the insert operations, no matter how you do them:

SQL> insert into departments(department_id, department_name, manager_id,location_id)
  2              select 999,'Administration',200,1700 from dual
  3  union all   select 666,'Marketing'     ,201,1800 from dual;
Inserted department 999
Inserted department 666

2 rows created.
Sign up to request clarification or add additional context in comments.

Comments

2

I think it is not possible from SQL without looking on trace files. It is possible if you're going to insert row by row from PL/SQL:

declare
  cursor c is   select 999 a, 'Administration' b, 200 c, 1700 d from dual union all
                select 666 a, 'Marketing' b     , 201 c, 1800 d from dual;
begin
for x in c loop
 insert into departments(department_id, department_name, manager_id,location_id) values (x.a, x.b, x.c, x.d);
 dbms_output.put_line(x.a || ' '); --or insert to log table here
end loop;
  dbms_output.put_line('has been inserted');
end;

1 Comment

It will work for me. Instead of single query need to build cursor.. Thanks Kacper for the alternative... .Also for confirming that It wont work with SQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.