Oracle SQL: Running insert statements from a large text file - oracle

I have a large text file (around 50mb). This text file has thousands of insert statements. I tried to open the text file in Oracle SQL Developer, but it is too large. How do I insert the data into my tables without opening the file in SQL Developer?
I tried to loop through the insert statements one by one and insert them into my table like this:
DECLARE
V1 VARCHAR2(32767);
fileVariable UTL_FILE.FILE_TYPE;
BEGIN
fileVariable := UTL_FILE.FOPEN('h:/Documents',
'clob_export.sql',
'R',
32760);
UTL_FILE.GET_LINE(fileVariable,V1,32767);
UTL_FILE.FCLOSE(fileVariable);
END;
But this doesn't seem to work. I can't create directories on the machine, and anyways, the text file is on the computer where I am running SQL Developer and SQL Developer is connected remotely to the database.

The simplest way - from my point of view - is to run it from SQL*Plus, such as:
c:\Temp>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Uto Sij 26 22:20:18 2021
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> #insert_data.sql
1 row created.
1 row created.
<snip>
presuming that insert_data.sql contains something like
insert into dept values (1, 'Dept 1', 'NY');
insert into dept values (2, 'Dept 2', 'London');
...

Use sqlplus and if where are too much text use options to log only in the file not on screen
SET TERMOUT OFF;
spool M:\Documents\test.log;

Call the file with # instead of trying to open the file. You may also want to disable feedback to avoid many thousands of "1 row inserted" messages.
set feedback off;
#c:\users\jon\Desktop\test.sql
The above commands are SQL*Plus syntax, but Oracle SQL Developer worksheets understand basic SQL*Plus commands. If you need to frequently run large scripts then you might want to learn the command line SQL*Plus, but if this is just a one-time task then stick with SQL Developer.

Related

Oracle SQL Loader control file to ignore ellipsis

I have an Oracle SQL Loader control file based on position in a text file. One particular field periodically gets an ellipsis '...' from the source, which causes a carriage return in the loading table. No matter how many times I request '...' to NOT be used by these users, there is eventually someone who forgets, or due to staff turnover, etc. Here is the current control file line for that field:
TRAN_DESC POSITION(153 : 202) Char,
Is there any command that can be added to this line in order to ignore special characters such as an ellipsis?
I'd think of REPLACE. Here's an example.
Sample table:
SQL> create table test (id number, tran_desc varchar2(10));
Table created.
Control file:
load data
infile *
into table test
(id position(1:2),
tran_desc position(3:12) char "replace(:tran_desc, '...', '')"
)
begindata
10LittleFoot
11Big...foot
Loading session and result:
SQL> $sqlldr scott/tiger control=test2.ctl log=test2.log
SQL*Loader: Release 11.2.0.2.0 - Production on Pon Tra 5 17:03:39 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
SQL> select * from test;
ID TRAN_DESC
---------- ----------
10 LittleFoot
11 Bigfoot
SQL>

How to read data from text file with comma separated values and insert into temp table using in stored procedure

FIle name emp.txt - the text file contains data like this:
emp_no,emp_EXPIRY_DATE,STATUS
a123456,2020-07-12,A
a123457,2020-07-12,A
I want to insert data into a temp table using a stored procedure.
Which database do you use? "Oracle" SQL Developer looks like "Oracle" (of course), but - code you posted as a comment isn't Oracle.
Anyway, if it was, then doing what you plan to do would require UTL_FILE package. CSV file should be put into a directory (usually on the database server) which is a source for directory (as an Oracle object); user that is supposed to load data should have read (and write?) privileges on it.
Alternatively, you could use the CSV file as an external table. That option might be simpler as it allows you to write ordinary SELECT statements against it, i.e. read data from it and insert into the target table that resides in an Oracle database. This option also requires the "directory" stuff.
Or, if you want to do that locally, consider using SQL*Loader; create a control file and load data. This option might be extremely fast, way faster than previous options. You won't see any difference for small files, but - for a lot of data - this might be your choice.
A SQL*Loader example:
Test table:
SQL> create table test
2 (emp_no varchar2(10),
3 emp_expiry_date date,
4 status varchar2(1));
Table created.
Control file:
options (skip=1)
LOAD DATA
infile emp.txt
replace
INTO TABLE test
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
emp_no,
emp_expiry_date "to_date(:emp_expiry_date, 'yyyy-dd-mm')",
status
)
Loading session & the result:
SQL> alter session set nls_date_Format = 'yyyy-mm-dd';
Session altered.
SQL> $sqlldr scott/tiger control=test13.ctl log=test13.log
SQL*Loader: Release 11.2.0.2.0 - Production on Sri Pro 11 21:02:44 2019
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 1
Commit point reached - logical record count 2
SQL> select * from test;
EMP_NO EMP_EXPIRY S
---------- ---------- -
a123456 2020-12-07 A
a123457 2020-12-07 A
SQL>

Inserting vietnamese characterset into nvarchar2/varchar without Prefix 'N' from client

