?
Solved

Procedure to run existing SQL queries

Posted on 2012-12-20
5
Medium Priority
?
420 Views
Last Modified: 2012-12-24
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
Comment
Question by:jose11au
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38711398
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
 
LVL 14

Expert Comment

by:ajexpert
ID: 38711480
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
 

Author Comment

by:jose11au
ID: 38711517
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
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 38711546
>>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
 

Author Comment

by:jose11au
ID: 38719440
Thanks for your information. I hav been doing some reading on cursors and will start using cursors very soon.

Cheers.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question