How to execute .sql file using another .sql file in Oracle? - oracle

I try to start cr_tb.sql file using another start.sql file and get an error unknown command beginning pid number... The strange thing is that when I simply copy paste the cr_tb.sql content into the SQL*Plus, it executes perfectly.
What am I doing wrong? (I have posted the dropbox links)

The root of the problem lays in the create table frclubs statement. There are blank lines in
the table definition:
create table frclubs
(
-- here they are
pid number(2) not null,
clubid number(2) not null,
constraint cPIDCLUBIDPK primary key(pid,clubid),
constraint fPIDFK foreign key(pid) references friends(pid),
constraint fCLUBIDFK foreign key(clubid) references clubs(clubid)
);
You have two choices:
Remove blank lines in the create table frclubs DDL statement;
Allow SQL*PLUS ignore blank lines in the script issuing SET SQLBLANKLINES ON command.

Related

use UTL_TCP.connection in pl sql

i have to read a file .csv that is in a directory of my virtual box oracle. In my local system windows with sql developer i want to create a procedure that read that file in the virtual box. I don't undestand how use UTL_TCP.connection and how write that procedure
Consider using External tables.
Note that prior to creating external table You must have database directory created. The location specified is then used to read files from. You can specify separators, delimiters and filenames in the DDL statement withing creation of table. Once set-up it could be queries by using standard select statement.
For example:
create directory << db directory >> as '/u01';
create table ext_filename_csv (
column01 varchar2(4000),
column02 varchar2(4000),
column03 varchar2(4000)
)
organization external (
type oracle_loader
default directory << db directory >>
access parameters (
records delimited by newline
fields terminated by ",")
location ( << db directory >>:'filename.txt')
);
Read more here:
http://psoug.org/reference/externaltab.html

Creating a text file from PL/SQL Developer

Can someone explain how to create a text file and save to my C drive from a select query? Anytime I run the query I would like the query to replace the old text file with the new query data.
You will need to follow the steps :
save this in file named get_data.sql
SET HEADING OFF
SET FEEDBACK OFF
SET TERM OFF
SET LONG 100000
SPOOL Dept_100.txt
select 'EmployeeName : '||Emp_id from Employee where Deptno = 100;
SPOOL OFF
on CMD, Go to the folder where the script is.
connect using sqlplus through command prompt.
Run the script
SQL>#spool.sql
spool.sql contains select statement which will select employeename of all employees who work for Dept :100(Just for example).
running spool.sql will create a file "Dept_100.txt" containing employeenames.

Oracle error:- LRM-00116: syntax error at 'control' following '='

Oracle DB/Windows XP:-
I am running an batch file that calls an “.ctl” file which in turn calls an “.xls” file, both present in the same folder.
The idea is to load the data onto Oracle db present on an remote oracle server.(non local machine)
I am getting this error, no matter what I do.
Oracle error:- LRM-00116: syntax error at 'control' following '='
The .bat file code is as below
rem SET SQLLOGIN=remod/P3w1d0ry#wsd
pause Ready to Load the remo.Temp_data Table
sqlldr userid=%SQLLOGIN% control=TempData.ctl errors=100
pause
The .ctl file is as follows:-
LOAD DATA
INFILE "data.xls"
replace
into table remo.Temp_data
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
test_abbr "rtrim(:test_abbr)",
test_pk "rtrim(:test_pk)",
test_sk "rtrim(:test_sk)",
test_dt "rtrim(:test_dt)",
test_email "rtrim(:test_email)",
)
You've remarked out the the SET of SQLLOGIN. Also you might want to put a call in front of the sqlldr statement. You'll also need some data to load...
SET SQLLOGIN=remod/P3w1d0ry#wsd
pause Ready to Load the remo.Temp_data Table
call sqlldr userid=%SQLLOGIN% control=TempData.ctl data=mydata.csv errors=100

Use parameters with CTL

I am using a CTL file to load data stored in a file to a specific table in my Oracle database.
Currently, I launch the loader file using the following command line:
sqlldr user/pwd#db data=my_data_file control=my_loader.ctl
I would like to know if it is possible to use specify parameters to be retrieved in the CTL file.
Also, is it possible to retrieve the name of the data file used by the CTL to fill the table ?I also would like to insert it for each row. I currently have to call a procedure to update previously inserted records.
Any help would be appreciated !
As I know don't have any way to pass parametter as variable in ctrl. But You can use constant in ctl and modify clt file to change that constant value (in ctl file content) for every loading times.
Edit: more specific.
my_loader.ctl:
--options
load data
infile 'c:\$datfilename$' --this is optional, you can specify here or from command line
into table mytable
fields....
(
datafilename constant '$datfilename$', -- will be replace by real datafname each load
datacol1 char(1),
....
)
dataload.bat: assume that $datfilename$ is the text will be replace by datafile's name.
::sample copy
copy my_loader.ctl my_loader_temp.ctl
::replace the name of datafile (mainly the content to load into table's data column)
findandreplace my_loader_temp.ctl "$datafilename$" "%1"
::load
sqlldr user/pwd#db data=%1 control=my_loader_temp.ctl
::or with data be obmitted if you specified by infile in control file.
sqlldr user/pwd#db control=my_loader_temp.ctl
using: dataload.bat mydatafile_2010_10_10.txt

How to append system time to the file name in oracle?

I am very new to oracle.I need to create a file with the system timestamp from oracle. Please let me know how do i do that.
More over I need to write any exceptions or errors thrown by my pl/sql code to a file and exit after an error. How do i do this?
Thanks,
Priya.R
It seems you want to look into the UTL_FILE Oracle supplied package, as you're trying to use PL/SQL to create the file.
You can generate the file this way:
(You'll need to create an Oracle DIRECTORY first, pointing to the OS location of the file:
CREATE OR REPLACE DIRECTORY DIR AS 'your OS directory';
Note that the name 'DIR' is used in the sample code that follows. You will also require the CREATE DIRECTORY privilege, and then grant read and write permissions on the directory to the user who will use it:
GRANT READ,WRITE ON DIR TO user1;
)
DECLARE
v_logfile VARCHAR2(100);
v_FH UTL_FILE.FILE_TYPE;
BEGIN
v_logfile := TO_CHAR(SYSDATE,'YYYYMMDD HH24MISS')||'_process.log';
v_FH := UTL_FILE.FOPEN(DIR, v_logfile, 'w');
UTL_FILE.PUTLINE(v_FH, 'Some text on a new line');
UTL_FILE.FCLOSE(v_FH);
END;
This is how you can get a dynamic filename in SQL Plus
SET TERMOUT OFF
DEFINE dynamic_filename = idle
COLUMN which_dynamic NEW_VALUE dynamic_filename
SELECT 'prefix_'
||TO_CHAR( SYSDATE, 'YYYYMMDD' )
||'_'
||TO_CHAR( SYSDATE, 'HH24MISS' )
||'.log' which_dynamic
FROM dual;
SET TERMOUT ON
SPOOL &dynamic_filename
SELECT * FROM dual;
SPOOL OFF
The file gets created in the default directory for SQL Plus (on windows this is the "Start In:" property of the shortcut)
To place the output in a known directory amend the SPOOL command to something like...
SPOOL c:\output_dir\&dynamic_filename
To get an SQL Plus script to exit after an error then include this command...
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK

Resources