I am having a database(Oracle 11g) on windows whose NLS_CHARACTERSET value is WE8MSWIN1252, while the NLS_NCHAR_CHARACTERSET value is AL16UTF16.
Now, I have a table 'TEST_NOTE' whose column type is NVARCHAR2. While running the following insert statement:
insert into test_note values (n'Chào thế giới!')
The data gets inserted fine and I am able to fetch it properly.
Since I have to insert the data from a different client software(company's proprietary software), I am not able to append 'n' to the value the user enters.
Also, can I make do with VARCHAR2 instead of NVARCHAR, as I don't want to change the existing schema of the database in production?
My ideal solution will be using VARCHAR2 and inserting Vietnamese Characters without using 'n' as prefix.
EDIT:
I Tried the following on Windows 10:
C:\WINDOWS\system32>chcp
Active code page: 437
C:\WINDOWS\system32>set NLS_LANG =.AL32UTF8
C:\WINDOWS\system32>sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Wed Feb 22 11:15:11 2017
Copyright (c) 1982, 2010, Oracle. All rights reserved.
SQL> connect system as sysdba
Enter password:
Connected.
SQL> insert into ss_repo.test_note values ('abcs','Chào thế giới!');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from SS_REPO.TEST_NOTE;
SOEID NOTE
-------------------- --------------------
ID17836 Chào th? gi?i!
s Chào th? gi?i!
abcs Chào th? gi?i!
ABCD Chαo th┐ gi┐i!
Or Can I do the same from SQL Developer? Will it be easy using that?
The client which will be used in production will be using JDBC JAR file OJDBC6.JAR
But for the time being I am trying to do using SQL Plus or SQL Developer.
Codepage 437 does not support any Vietnamese characters.
When you set set NLS_LANG =.AL32UTF8 then you have to execute chcp 65001 beforehand in order to change your codepage (and thus also character set of SQL*Plus) to UTF-8
However, using UTF-8 on Windows command line has some issues, see this discussion: https://community.oracle.com/thread/600575
You can also try Codepage 1258 which should work for Vietnamese:
C:\>chcp 1258
Active code page: 1258
C:\>set NLS_LANG =.VN8MSWIN1258
C:\>sqlplus ...

'Invalid input values for pname' during table statistics gathering?

When I'm trying to gather table statistics using GATHER_TABLE_STATS procedure, I'm getting the following error:
ORA-20001: Invalid input values for pname
ORA-06512: at "SYS.DBMS_STATS", line 31513
ORA-06512: at line 2
The code I'm running to gather statistics is
BEGIN
DBMS_STATS.gather_table_stats ('OWNER', 'TABLE_NAME');
END;
/
My Oracle version is Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
I guess you might have misspelled OWNER or TABLE_NAME parameter. Its working fine for me as shown below.
SQL> conn jay/jay
Connected.
SQL> select table_name from tabs;
TABLE_NAME
------------------------------
ROAD
EVENT
ALL_OBS
ACCOUNT
NVARCHAR2_EMAIL
TABLEA
T2
MYTABLE
8 rows selected.
SQL> exec dbms_stats.gather_table_stats('JAY','ROAD');
PL/SQL procedure successfully completed.
Update
As per the My Oracle Support Doc:755577.1, It is possible that the post installation scripts for the patch were not run correctly after a patch was applied.
You may need to reinitialize the DBMS_STATS package using execstat.sql under $ORACLE_HOME/rdbms/admin directory. Or reinstall DBMS_STATS.
Or you might hit the bug- Bug 14479079 : ORA-20001 GATHERING STATS AFTER CPU JULY 2012 PATCH

Running Oracle stored Procedure

I have a sql file that contains a simple procedure to print "Hi" like,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
When I try to execute this from sql file itself, it just gets compiled and it is not running.
I added the as,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/ /* this is required else it throws me compilation error*/
set serveroutput on
EXECUTE skeleton;
I tried calling the same skeleton from other sql file also and even from the sqldeveloper GUI, that also didnt execute this. Only the sqlplus commandline helps me. Let me know what I am missing and the solution for this.
Here are the steps I took using SQL Plus
SQL> CREATE OR REPLACE PROCEDURE skeleton
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE('Hi');
5 END;
6 /
Procedure created.
SQL> set serveroutput on
SQL> EXECUTE skeleton;
Hi
PL/SQL procedure successfully completed.
SQL>
Can you start a new sqlplus session replicate these steps and post the content?
The only change I had to make to your sql to allow running it as an #file was to remove the comment. This is the whole content of the the .sql file:
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/
set serveroutput on
EXECUTE skeleton;
You should get an output something like this:
C:\Temp>sqlplus username/password #skeleton.sql
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Oct 5 17:10:46 2009
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Procedure created.
Hi
PL/SQL procedure successfully completed.
SQL>
Try setting set serveroutput on before the dbms_output.
In SQL Developer, you need the "execute script" button which is the second button at the top left (little green arrow in front of document) instead of the one with the big green arrow.
That threw me the first time I used it.
(source: devshed.com)
The reason your first example gets compiled and not run, is that you are not actually asking it to run. You are creating a procedure inside the database, which works correctly, but not calling it. In your second block you have an EXECUTE which is where it actually runs.

Resources