File Ownership while executing sql script from stored procedure
We have a test_command.sql script which is spooling the result into a file. From database we have one Store Procedure(run_sql) which is
calling and executing the .sql script.
When we are calling the sql script directly from the database, i e SQL > @/dccops/test_command.sql it is creating the
file under the Ownership of OS user which is connected to the system.
The problem we are facing is when we are executing the stored procedure i e exec run_sql(), the file is creating under
Oracle User.
Could u please suggest me a solution inorder to create the output file under the user who is logged to the OS.
First of all, your usage of IM speak is NOT appreciated. Please do not address anyone as if they were a 12-year old.
'Our Applcation is in C. So we have to call the procedure to run the sql script.'
This is just utter nonsense!
Oracle has Pro*C which allows Embedded SQL in C. There is also OCI (Oracle Call Interface) to call Oracle directly in C, and there is OCCI, to do the same in C++.
Apart from that, Oracle has Ole DB for Windows platforms, to allow for a .NET compatible interface to Oracle.
There is NO NEED AT ALL to call PL/SQL to run a SQL script.
Sybrand Bakker
Senior Oracle DBA
Similar Messages
-
Running a sql script from stored procedure
Hi everyone!
Has anybody tell me how to execute a sql script from a stored
procedure!
Thanks in advance!
Sasa>
Hi everyone!
Has anybody tell me how to execute a .sql file from a stored
procedure!
Thanks in advance!
Sasa Sorry, a .sql file!! -
How to execute sql scripts from Powershell across multiple databases
Re: How to execute sql scripts from Powershell across multiple databases
I have an tsql script that I want to run across a list of databases. How is the best way to do this in Powershell? Thanks.My example below, using just the SMO and not breaking up the batches, the ExecuteWithResults give the following error when the .sql file contains a GO. My script files are as simple as a DECLARE and then a GO.
WARNING: SQL Script Failed
The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-list" comm
and which is conflicting with the default formatting.
+ CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
Also, when executing from the ISE, is there a way to force the ISE to release the files. I am having to close the ISE and reopen my script every time I want to make a testing change to the .sql file.
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$scriptspath = "C:\temp\psExecuteSQLScripts\scripts"
$SQLServer = "fidevc10"
$SQLDB = "Bank03"
# Create SMO Server Object
$Server = New-Object ('Microsoft.SQLServer.Management.Smo.Server') $SQLServer
# Get SMO DB Object
$db = $Server.Databases[$SQLDB]
# Load All SQL Scripts in Directory
$scripts = Get-ChildItem -Path (Join-Path $scriptspath "*") -Include "*.sql" -Recurse
# Loop through each script and execute
foreach ($SQLScript in $scripts)
$fullpath = $SQLScript.FullName
# Read the Script File into Powershell Memory
$reader = New-Object System.IO.StreamReader($fullpath)
$script = $reader.ReadToEnd()
# Execute SQL
Write-Host "Executing $SQLScript on $SQLDB...."
try
$ds = $db.ExecuteWithResults($script)
Foreach ($t in $ds.Tables)
Foreach ($r in $t.Rows)
Foreach ($c in $t.Columns)
Write-Host $c.ColumnName "=" $r.Item($c)
Write-Host "Complete"
catch [Exception]
Write-Warning "SQL Script Failed"
echo $_.Exception|format-list -force
Write-Host " " -BackgroundColor DarkCyan -
Issue while executing OS script from Oracle
I am trying to execute the scripts provided at :
http://github.com/xtender/XT_SHELL
provided by xtender user.
As required, I have asked my DBAs to grant privileges by executing the following scripts:
Begin
--change to needed permissions and execute
dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/factiva/ODS/bin/CVIM_Rpt_ExportCSVFile’, 'read,write,execute' );
end;
/where CVIM_Rpt_ExportCSVFile is my script residing in the Unix server where my Oracle is installed.
The error I am facing when I try to execute the following command is:
select * from table(xt_shell.shell_exec('/var/scripts/CVIM_Rpt_ExportCSVFile',100))
Exception:the Permission (java.io.FilePermission /var/scripts/CVIM_Rpt_ExportCSVFile execute) has not been granted to ODS. The PL/SQL to grant this is dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/scripts/CVIM_Rpt_ExportCSVFile', 'execute' )I have asked my DBA to also execute the following scripts: - But I still see the same error as above. I am not able to figure out whats going on. Can anyone pls help me out??
EXEC Dbms_Java.Grant_Permission('ODS', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC Dbms_Java.Grant_Permission(ODS', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/scripts/CVIM_Rpt_ExportCSVFile', 'execute' )DUPLICATE
=============================================================
Issue with Executing OS commands from Stored Procedure
============================================================ -
Issue with Executing OS commands from Stored Procedure
I am trying to execute the scripts provided at :
http://github.com/xtender/XT_SHELL
provided by xtender user.
As required, I have asked my DBAs to grant privileges by executing the following scripts:
Begin
--change to needed permissions and execute
dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/factiva/ODS/bin/CVIM_Rpt_ExportCSVFile’, 'read,write,execute' );
end;
/where CVIM_Rpt_ExportCSVFile is my script residing in the Unix server where my Oracle is installed.
The error I am facing when I try to execute the following command is:
select * from table(xt_shell.shell_exec('/var/scripts/CVIM_Rpt_ExportCSVFile',100))
Exception:the Permission (java.io.FilePermission /var/scripts/CVIM_Rpt_ExportCSVFile execute) has not been granted to ODS. The PL/SQL to grant this is dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/scripts/CVIM_Rpt_ExportCSVFile', 'execute' )I have asked my DBA to also execute the following scripts: - But I still see the same error as above. I am not able to figure out whats going on. Can anyone pls help me out??
EXEC Dbms_Java.Grant_Permission('ODS', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC Dbms_Java.Grant_Permission(ODS', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
dbms_java.grant_permission( 'ODS', 'SYS:java.io.FilePermission', '/var/scripts/CVIM_Rpt_ExportCSVFile', 'execute' )DUPLICATE
===============================================================
Issue with Executing OS commands from Stored Procedure
============================================================== -
Problem with execute SSIS package from stored procedure
Hi,
I would like to execute SSIS package from stored procedure. Therefore, I implemented sp which exec SSISDB.CATALOG.CREATE_EXECUTION method. When I try to test it from SSMS on remote server, I got error that
I was able to solve by adding “WITH EXECUTE AS …”. Then I got another error: The server principal "Domain\user" is not able to access the database "SSISDB" under the current security context. On Internet, I found a couple post that describe
how to access SSIS catalog (one of them by Ke Yang -
http://blogs.msdn.com/b/mattm/archive/2012/03/20/ssis-catalog-access-control-tips.aspx). It didn’t help. I’m still getting the error message.
How to debug this issue?
Any suggestion?
Thanks
SQL Server 2014 BISSMS does not propagate user credentials thus the error
Arthur
MyBlog
Twitter -
Returning SQL cursor from Stored Procedure
Hi,
I have a query regarding returning sql cursor from stored procedure to java in oracle 11g.
I want to query some data ex: my query returns A, B, C. Also, now I want to query some other data ex: D, E, F. Now I want to return this data as an sql cursor as a single row . Example: A,B,C,D,E,F. Is it possible to return/create a cursor in stored procedure?
assume both query returns equal number of rows.. however both are not related to each other..RP wrote:
Hi,
I have a query regarding returning sql cursor from stored procedure to java in oracle 11g.
I want to query some data ex: my query returns A, B, C. Also, now I want to query some other data ex: D, E, F. Now I want to return this data as an sql cursor as a single row . Example: A,B,C,D,E,F. Is it possible to return/create a cursor in stored procedure?
assume both query returns equal number of rows.. however both are not related to each other..It sounds like what you need is a ref cursor.
First thing to remember though is that cursors do not hold any data (see: {thread:id=886365})
In it's simplest form you would be creating a procedure along these lines...
SQL> create or replace procedure get_data(p_sql in varchar2, p_rc out sys_refcursor) is
2 begin
3 open p_rc for p_sql;
4 end;
5 /
Procedure created.
SQL> var rc refcursor;
SQL> exec get_data('select empno, ename, deptno from emp', :rc);
PL/SQL procedure successfully completed.
SQL> print rc;
EMPNO ENAME DEPTNO
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected.
SQL> exec get_data('select deptno, dname from dept', :rc);
PL/SQL procedure successfully completed.
SQL> print rc
DEPTNO DNAME
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
50 IT SUPPORTWhich takes an SQL statement (as you said that both your queries were unrelated), and returns a ref cursor, and then your Java code would fetch the data using that cursor.
Now, as for getting your rows to columns and combining two queries that do that... something along these lines...
SQL> select * from x;
C
A
B
C
SQL> select * from y;
C
D
E
F
SQL> ed
Wrote file afiedt.buf
1 select x.col1, x.col2, x.col3
2 ,y.col1 as col4
3 ,y.col2 as col5
4 ,y.col3 as col6
5 from (
6 select max(decode(rn,1,col1)) as col1
7 ,max(decode(rn,2,col1)) as col2
8 ,max(decode(rn,3,col1)) as col3
9 from (select col1, rownum rn from (select * from x order by col1))
10 ) x
11 cross join
12 (
13 select max(decode(rn,1,col1)) as col1
14 ,max(decode(rn,2,col1)) as col2
15 ,max(decode(rn,3,col1)) as col3
16 from (select col1, rownum rn from (select * from y order by col1))
17* ) y
SQL> /
C C C C C C
A B C D E F... will do what you ask. For further information about turning rows to columns read the FAQ: {message:id=9360005} -
Executing sql script from file, with multiple queries
Hello,
I am trying to load an SQL script from a file, parse the script into separate SQL statements, and execute each of them. The final statement will always be a query, and thus will return a result.
Three problems arise:
1. I don't know what TYPE of statement each one is (i.e. whether it is an UPDATE or a QUERY), which seems to be required in order to execute it in java (via executeQuery and executeUpdate).
2. I am not sure of the best way to execute a series of statements (the Statement object has some "batch" functionality, but I'm not sure if there is a better way, or even of the proper way to use this).
3. I need to be able to make sure the final statement is a QUERY, and thus will return a ResultSet. Otherwise I need to be able to throw an exception.
This has been causing me a lot of trouble. Any help would be much appreciated.
Thanks,
KevinHi Kevin,
Have you got any solution for your mentioned problem????
I am facing almost the same kind of problem like I have some sql files containg simple sql statements as well as code for writting Procs , triggeres, index...functions..and so on. I need to execute this XYZ.sql file from Java side.
Can you guide me about my problem?????
Thanks in advance.............
Mak -
Executing sql scripts from url
A nice feature of isqlplus was that we could execute a sql script stored on a
server from a browser on a client, using dynamic. Is ther any comparable
facility in APEX for executing a sql script from a browser?Hi!
Here are some other threads to look at. Make sure to also look at the blog at the bottom of the 2nd thread. In the forum search set the date range to "all" and search on "run_cmd". You might have to look thru a few pages but there are several threads.
How to execute sqlplus command in html_db
Re: Returning dbms_output.putline type output to apex page
Re: RUN A DOS BATCH FILE FROM PLSQL...
Also go to ASKTOM (Tom Kyte) and search on "run_cmd":
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:3361193268672
This will give you several ideas!
Let us know if you can't get it working.
Dave Venus -
Execute .sql scripts from a remote login
Hello,
1) How do I execute a remote sql script from sqlplus?
SQL>@remote_server:$HOME/test_sql.sql ...this did not work, which would work?
2) Another question, in sqlplus I can do...! echo testing123>$HOME/test123.txt.
How do I do this within a store procedure?
This does not work, which would work?
declare
v_test varchar2(300) := '! echo testing123>$HOME/test123.txt';
begin
execute immediate v_test;
end;
/Re Q2.
I take it that's an sqlplus feature on unix as it doesn't work on windows:
SQL> ! echo testing123>$HOME/test123.txt
SP2-0734: unknown command beginning "! echo tes..." - rest of line ignored.
SQL>Also the reason this isn't working from within PL/SQL is that the execute immediate statement is used to send SQL statements to the SQL engine. What you are attempting to do is to send sqlplus commands to the SQL engine which obviously knows nothing about sqlplus commands. Those commands are very specific to the sqlplus executable and will only work in that environment.
;) -
How to execute sql script from linux command line
Hello everyone !
Just short question. On my server I must install script in crontab which will eexecute some sql statements.
How can I write command which will execute script from file without asking about password ?
sqlplus user/pass ... and then what ?
thanks
dlugasxuser10064952 wrote:
Just short question. On my server I must install script in crontab which will eexecute some sql statements.
How can I write command which will execute script from file without asking about password ?Crontab does not run your local shell profile - which means no valid Oracle environment for SQL*Plus to execute in.
On newer cron versions, you can add an environment to the crontab file itself that needs to be set before executing the scheduled command.
Whatever method - make sure you have ORACLE_HOME set, that the path includes ORACLE_HOME/bin, that ORACLE_SID is set (if not using a TNS alias) and so on.
But I agree with Sybrand... crontab is not the best place for running SQL or PL/SQL code. That should reside in the database as named code (e.g. stored procs) and be executed via DBMS_JOB or DBMS_SCHEDULE.
Less moving parts that way. No external dependancies. And will work on any Oracle server, irrespective of the o/s used and whether or not the relevant o/s user has batch/cron/etc privs to do the deed. -
hi all
i have dir /usr/tmp which there i have script in sql that creats a report
in the command line i'm running the script like this :
sqlplus user/passwd@db @name_of_script
my goal is to run this script thru the apex
i tried to create some process
before header like this
:p116 := sqlplus user/passwd@db @/usr/tmp/name_of_script ;
doesn't work!!
i even try to put this action in table but then i don't know how to run this ?
can it executed in apex ? if yes then how ?
thanks a lotI checked the link and it did not work, so I am reposting my original reply below.
had a requirement to run a Fortran program against some data that woudl be extracted from the database after the user set up their filtering criteria and made some additional input. SInce the program was to complex to conver to PL/SQL, we decided to try and invoke it from Apex. This is how I did it.
1. I followed the steps in Tim Archer's excellent article "Oracle External Procedure to Run Host Commands" (http://www.timarcher.com/?q=node/9). If the link does nto work, google the article's title.
Using this steps I created a function which accepts any OS command, including calling my own shell scripts, and runs them. I called my PL/SQL function "shell" instead of "USF_RUN_HOST_CMD " as Tim did in his example (step 9).
2. In Apex,
a. I created a button to run my shell command. (I named it P2_RUN_SHELL)
b. I created a PL/SQL process whose source looks as follows:
shell('/home/ackness/scripts/cr_xcf_file.sh > /tmp/cr_scfp_file.log');
and which was conditioned on the the button P2_RUN_SHELL.
It works like a charm.
Note: since you can run your own scripts using this method, you can encapsulate a series of commands in a UNIX shell script and invoke that script from Apex. This allows you to be able to test or run you commands from the command line as well as Apex and makes it easier to develop/debug/enhance the scripts in the future.
Ackness -
Migrating SQL Script to stored procedure...
I created a SQL script file to run in SQLPlus to
generate a fixed-format text file (See EDI_PrepForSend
below)
How do i do the same thing in a stored
package/procedure? Can I utilize the
script I already have working? I need to
do it in a stored procedure because I need
to do dynamic queries.
TIA
Chuck Plinta
[email protected]
++++++++++++++++++++++++++
-- EDI_PrepForSend.sql
SET CONCAT '#';
SET ESCAPE OFF;
SET TERM OFF
-- Set's for flat file
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 50
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET VERIFY OFF
SPOOL o:\ClientData\EDI-Send\&2\EDI-HCFA-Dtl-&1-&3.txt
COLUMN MC_Number FORMAT A15 HEADING MC_Number
COLUMN MC_Line_Number FORMAT A3 HEADING Ln#
COLUMN MC_CPT_Code FORMAT A5 HEADING Code
COLUMN MC_CPT_Modifier FORMAT A2 HEADING Mod
COLUMN MC_FDOS FORMAT A8 HEADING FDOS
COLUMN MC_LDOS FORMAT A8 HEADING LDOS
COLUMN MC_Line_Charge FORMAT 99999.99 HEADING Charge
SELECT MC.ICN MC_Number,
'???' MC_Line_Number,
MC.CPT_Code MC_CPT_Code,
MC.CPT_Modifier_1 MC_CPT_Modifier,
TO_CHAR(MC.First_DOS,'YYYYMMDD') MC_FDOS,
TO_CHAR(MC.Last_DOS,'YYYYMMDD') MC_LDOS,
MC.Charge MC_Line_Charge
FROM &1.Decision_Support DS,
&1.Medical_Claims MC
WHERE DS.Reprice_Status = 'U' AND
MC.CID = DS.CID
ORDER BY MC.ICN, MC.CID;
SPOOL OFFYou need to put your select statement in a parameterised function that returns a REF CURSOR. This is all well documented in the PL/SQL users guide.
Then you amend your SQL*PLUS script to call the function
VAR rc REFCURSOR
EXEC :rc := yr_function('&1')
PRINT rc
Cheers, APC -
Running Unix script from stored procedure
At present, I run a unix script to export my data for backup using telnet. I would like my users to run the commands on their own without my help. My users do not know telnet (they have no IT knowledge). So, I plan to create a form using Developer/2000 and let them run the procedure thru a stored procedure.But, my problem is how can a stored procedure call a unix script?
Hi,
solution, used by me.
Create a stored procedure that produces a text file with utl_file. The content of the file is the script you want to execute on your Unix box.
On your Unix Box write a shell script that scans the utl_file_dir for Files. If a file is in, chmod 744 to grnt execute rights to it and execute it.
I have a example if you want.
Start the script with crontab or let it loop with a sleep inside.
HTH
Detlev -
Calling shell script from stored procedure.
Hi Everybody,
Could anyone tell me how to call a shell script from a stored procedure.
Thanks,
VasuYou would need to write a Java stored procedure that calls out to the underlying operating system. Tom Kyte has an example of this here
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:952229840241
Make sure that you're very aware of the security implications here, however. Your commands will run as the Oracle user on the host operating system, which makes it possible that a coding error and/or an attacker could do something like delete or corrupt Oracle data files, so you'll probably want to harden the code substantially.
Justin
Maybe you are looking for
-
How can I copy all the itunes content from my pc to my new MAC?
I just bought a MacBook pro new . I had all the library, books, podcasts, music, etc in my PC, and I want to move all that to my new Mac. <How can I do that easily? Thanks!
-
PR 2.0 aká Anna. Big Failure Nokia!
In a last year, Nokia said new update with a improved browser will be released in a Early Q1. So i wait, new year is gone, january, february....now i a april? Starts Q2 and nothing? Why promises Nokia and lies? I am angry and very dissapointed. Nokia
-
Setting access rights at component level
I have created a component. The business wants to restrict its use to a certain group of users. If a user is part of that group, ONLY then the coomponent should be available in sidekick at time of page creation. How is setting access rights at compon
-
The color of visited links box around
As could be achieved in the version 10.0.2 that is created around the reference frame visited, for example. in red? In earlier versions it was to write: {border 1px solid black! important;} Now I really would not work and it helped me. Thanks for any
-
How do i bookmarks in FF for android?
How? And if It's not possible then why not?