not able to export table into csv format - sql

I am trying to export table into csv format as below:
SQL> desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
DN NUMBER(10)
DISCONNECT_DATE DATE
SQL> select DN ,DISCONNECT_DATE from test into OUTFILE '/tmp/data.csv';
select DN ,DISCONNECT_DATE from test into OUTFILE '/tmp/data.csv'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
could you please anyone help me to resolved above problem.

I got the answer if we are using sql plus then we need use spool to get data into csv format. Below is the steps ...(don't forget to spool off after execution of query. )
SQL> set colsep ,
SQL> set headsep off
SQL> set pagesize 0
SQL> set trimspool on
SQL> spool /tmp/data.csv
SQL> select * from test;
-----------------(we cant placed the data)
10 rows selected.
SQL> spool off

Related

Oracle SQL, spool to a dynamic file name

I am trying to spool to a csv that contains the current_date in its name:
spool '\mydir\'||to_char(current_date,'YYYYMMDD')||'.csv';
SELECT /*csv*/* FROM mydata;
spool off;
However, I get an error:
SP2-0768: Illegal SPOOL command
Usage: SPOOL { <file> | OFF | OUT }
where <file> is file_name[.ext] [CRE[ATE]|REP[LACE]|APP[END]]
Is it not allowed? Is there a workaround?
The same error appears if I try using a substitution variable, which does not seem to be allowed either.
Here's an example of how I do it [you may need to fiddle around a bit (e.g. set heading off) to get it exactly how you want it]:
set echo off
set feedback off
set term off verify off head on
set linesize 200 pagesize 9999
set long 4000000
set serveroutput on
-- get instance name in file name
column DATE_YYYYMMMDDD new_val FILE_NAME noprint
Select to_char(sysdate, 'YYYYMMDD') DATE_YYYYMMMDDD from dual;
spool \temp\CSV_&FILE_NAME..CSV
select table_name || ',' || column_name from user_tab_columns;
spool off
set serverout off
col myspoolbase noprint new_value val_myspoolbase
select 'myfilename' myspoolbase from dual;
col log_date noprint new_value val_log_date
select to_char(sysdate,'yyyymmdd_hh24miss') log_date from sys.dual;
spool &&val_myspoolbase._&&val_log_date..log
select sysdate from dual;
spool off

sql query to export data from different tables to file

Currently, am trying to export data from different databases(oracle, sqlserver, MySQL...etc) to file using sql statment. can some one help me to do so, just like below:
SELECT order_id,product_name FROM orders INTO OUTFILE 'orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
But my query doesn't work,with the following error
ORA-00933: SQL command not properly ended
As of Oracle, a simple option is to spool the result of a query into a file (which is something you tried to do in your example). It is done from SQL*Plus, a command line tool. You're supposed to learn about SET commands which allow you to make a pretty output.
Here's an example of such commands:
SQL> set termout off
SQL> set trimspool on
SQL> set echo off
SQL> set verify off
SQL> set autoprint off
SQL> set serveroutput off
SQL> set arraysize 1000
SQL> set pagesize 0
SQL> set linesize 100
SQL> set long 10000
SQL> set numwidth 10
SQL> set feedback off
SQL> set colsep ';'
SQL> col empno format 99999
SQL> col ename format a10
SQL> col sal format 999G990
SQL> spool emps.txt
SQL> select empno, ename, sal from emp;
7369;SMITH ; 800
7499;ALLEN ; 1.600
7521;WARD ; 1.250
7566;JONES ; 2.975
7654;MARTIN ; 1.250
7698;BLAKE ; 2.850
7782;CLARK ; 2.450
7839;KING ; 5.000
7844;TURNER ; 1.500
7900;JAMES ; 950
7902;FORD ; 3.000
7934;MILLER ; 1.300
7788;SCOTT ; 3.000
7876;ADAMS ; 1.100
SQL> spool off
SQL>
By the way, lines 2-4 you wrote look like SQL*Loader's control file which is used to load data (not to unload it).
Ultimately, this depends on which database you're using.
The easiest way to do this would be using a database manager. For example, I know that phpMyAdmin has an export function and is easy to do that with (it's very good, I'll link a post which can help you with this). MySQL Workbench has one as well.
You will need different syntax for each different database. I'll try to provide answers for a few of the possibilities:
MySQL:
You would use the command line, navigate to a folder where you want the output file to be and then enter the command:
mysqldump --add-drop-table -u admin -p`cat /etc/psa/.psa.shadow` dbname > dbname.sql
Note that this will export the entire DB which might not be what you want. See a nice guide for this here. If you'd like something a bit more field selective (using SELECT), give this a shot:
SELECT order_id,product_name INTO outfile 'full_path/orders.csv' fields terminated BY ',' from orders;
Oracle:
You should be able to use the following command:
exp username/password PARAMETER=(value1,value2,...,valuen)
This is a more complicated one, I'd recommend checking out their official documentation for EXPORTING here.
MS SQL:
This is probably the best one. Your best bet is to go with server management studio as it's extremely simple there, as I don't recall and could not find any easy commands for doing this. You might be able to do something with the bcp command (in cmd).
The Truth:
Use a server management studio, this would by far be your best bet. If for some reason, you need to do it by command prompt or by script, it's possible but not really meant for that. Check this out for general help as well.

How to echo text during SQL script execution in SQLPLUS

I have a batch file which runs a SQL script in sqlplus and sends the output to a log file:
sqlplus user/pw < RowCount.sql > RowCount.log
My log file contains this:
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production
SQL> SQL>
COUNT(*)
----------
0
SQL>
COUNT(*)
----------
0
etc. but it's several thousand lines of output and therefore hard to determine which results belong to which statement.
I would like to add some formatting to the output, so that I may discern what happened. Either an echo of the executed statement or manually inserting some "echo" statements into the script would be fine. Ideally it would look something like this:
SQL> select(*) from TableA;
COUNT(*)
----------
0
SQL> select(*) from TableB;
COUNT(*)
----------
0
The prompt command will echo text to the output:
prompt A useful comment.
select(*) from TableA;
Will be displayed as:
SQL> A useful comment.
SQL>
COUNT(*)
----------
0
You can use SET ECHO ON in the beginning of your script to achieve that, however, you have to specify your script using # instead of < (also had to add EXIT at the end):
test.sql
SET ECHO ON
SELECT COUNT(1) FROM dual;
SELECT COUNT(1) FROM (SELECT 1 FROM dual UNION SELECT 2 FROM dual);
EXIT
terminal
sqlplus hr/oracle#orcl #/tmp/test.sql > /tmp/test.log
test.log
SQL>
SQL> SELECT COUNT(1) FROM dual;
COUNT(1)
----------
1
SQL>
SQL> SELECT COUNT(1) FROM (SELECT 1 FROM dual UNION SELECT 2 FROM dual);
COUNT(1)
----------
2
SQL>
SQL> EXIT
You can change the name of the column, therefore instead of "COUNT(*)" you would have something meaningful. You will have to update your "RowCount.sql" script for that.
For example:
SQL> select count(*) as RecordCountFromTableOne from TableOne;
Will be displayed as:
RecordCountFromTableOne
-----------------------
0
If you want to have space in the title, you need to enclose it in double quotes
SQL> select count(*) as "Record Count From Table One" from TableOne;
Will be displayed as:
Record Count From Table One
---------------------------
0

string comparing query with chinese chars - Oracle Database

I'm having trouble executing a simple query such as the following
select * from table_name where variabe_name like '在职'
the problem is the chinese chars. Those chars are in the table (I just copied them after doing a select * from table, so the displaying of the chinese chars works just fine) but it doesn't seem to work. When it execute the query, it returns 0 rows.
I' ve also tried
select * from table_name where variabe_name like '%在职%'
and
select * from table_name where variabe_name = '在职'
But that doesn't work either.
Any clue of what the problem might be?
Thnaks a lot
==> Found solution: put 'N' before the chinese characters, so that they are interpreted as Unicode. Like: where field like N'罐'
SQL> create table mytbl (data_col varchar2(200));
Table created
SQL> insert into mytbl values('在职');
1 row inserted.
SQL> commit;
Commit complete.
SQL> select * from mytbl where data_col like '%在职%';
DATA_COL
-----------
在职
SQL> SELECT * FROM nls_database_parameters where parameter='NLS_CHARACTERSET';
PARAMETER VALUE
------------------------------ ----------------------------------------
NLS_CHARACTERSET AL32UTF8
Your NLS_CHARACTERSET should be set to AL32UTF8. So try
SQL> ALTER SESSION SET NLS_CHARACTERSET = 'AL32UTF8';
Also make sure that parameter NLS_NCHAR_CHARACTERSET is set to UTF8.
SQL> ALTER SESSION SET NLS_NCHAR_CHARACTERSET = 'UTF8';

SQL query inside a SQL Script

I have a SQL script, which spools data to a file.
Sample Existing SQL script:
whenever sqlerror exit failure rollback
spool test.txt
set serveroutput on
select * from emp;
spool off
/
But, I would like to write a SQL query in this script before spooling data.
I don't want to hardcode the name of the spooling file, so how could I get the file name from a table or lookup?
I want the code to be something like
var filename varchar2(30);
select fname into :filename from table where script = 'abcscript';
spool :filename
set serveroutput on
select * from emp;
spool off
/
Thanks.
COLUMN spool_file_name NEW_VALUE.spool_file_name NOPRINT
select fname spool_file_name
from table where script = 'abcscript';
SPOOL &spool_file_name
SET ECHO ON
select * from emp ;
SPOOL OFF
COLUMN spool_file_name CLEAR