Solved

Procedure to run existing SQL queries

Posted on 2012-12-20
5
405 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
  • 2
  • 2
5 Comments
 
LVL 76

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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to recover a database from a user managed backup

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now