I have a sql file update_qde.sql
which contains the following code
UPDATE profile_const
set parameter_value = '04_2015'
where profile_name = 'MIDAS_MTH_ALL_Panels'
and parameter_name = 'PERIODS_TO';
commit;
quit;
I have another batch file test.bat
I want to pass the parameter_value as a variable from a batch file using batch command.
Could you please show me any way to do this?
Use SQLCMD variables, so change test.bat to call SQLCMD rather than ISQL
#echo off
sqlcmd -E -S myserver -i update_qde.sql -v PARM1=2015-01-01
Change the SQL to use the $() notation
UPDATE profile_const
SET parameter_value = '$(PARM1)'
WHERE profile_name = 'MIDAS_MTH_ALL_Panels'
AND parameter_name = 'PERIODS_TO';
COMMIT;
Its pretty simple.
Create a batch file with the below 3 line of code. To create a batch file, create a new .txt file, save the below code inside the file and rename the extension of this file from .txt to .bat.
In the 2nd line below code value1 is the variable name which holds the value you need to update. Third line of code is where you will pass this variable value to the sql file.
#echo off
SET value1=04_2015
sqlplus db_username/db_password#db_schema #sample.sql %value1%
The code for sample.sql is given below. The &&1 in the below code accepts the parameter that you are sending from the batch file and will be stored in the variable param1. Later you can use this param1 variable inside the BEGIN block to be used for UPDATE query.
SET SERVEROUTPUT ON
SET DEFINE ON
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 200
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET MARKUP HTML OFF SPOOL OFF
SET term off
SET verify off
set echo off
SPOOL "C:\Test\Logfile.log" append;
var param1 VARCHAR2(20);
BEGIN
:param1 := '&&1';
END;
/
SET DEFINE OFF
BEGIN
UPDATE profile_const
set parameter_value = :param1
where profile_name = 'MIDAS_MTH_ALL_Panels'
and parameter_name = 'PERIODS_TO';
commit;
END;
/
quit;
/
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
I verified the above code, which works fine.
Related
getting an error on trying to pass a variable as a schema name for an sql file and I need a way to get this done.
I have noticed I do not get any error on passing a variable in a where clause of an sql statement.
I have tried with the below ways:
1)
Shell script 1:
#!/bin/bash
b=`sqlplus -silent $dbconnect #abc.sql prd01`
SQL script:
Set Heading off
Set term off
Spool xyz.csv
Select * from pkk$'&1'_02.tab1
Where
Col = 'S';
ERROR-
ORA-00933: SQL command not properly ended
Shell script 1:
#!/bin/bash
b=`sqlplus -silent $dbconnect #abc.sql prd01`
SQL script:
Set Heading off
Set term off
Spool xyz.csv
Select * from pkk$"&1"_02.tab1
Where
Col = 'S';
ERROR-
ORA-00933: SQL command not properly ended
Shell script 1:
#!/bin/bash
b=`sqlplus -silent $dbconnect #abc.sql prd01`
SQL script:
Set Heading off
Set term off
Spool xyz.csv
Select * from pkk$&1_02.tab1
Where
Col = 'S';
No error:
but waiting prompt to pass a variable
No error on passing the variable in the where clause
Shell script 1:
#!/bin/bash
b=`sqlplus -silent $dbconnect #abc.sql prd01`
SQL script:
Set Heading off
Set term off
Spool xyz.csv
Select * from pkk$prd01_02.tab1
Where
Col = '&1';
No error
In sqlplus, you need to use a '.' if the variable name is followed by something, in your case write your sql like this:
Select * from pkk$&1._02.tab1
Where
Col = 'S';
And, if you tried your script by logging in sqlplus and running it interactively, you would have seen your error.
I created a script to export a table from my db into a .csv file
#!/usr/bin/bash
FILE="example.csv"
sqlplus -s abcd/abcd#XE <<EOF
SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF
SPOOL $FILE
SELECT * FROM myTable;
SPOOL OFF
EXIT
and now I'd like to modify this script and export my table into another. How can I change my code?
By "exporting your table into another", do you mean copying data from one table to another? If you don't need indexes or keys or other features on your new table initially, i.e. if it's not for production use, it's quite simple:
#!/usr/bin/bash
TABLE="myOtherTable"
sqlplus -s abcd/abcd#XE <<EOF
CREATE TABLE $TABLE as SELECT * FROM myTable;
EXIT
You could also do a create table statement first, specifying columns, keys and storage options as any other table, and then have a separate line that does INSERT INTO $TABLE (SELECT * FROM myTable) to fill it with data copied from myTable.
I need to export data from my Oracle table to a csv file.
Below is the code that I am running under SQL Developer :
set termout off
set serveroutput off
set feedback off
set colsep ';'
set lines 100000
set pagesize 0
set echo off
set feedback off
spool D:\myfile.csv
select *
from Employee;
spool off
However the output of the above code in the csv file is :
select *
from Employee;
I want the data of the Employee table to be in the csv, not the sql statement.
Any idea what might be wrong in the above code? Thanks.
save your sql in a file emp.sql in a directory D:\scripts and use like below;
set term off
set feed off
spool D:\myfile.csv
#D:\scripts\emp.sql
spool off
You're in SQL Developer, so you don't have to write so much code.
SET SQLFORMAT csv
SPOOL C:\your_file.csv
select * from whatever;
spool off
Run with F5
My code is as below-
set define on;
set spool on;
set SQLFORMAT csv;
set fpath = &folderpath;
spool #fpath/mycsvfile1.csv
select * from I_PTY;
spool off;
spool #fpath/mycsvfile2.csv
select * from I_LEG;
spool off;
I want to pass folderpath value as 'C:\Documents\Spool'.
And use this value to pass to the variable fpath, so that the value passed in line5 equals to 'C:\Documents\Spool\mycsvfile1.csv'
However, I am getting error as: Unknown set option "fpath".
Where am I getting wrong? Kindly assist.
You could do this in two ways:
Just use &&folderpath in the spool commands directly, e.g.:
set spool on;
set sqlformat csv;
spool &&folderpath/mycsvfile1.csv
select * from i_pty;
spool off;
spool &&folderpath/mycsvfile2.csv
select * from i_leg;
spool off;
Or use DEFINE to assign the value (which you'd then have to reference in the same way as the previous answer, so you don't gain anything, really...):
set spool on;
set sqlformat csv;
define fpath = &folderpath
spool &&fpath/mycsvfile1.csv
select * from i_pty;
spool off;
spool &&fpath/mycsvfile2.csv
select * from i_leg;
spool off;
Using the double ampersand stops Oracle from prompting you for a value each time you reference the same substitution variable. If you want your next script or next run through of this script to be prompted again for the value of folderpath, then you will need to include undefine folderpath (and maybe undefine fpath) at the end of the script.
Also, you may want to include set verify off at the top of your script.
N.B. you were using # in your spool file name - that has no place there. It's only used when you're running a script.
that "set" should probably be a "define"
Try:
spool &folderpath./mycsvfile2.csv
Example from my code:
spool c:\temp\SQL_Details_Report_&FILE_NAME..html
I have to run several queries on an oracle 11g database, within SQLDeveloper 3.1.
For example:
select * from product;
select * from customer;
select * from prices;
At the moment I am exporting the resultsets "per hand", I simply right-clickonto the result and thenexport` it.
I would like to automatically save the resultset of each query in a specific folder.
Any recommendation how I could do that?
UPDATE
I tried using the csv and als the txt extesion of testFile:
spool C:\Users\User\Desktop\testFile.csv --I tried also .txt extension here!!!
set colsep ';'
select * from product;
spool off;
However, when I open the file I get for csv and txt the following result:
> set colsep '
> select * from product
I appreciate your replies!
set echo off
set feedback off
set linesize 1000
set pagesize 0
set sqlprompt ''
set trimspool on
spool output.csv
select columnA || ',' || columnB || ',' || ......
from table
where ...
spool off;
exit 0;
Then create a shell script that calls the sql file
sqlplus >/dev/null 2>&1 "user/pass#DATABASE" << EOF
whenever sqlerror exit 1
#file.sql
EOF
UPDATE just saw you are on windows, same principle still applies, you probably will need to use PowerShell
You can use Spool, http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12043.htm
spool OutFile.txt
select row1||','||row2... from product; --format you prefer
spool off;