Calling shell script program SQL and passing parameters
Hi guys,.Greetings from Toronto. Is it possible to move one or more strings as parameters to a sql from shell script program?
Shell script
# Accept system input parameters
p_user_id=$1
p_job_id=$2
sqlplus.exe -s $p_user_id @$RD_PATH/rd_test.sql $p_user_id $p_job_id
SQL program (rd_test.sql)-- Accept system input parameters
define p_user_id = &1
define p_job_id = &2
-- Wondering if its possible to pass these 3 parameters from a shell script program.
-- (1) rd_test-$p_job_id-3-1.lis
-- (2) RD_PRINTER(rd_test,3)
-- (3) t_data
select
t_data=''rd_test-'||'''$p_job_id'''||'-1-1.txt''
t_data_dir=''.''
ksh $RD_PATH/another_shell_prog.sh $p_user_id $p_job_id '||a.rd_value||' '
||b.rd_value||' rd_test-$p_job_id-3-1.lis $RD_PRINTER(rd_test,3) '||
'$t_data $t_data_dir'||'
exit_status=$?
check_command'
from rd_job_parameter b, rd_job_parameter a
where a.job_id = &2
and a.jp_module_id = 'RDTEST'
and b.job_id = a.job_id
and b.module_id = a.module_id
/
As always, this is not something I have to solve that minute. Any help would be appreciatedand I will think of the problem.
Thank you
REDA
Published by: Raj404261 on June 25, 2009 11:40
Published by: Raj404261 on June 25, 2009 17:29
OK, just a few pointers possible if you need to go the way of shell script.
I suggest that you have a separate SQL script for each of your options, which retains the fairly well-defined lines and keeps the maintenance as simple as possible. Therefore, in your shell script, you can have something like:
#!/bin/ksh
# Accept system input parameters
p_user_id=$1
p_job_id=$2
# Deduce which script you'll call here
# using if statements, or even a case:
if [[ p_job_id -eq 1 ]]
then
SQL_SCRIPT=sql_script1.sql
else
SQL_SCRIPT=sql_script2.sql
fi
# Or using case
case $2
in
1) SQL_SCRIPT=sql_script1.sql;
2) SQL_SCRIPT=sql_script2.sql;
3) SQL_SCRIPT=sql_script3.sql;
*) SQL_SCRIPT=sql_script4.sql;
esac
export ${SQL_SCRIPT}
#If you need to query the database at all to deduce which script to cal
#Then something like this:
SCRIPT_VAR=`sqlplus -s username/password << EOF
set heading off
set feedback off
set verify off
SELECT * FROM DUAL;
EOF`
export {SCRIPT_VAR}
SQL_SCRIPT=sql_script${SCRIPT_VAR}.sql
# Then just call your script:
sqlplus. -s $p_user_id/password @${SQL_SCRIPT}
Tags: Database
Similar Questions
-
Call Shell Script from within PL/SQL - using DBMS_PIPE
Hi all
I'm trying to run a shell script inside my PL/SQL using DBMS_PIPE block (not sure if this is the right way or not)
Pl Sql script
==========
Set serveroutput size 100000
declare
cursor c1 is select * from mag_images
where nom_image like '% comp2.
v_path varchar2 (100): = ' / home/maguzzi/DOCUMENTS_DIR / ";
v_result number: = 0;
v_in_filename varchar2 (100): = v_path | ' test_file.jpg';
v_out_filename varchar2 (100): = v_path | ' test_file_out.jpg';
v_script varchar2 (100): = "tst_convert.sh";
v_command varchar2 (200): = 'sh'. v_path | v_script | » '|| v_in_filename | » '|| v_out_filename;
Start
A1 to c1 loop
dbms_output.put_line ('v_command: ' | v_command);
write_to_file ('test_file.jpg', 'DOCUMENTS_DIR', a1. Content);
dbms_output.put_line (' file write result: ' | v_result);
v_result: = exec_host_command (v_command);
dbms_output.put_line (' resize result :'|| file v_result);
end loop;
end;
Result DBMS_OUTPUT
====================
v_command: /home/maguzzi/DOCUMENTS_DIR/tst_convert.sh test_file.jpg test_file_out.jpg
Result of the entry of the file: 0
Result of resizing the file: 0
PL/SQL procedure successfully completed.
Function exec_host_command
=======================
FUNCTION to CREATE or REPLACE exec_host_command (lc_cmd IN VARCHAR2)
RETURN INTEGER IS
ln_status NUMBER;
lc_errormsg VARCHAR2 (80);
lc_pipe_name VARCHAR2 (30);
BEGIN
lc_pipe_name: = 'Maguzzi_PIPE ';
DBMS_PIPE. PACK_MESSAGE (lc_cmd);
ln_status: = dbms_pipe.send_message (lc_pipe_name);
RETURN ln_status;
END;
/
The Image of the BLOB is written to the correct location on the server "test_file.jpg", and although the exec_host_command function returns a value of 0, the shell script (tst_convert.sh) is not executed (i.e. no output file is created - outfile.txt and the image is not copied/converted to a new file name "" test_file_out.jpg" )
Shell script
========
tst_convert.sh
echo 'Here' > outfile.txt
Echo $1 > > outfile.txt
Echo $2 > > outfile.txt
resizing to convert $1 - $200 2
Could this be problems with permissions on files?
test_file1.jpg has permission - rw-rw-r-
test_convert.sh a permission rwxrwxrwx
When I run the script manually and pass in 2 parameters ($1 = infilename and $2 = outfilename), it works...
I'd appreciate if someone can point out my mistake or suggest another method that works.
Thanks in advance
How long does it take to create the converted file? Remember that the external Scheduler is running asynchronously, so it is possible that the converted file does not exist yet when the PL/SQL block subsequently tries to open it.
Try to add some delay seconds to the routine to ensure that the external task is completed:
begin dbms_scheduler.create_job( job_name => 'MAG_IMAGE_CONVERT2' , job_type => 'EXECUTABLE' , job_action => '/home/oracle/DOCUMENTS_DIR/tst_convert2.sh' );-- '/home/oracle/DOCUMENTS_DIR/tst_convert.sh'); dbms_scheduler.enable('MAG_IMAGE_CONVERT2'); dbms_lock.sleep(3); end;
This probably isn't really a viable solution in the long term if it turns out that this is really the problem.
-
Calling batch file. SQL file with parameters
Hi all:
I may / may not have correct code, because I can't find a Google result for answering the question and cannot test this solution until it goes live, so I want to make sure I have it correct.
I have a batch (file1.bat) file that implements some variables. Inside this file, I call a .sql like this procedure:
sqlplus username/password@environment @c:\sql\export.sql cID %%d sID % % exportFilename %
How do I use these settings within the export.sql so they are passed to the oracle procedure?
I am currently using the following code:
BEGIN
sinterface. Export (1, 2 & 3);
EXCEPTION
WHILE OTHERS
THEN NULL;
END;
This code will work? I know that if I call another code file1.bat .bat file, I can get the settings such as:
SET of cID = %1
SET sID = %2
SET exportFileName = %3
OR
SET CompID = compID %
SET SiteID = siteID %
SET exportFileName = % exportFilename %
But I have no idea if / how this translates an oracle .sql file to call a stored procedure. I got a result of Google to show using the & 1 & 2 & 3 to set the parameters of this batch file, but as I can't test it, I have no idea if it would work.
Anyone would be able to confirm the correct method?
Thank you
AndyAndy,
Welcome to the Oracle Forums. Please take a few minutes to review the following:
This forum is for general issues Oracle Forms. Please post your question in the forum SQL and PL/SQL . If you have a general question of forms, by all means, ask it here! ;-)
That said, you asked...
How do I use these settings within the export.sql so they are passed to the oracle procedure?
I am currently using the following code:
BEGIN
sinterface.export(&1, &2, &3);
EXCEPTION
WHEN OTHERS
THEN NULL;
END;
Since you simply call a Script SQL of your batch file file is not really necessary to use an anonymous block to call your stored procedure. In addition, as you use the OTHER generic exception - you never know if there was a mistake. Really, you shouldn't use SO that OTHERS value NULL. This isn't a very good programming practice. As for your variables, I would recommend that you set the substitution variables in your script and set your settings to access these variables. For example:
DEFINE c_id = &1 (CHAR)
DEFINE s_id = &2 (CHAR)
DEFINE f_name = &3 (CHAR)
Execute sinterface.export(c_id, s_id, f_name);
I would not include an exception handler at this level that any exception must propagate to the calling program.
Craig...
Basic i/o and pass parameters
I'm trying to work with shell scripts and set of stored procedures and I have a few problems with the basics, the manual is simply not seeming to give me the answers to.I think I can go a long way, I can tell how to proceed:
Create a procedure very very simple that takes one argument (a name of table for example), attributes this arg in a variable, this variable is used in a sql and also uses it to an output file and then returns the results to the file.
Something like this:
Pseudocode
Create procedure testproc (tabname (varchar (200)) AS)
tabowner varchar (200);
BEGIN
coil tabname.out
Select the owner from user_objects where object_name = tabname in tabowner;
"dbms_output.put_line (' the owner of ' | 'is' tabname | tabowner)"
END;
/
Thank you!
Floyd
Hello
You cannot call SQL * Plus commands in PL/SQL, as you try here (although it is possible to use the PL/SQL in SQL * more). If you want to write to a file in PL/SQL, you can use an integrated as UTL_FILE package.
It is quite possible, however, to achieve what you want through SQL * Plus, something like:
#!/bin/ksh
export TABLE_NAME=$1 # table passed in to script
sqlplus username/password << EOF
set pagesize 1000
spool tabname.out
BEGIN
FOR cur_rec IN (SELECT owner FROM ALL_TABLES WHERE table_name = '${TABLE_NAME}')
LOOP
dbms_output.put_line('the owner of ' || ${TABLE_NAME} || ' is ' || cur_rec.owner);
END LOOP;
END;
/
spool off
quit
EOF
exit $?
How to run and pass parameters to the exe file in the txt file.
Hello
I am facing problem with passing parameters in the .exe file, which is run by labview.firstly that I have script .txt with the settings that I have to go to the .exe file. This .exe file is also a software, and this script is written for her. And now I have to create program in labview which should satisfy the following points.
1. I have to use a button named "Run script". If I pressed the button, it should open the browser where I can choose the script .txt file.
2 .exe application is expected open(Path already chossen).
3. After passing the .exe application settings should be closed.
4 and labview should now update its parameter
I just open using Exce.vi .exe file, but I didn't get any idea to do more than this.can someone please help me?
Thanks norbert. CAN I love it? What should I change now? VI attached below...
Calling a procedure with IN and OUT parameters
HelloI usually call my procedures using the following
declare the variable msg_erreur varchar2 (50)
exec simple_msg('ABC,'ABC','ABC',:error_msg);
CREATE OR REPLACE PROCEDURE SIMPLE_MSG)
ID IN VARCHAR2,
URL IN VARCHAR2,
THE LIST IN VARCHAR2,
MSG_ERREUR OUT VARCHAR2
*************************************************************
Now my question is, I try to call a proc that has IN OUT parameters. Can someone guide me on how to call the d.a. thanks
CREATE OR REPLACE PROCEDURE SIMPLE_MSG)
ID IN VARCHAR2,
URL IN VARCHAR2,
THE LIST IN VARCHAR2,
NAME IN VARCHAR OUT,
MSG_ERREUR OUT VARCHAR2
Hello
OUTSIDE of the parameters are passed as OUT was: you must pass a variable.
If you set the IN OUT parameter before calling the procedure, and then either
(a) use the separate EXEC command:
EXEC :name := 'Original name';
EXEC simple_msg ('ABC', 'ABC', 'ABC', :name, :error_msg);
or
(b) use a PL/SQL anonymous block, like this:
BEGIN
:name := 'Original name';
simple_msg ('ABC', 'ABC', 'ABC', :name, :error_msg);
END;
/
The parameter can be either a variable binding (as illustrated above), or a local variable (which may only be used in the block).
Order urgent WLST: calling shell script
HelloIs it possible to call wlst cript i.e shell command can I call wlst.sh (present in Weblogic home/common/bin) and call then the following commands to connect to the shell script weblogic server, if yes then how.
Concerning
Rahul
Yes, you can...
Java weblogic. WLST filePath.py arg1 arg2 arg3 arg4
And filePath.py access these arguments as below:
domainName = sys.argv [1]
host name = sys.argv [2]
users = sys.argv [3]
groups = sys.argv [4]
Creation of database without executing the script catproc.sql and catalog.sql
HelloI created the database of towing without running catalog.sql and catproc.sql. Someone knows what's the downside to not run them?
ARM
You can run them at any time, and Yes, you must be connected as sysdba. In fact, it's in the scripts generated dbca.
----------------
Sybrand Bakker
Senior Oracle DBA
Shell script to monitor and alert DB support TBS space issues
Hello:I am looking to build a shell script which would basically be to connect to the database using tnsnames.ora and check the availability of space on each of the storage space in a database. Send an alert message to dbsupport if it is less then a specified threshold value. Y at - it a script that you can share it with me.
Kind regards
BMP
I would like to know if it works... or if you have any problem... Let me know...
To call the function in sql and script call
Hello
I'll have a .sql script that returns the name of the partition and I need to run alter table Exchange partition something like this
get_part. SQL
COLUMN file_name new_value by sub_script_col
accept the start_date char prompt arguments ' input start (exact): '
accept the end_date char prompt "enter end date (exact): '"
create or replace function get_part_name (p_date in date)
VARCHAR2 is back
date d;
RETP varchar2 (30);
spirit date: = to_date('4444-01-01','yyyy-mm-dd');
Str varchar2 (32000);
cursor c is
Select high_value, nom_partition p
of all_tab_partitions
where table_name = 'TO_TEHBLNC ';
Start
for r in c loop
Str: = r.high_value;
run immediately 'select'. Str |' double ' in d;
If p_date < d and d, then the spirit
RETP: = r.p;
spirit: = d;
end if;
end loop;
return pter;
end;
Select get_part_name ('& 1') twice;
ACCEPT user_yn PROMPT ' do you want to partion with < partition name > table? (Y, N) : "
SELECT THE CHECK BOX
WHEN TOP (LTRIM (' & user_yn')) AS 'y %. "
THEN "insert_partition".
ELSE 'do_nothing.
END AS sub_script_col
DOUBLE;
@ &sub_script_col < partition_name >
sub_script_col. SQL
ALTER TABLE to_tehblnc
SWAP PARTITION < name patition >
WITH TABLE T1
WITHOUT VALIDATION
UPDATE GLOBAL INDEXES;
I need to call once again for the sub_script.sql based on start_date and end_date, for example 1st January 2015 and January 5, 2015 I must first get the parition name and proceed to modify the table with that respective partition name returned by the function, so that it will be 5 times alter the table to swap partition
I'll have a .sql script that returns the name of the partition and I need to run alter table Exchange partition
This is just a continuation of your other thread
For the name of the partition in the partition range table
As I said it you do not have to know the name of the partition to make an EXCHANGE. I gave you an example in this other thread.
ACCEPT user_yn PROMPT ' do you want the table to partition with
? (Y, N) : "
That makes no sense at all. You cannot partition a table that is already partitioned. If there is a name for the partition and then the table is partitioned already - then ask why to partition?
ALTER TABLE to_tehblnc
SWAP PARTITION
WITH TABLE T1
WITHOUT VALIDATION
UPDATE GLOBAL INDEXES;
I need to call once again for the sub_script.sql based on start_date and end_date, for example 1st January 2015 and January 5, 2015 I must first get the parition name and proceed to modify the table with that respective partition name returned by the function, so that it will be 5 times alter the table to swap partition
No - you don't have. Just use the code example I provided in your other thread. You don't need to know the name of the partition and use dynamic sql statements to make the Exchange.
Just use PARTITION syntax as shown in the example.
Do things the EASY way. Your code and the script is totally useless.
Threads and passing parameters to it
Hello everyone,
now I play with sons and I am wondering how to change the setting to the thread function.
I created a thread function is
void CVICALLBACK (char id [], char value []) doThread
{
do something
}
and I want to call the thread like this
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_HANDLE, doThread(FIX_ARG1,FIX_ARG2), NULL, & threadID);
But somehow, it does not work. How can I pass strings to the thread function?
Thank you all...
Common to pass data to a thread is to pick everything up in a structure, and then past the address of this structure to the function that installs the thread. Then tapped him, cast the parameter passed to a pointer of type structure, and you can then reference the data. As:
typedef struct {}
int executed;
Short flag;
char string [32];
} structThread, * pstructThread;
...
structThread tData;
...
tData.run = 1;
CmtScheduleThreadPoolFunction (..., & tData,...)
...
int MyThread (void * param) {}
pstructThread td = param (pstructThread);
so that {(td-> run)
...
}
return 0;
}
JR
Need help in shell script programming
I use the following script to check the status of the db.check_stat ='ps - ef | grep ${ORACLE_SID} | grep pmon | WC-l ';
oracle_num = 'expr $check_stat.
If [$oracle_num - lt 1]
then
Exit 0
FI
#*************************************************************
# Test to see if Oracle accepts connections
#*************************************************************
$ORACLE_HOME/bin/sqlplus - s "virtue sysdba" > /tmp/check_$ORACLE_SID.ora < < ENDSCRIPT
Select name from v$ database;
output
ENDSCRIPT
#*************************************************************
# If this is not the case, go out and e-mail...
#*************************************************************
check_stat ='cat /tmp/check_$ORACLE_SID.ora|grep-i error | WC-l ';
oracle_num = 'expr $check_stat.
If [$oracle_num - no 0]
then
mailx-s "$ORACLE_SID is down!" [email protected] < /tmp/check_$ORACLE_SID.ora
#else
#mailx s "$ORACLE_SID is running" [email protected] < /tmp/check_$ORACLE_SID.ora
exit 16
FI
When run at the command prompt, it works fine. I need to know what lines I need to add to this schedule a crontab?
Thanks in advance
See "man cron" cron documentation
Check what types of jobs already exist in the file crontab "crontab-l".
Your script is based on $ORACLE_SID and $ORACLE_HOME is defined, then you need to add lines to do this (by calling the existing scripts, or by calling oraenv)
Hello, I have two questions...
Many examples of security of the site, I noticed the use CFlock tag. Hwoever, when you look at the documentation of CF9, in a specific example of the security of the site that includes user roles (using an application.cfc file) there is no mention of the herds. They are required to authenticate users or not? I don't see why they would. The variable being edited is the status of users (authenticated or not), and since it is in the session scope, it is already unique, no? So why bother with CFLock?
Secondly, how can I redirect a person to the same page that they where after having submitted a form? I have a form letter of new present on all pages of the Web site, and I would like to than the person to return to the same page they where, the when they presented the form.
In pre - mx days, i.e. ColdFusion 4.5, ColdFusion was leaking bad memory with the memory shared as session variables. Popular workarounds should
The action of a form page is defined in the
call a script sql with one parameter of another
I would like to call a sql script that takes a single parameter (& 1)... but it doesn't.It looks like (first.sql):
begin
for cv in (select table_name from all_tables where owner='IBIS') loop
@my_script.sql cv.table_name;
end loop;
end;
but when I run it, I got an error:@first.sql;
old 9: spool &1..cob replace
new 9: spool cv.table_name.cob replace
old 10: select ' 05 DBUFF-' || '&1' || '-1.' from dual;
new 10: select ' 05 DBUFF-' || 'cv.table_name' || '-1.' from dual;
old 12: from all_tab_cols where table_name='&1' and substr(column_name,1,3)<>'SYS';
new 12: from all_tab_cols where table_name='cv.table_name' and substr(column_name,1,3)<>'SYS';
old 13: select ' 05 ' || '&1' || '-ROWID PIC X(18).' from dual;
new 13: select ' 05 ' || 'cv.table_name' || '-ROWID PIC X(18).' from dual;
What is missing?Kind regards...
Hello
The trick is to generate a sql script with all your calls to internal scripts and their parameters.
For example, my script is internal countTableIndex.sql :
select 'Table &1 has got '||count(*)||' indexes.' from user_indexes where table_name='&1';
And here's my script outside coutnIndex.sql :
set serverout on pages 0 lines 100 feed off time off verify off define on tab off trimspool on trimout on termout on termout off
spool cmd.sql
begin
for cur in (select table_name tab from user_tables) loop
dbms_output.put_line('@countTableIndexes.sql '||cur.tab);
null;
end loop;
end;
/
spool off
set termout on
@cmd.sql
exit;
As you can see, it generates a file cmd.sql containing internal script calls and ultimately calls the script cmd.sql.
In my example schema scott after the execution, the cmd.sql file contains:
@countTableIndexes.sql DEPT
@countTableIndexes.sql EMP
@countTableIndexes.sql BONUS
@countTableIndexes.sql SALGRADE
@countTableIndexes.sql TOTO
Here is the result of my machine:
[oracle@Nicosa-oel test19_parameterizedSqlScripts]$ sqlplus -s scott/tiger@my11g @countIndex.sql
Table DEPT has got 1 indexes.
Table EMP has got 1 indexes.
Table BONUS has got 0 indexes.
Table SALGRADE has got 0 indexes.
Table TOTO has got 0 indexes.
Call a script from a function sql file.
HelloI need to call a script file sql from a user-defined function. Currently I am doing this in Oracle SQL Developer. I tried to call with
@ {filename}, EXECUTION IMMEDIATE etc, but nothing worked. I get the compile error.
Basically, my need is to call catldap.sql file so that DBMS_LDAP package gets loaded and then I can call this API functions.
Please let me know if this is possible to do in a PL/SQL function.
Thank you
NGO
user784520 wrote:
I need to call a script file sql from a user-defined function.
Not possible... and it seems that you do not fully understand the client-server in the context of Oracle.
All SQL and PL/SQL are parsed and executed by an Oracle server process. Engines SQL and PL/SQL is expecting a single command at a time block. Any of these can accept a series of commands in a single call and then runs them in turn. The SQL engine expects a single SQL statement at a time. PL engine provides an anonymous PL/SQL block for both.
This server process can not also enter the local file system to access the script files. Nor can he hack it on the network to access the script on the client files.
So that the server process to access local files, a Directory object must be created and the current Oracle schema must read or a write access to this directory object. Sound security principles apply.
There is no PL/SQL command to run a script. Do not confuse SQL * more (this customer has a very limited vocabulary) orders with PL/SQL commands. SQL * Plus runs its own orders... and send SQL and PL/SQL commands (a statement block a time) either repaired and running Oracle server process.
It's also a very bad idea to run the external script content from within an Oracle server process - as this script resides outside and so outside the Oracle security mechanisms. This means that it is fairly easy for a person to access this script, compromise and then you inject and execute the contents of this script in the database.
It is not good security.
Latest issue - it's even worse to have the PL/SQL application dynamically create (or try to create) parts of the Oracle data dictionary and calling PL/SQL interface.
The database must be installed correctly - and that includes loading and execution of the required rdbms/admin scripts during installation. It makes no sense at all for the application code to try to run these scripts. It raises many questions, including getting to allow the full application and full SYS code access to the instance database. A very serious breach of security.
I agree at all with the approach to use.
Maybe you are looking for
-
I checked in my control panel for firefox, it says I have firefox version 3.6.8
-
Backup issues 'MyBookLive '.
I got this error message during backup time machine. "Unable to complete backup. An error occurred when creating the backup folder". I just had this problem since last January 4. Any help? I have IMAC 27 inch end 2014 model, OS El Capitan version 10
-
How do you unsubscribe/delete alerts for the 'Australian Holidays' calendar?
There is an 'Australian Holidays' calendar displayed on my iPhone calendar app. He won't let me not unsubscribe from him, I can't stop the dates on it send me reminders, notifications and I can't remove it. I deleted the calendar application on my de
-
Expanded American advanced time - windows XP DST has no
Given that the US federal Government extended advanced time saving a couple of years, the Windows XP system time is off by an hour for about 6 weeks each year. Not a big deal until I got an iPod and I use it instead of a watch, and if I sync it duri
-
HP Laserjet P1102: Two rows of black dots on every print
I recently removed the seals and tested a few impressions, but each of them contains two rows of black dots on the left side (a line more fat and other points more small rank). I did the cleaning print but no use. Here are some pictures of the questi