[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Procedure to run existing SQL queries

Hi All,

I need your advice. We have a number of SQL queries that consist of select statement, case statement, NVLs, left and inner joins, where clauses, parameters and so on. These queries extract thousand of rows, well up to 300,000 rows then the data is save in CSV files.

I was thinking of running these SQL queries from Oracle procedures that gets trigger by someone else’s procedure. Is it possible to create Oracle procedures to execute existing SQL queries without recreating a procedure from scratch? Unfortunately we don’t have time or resources or justification to PM to spend time recreating existing SQL queries.

And just another question while I am at it, are procedures the best approach to accomplish the mentioned task?

Many Thanks.
0
jose11au
Asked:
jose11au
  • 2
  • 2
1 Solution
 
slightwv (䄆 Netminder) Commented:
You can execute queries inside procedures but I'm not following what you are asking.

What is currently executing the queries?
What procedures will execute the other procedures?

I'll answer the second question once I understand a little more about what you are asking in the first.
0
 
ajexpertCommented:
Can you please let us the reason  you want to put these queries in procedure?  Are you trying to group the relative queries or performance tuning is the intent?
0
 
jose11auAuthor Commented:
Hi

I hope the info below makes more sense….Let me know if you need further clarification.

What is currently executing the queries?
The existing queries are being executed manually, that is, the developers enter the parameters needed and run the queries from Oracle SQL developer. The data gets exported to a CSV file and save to a shared folder.

What procedures will execute the other procedures?
There is a procedure that has been developed by a different department, when their procedure has been completed then the procedures we create will run.

My aim is for these SQL queries to run unattended with predefined set of parameters then export the result to CSV file and save the CSV file to a shared folder.

But first I would like to know if we can just copy and paste the SQL queries to a procedure without recreating the SQL queries because (a) they work and (b) these queries are complex.

This is the legacy of project that was suspended but some of the work they did to extract data from the databases is useful to teams.

Cheers.
0
 
slightwv (䄆 Netminder) Commented:
>>But first I would like to know if we can just copy and paste the SQL queries to a procedure without recreating the SQL queries because (a) they work and (b) these queries are complex.

Yes and no.  You can use the existing queries inside a procedure but the results of them need to be dealt with.  Likely by using a cursor.

>>when their procedure has been completed then the procedures we create will run.

Once you get your procedure tested, then you can add a line to their procedure to call yours.
or
You can create a new procedure that calls both.

I don't know of any way to set up an end-of-procedure trigger.

>> The data gets exported to a CSV file and save to a shared folder.

You can use UTL_FILE to write the output from the procedure to a file system the database server can see.

There are a ton of examples out there on how to do this.

This is the first one I found.  There might be better ones but this gives the basics:
http://nimishgarg.blogspot.com/2011/09/create-csv-file-using-plsql.html


An alternative is keep the sql you have in a sql script file and schedule a task using sqplus and the spool commands.

Still won't trigger at the end of the other procedure but is likely simpler.
0
 
jose11auAuthor Commented:
Thanks for your information. I hav been doing some reading on cursors and will start using cursors very soon.

Cheers.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now