0

I have the following script which I am using to loop through a query and export the results in an excel file.

begin
    for months in 0..12 loop
      data_dump( query_in     => 'select count(*) from reservation where trunc(update_date) between (select add_months(TRUNC(SYSDATE), -(months) ) from dual) and (select add_months(TRUNC(SYSDATE),-(months+1)) from dual)',
                               file_in      => 'excel_'||months||'.csv',
                              directory_in => 'C:\Users\Administrator\Desktop\test',
                              delimiter_in => '|' );
     end loop;
end;

The data_dump is a procedure that exports the results to a excel file.

I am trying to make the formula I use dynamic like below:

(select add_months(TRUNC(SYSDATE), -(months) ) from dual)

the months variable comes from the loop, but when i run the query it turns with errors.

Help with the syntax would be appreciated.

1
  • Please provide the error. Commented May 24, 2018 at 10:13

2 Answers 2

2

I believe that problem is here:

'select count(*) 
from reservation 
where trunc(update_date) between 
(select add_months(TRUNC(SYSDATE), -(months) ) from dual) 
and (select add_months(TRUNC(SYSDATE),-(months+1)) from dual)'

In your case 'months' is just text. You need change expression in this way:

'select count(*) 
from reservation 
where trunc(update_date) between 
(select add_months(TRUNC(SYSDATE), -('|| months ||') ) from dual) 
and (select add_months(TRUNC(SYSDATE),-('|| months+1 ||')) from dual)'
Sign up to request clarification or add additional context in comments.

Comments

0

try the months like ''||months||'' to get values dynamically

Comments

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.