We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to run Window Command from PL/SQL

Billy Ma
Billy Ma asked
on
Medium Priority
844 Views
Last Modified: 2013-12-18
How to run Window Command from PL/SQL
Comment
Watch Question

Database Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012
Commented:
if your database is running on windows you can do it with external procedure calls, with java stored procedure or with dbms_scheduler, or with .net plugin stored procedures
note, these will run on the server only.

no code on the database server will run a windows command on your client.

here's an example of a java stored procedure to run commands
CREATE OR REPLACE TYPE "VCARRAY" AS TABLE OF VARCHAR2(4000);
/
 
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "Run_Cmd" as 
import java.io.*;
import java.sql.*;
import java.util.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
 
public class Run_Cmd{
 
    public static oracle.sql.ARRAY run_it(java.lang.String p_cmd)
    throws java.sql.SQLException,IOException
    {
        Runtime v_rt = Runtime.getRuntime();
        ArrayList v_output = new ArrayList();
 
 
        try
        {
            Process v_proc = v_rt.exec(p_cmd);
 
            BufferedReader v_stdout = new BufferedReader(
                                            new InputStreamReader(
                                                    v_proc.getInputStream()
                                                )
                                          );
            String v_line;
            while ((v_line = v_stdout.readLine()) != null)
                v_output.add(v_line);
 
            BufferedReader v_stderr = new BufferedReader(
                                            new InputStreamReader(
                                                    v_proc.getErrorStream()
                                                )
                                          );
            while ((v_line = v_stderr.readLine()) != null)
                v_output.add(v_line);
 
 
            v_proc.waitFor();
 
        }
 
        catch (Exception e)
        {
            e.printStackTrace();
        }
 
        Connection v_conn = new OracleDriver().defaultConnection();
        ArrayDescriptor v_descriptor = ArrayDescriptor.createDescriptor( "VCARRAY", v_conn );
        return new ARRAY( v_descriptor, v_conn, v_output.toArray() );
    }
}
/
 
CREATE OR REPLACE FUNCTION run_cmd(p_command IN VARCHAR2)
        RETURN vcarray
    AS
        LANGUAGE JAVA
        NAME 'Run_Cmd.run_it( java.lang.String ) return oracle.sql.ARRAY';

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Billy MaVice President

Author

Commented:
Can you tell me how to use it?
Use SQL*Plus to load the entire code to Database?
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
yes, exactly

then to run something...

select * from table(run_cmd('your command here'))


you run it as a select because the comand may return multiple lines of output or errors
Billy MaVice President

Author

Commented:
Thanks!
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
glad I could help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.