sql query to export data from different tables to file - sql

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.

Related

not able to export table into csv format

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

How import/export package body and spec into .sql or .pkb file

I need to import/export a package body/spec into a .sql or .pkb file using only sql plus commands.
I tried to execute the standard select but this only display it in the console, but I need the file to modify.
Also I need to do the opposite, that is to import a .sql/.pks/.pkb file to the database in order to apply changes.
If you want to do that using SQL*Plus, then - as you were told - spool seems to be a natural option. Here's an example, see whether it helps.
First, I'll create a simple package:
SQL> create or replace package pkg_test as
2 function f_today return date;
3 end;
4 /
Package created.
SQL> create or replace package body pkg_test as
2 function f_today return date is
3 begin
4 return sysdate;
5 end;
6 end;
7 /
Package body created.
SQL> select pkg_test.f_today from dual;
F_TODAY
-------------------
02.09.2019 22:28:56
SQL>
In order to create a nice output file:
there are some SQL*Plus settings that should be set
as you want to export a package, we'll query user_source
it doesn't contain create (or replace) so I'll select it separately
the same goes for the terminating slash /
All that will be stored in a .SQL file. If you run those commands directly, the export file will contain select statements as well and that's something you'd want to avoid.
Spool.sql file:
set heading off
set feedback off
set pagesize 0
set termout off
set trimout on
set trimspool on
set recsep off
set linesize 120
spool pkg_test.sql
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE'
order by line;
select '/' from dual;
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE BODY'
order by line;
select '/' from dual;
spool off;
Let's run it; because of all those SET commands, you won't actually see anything; the SQL> prompt will be all, as if nothing happened:
SQL> #spool
SQL>
But, if you check what's written in pkg_test.sql file, you'll see the package:
SQL> $type pkg_test.sql
create or replace
package pkg_test as
function f_today return date;
end;
/
create or replace
package body pkg_test as
function f_today return date is
begin
return sysdate;
end;
end;
/
SQL>
Looks OK, so - to answer your second question (how to import it back) - just run it. I'll exit SQL*Plus first; otherwise - again because of SET commands - you won't see anything:
SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
C:\Users\lf>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Pon Ruj 2 22:36:06 2019
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> #pkg_test
Package created.
Package body created.
SQL>
For exporting tables
exp <username>/<password> file=tables.dmp tables=(tab1,tab2)
For importing tables
imp <username>/<password> file=tables.dmp tables=(tab1,tab2)
For printing onscreen output in a file simply use
spool <filename> before all your commands to modify your sql outputs as report and spool off after sql commands as
Spool file.txt
SELECT * FROM TABLE
SPOOL OFF

Oracle SQL Developer: can bind variables begin with upper case letter?

I'm using Oracle SQL Developer to test a query to be used in an ADF application's read only view object. ADF documentation recommends using an uppercase letter to begin the name of a bind variable. So... I've creatively named mine :BindVariable
Funky part is SQL Developer appears to dislike bind variables that begin with an upper case letter.
This query works
select * from tablename
where id like :bindVariable
This one does not
select * from tablename
where id like :BindVariable
Am I correct in understanding that bind variable names cannot begin with an upper case letter? Or is there something else amiss here?
EDIT
Is this just an Oracle SQL Developer thing? :BindVariable works just fine in JDeveloper's database navigator.
Thanks for reading! Any input will be greatly appreciated.
Oracle SQL Developer: can bind variables begin with upper case letter?
Yes.
There is no issue with SQL Developer. I have tested it on version 3.2.20.10
Please see the screenshots:
Query:
Result:
No issues in SQL*Plus either:
SQL> variable BindVariable VARCHAR2(20)
SQL> EXEC :BindVariable := 'SMITH'
PL/SQL procedure successfully completed.
SQL> SELECT empno FROM emp WHERE ename LIKE :BindVariable;
EMPNO
----------
7369
SQL>

Getting the create table command used in oracle 11g

How can I get the create table command which is used to create a table in oracle 11g so that I can copy the command and run it in another database? Please help.
select dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME_GOES_HERE')
from dual;
Try to spool the output of the below query,
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_TABLES u;
Like,
set pagesize 0
set long 90000
set feedback off
set echo off
spool schema.sql
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_TABLES u WHERE TABLE_NAME = '<your_table>';
spool off;
Reference: http://www.dba-oracle.com/oracle_tips_dbms_metadata.htm
Please use the below Query
select dbms_metadata.get_ddl('TABLE','YOUR_TABLE_NAME','YOUR_SCHEMA_NAME') from dual;
if you use SQL Developer, select the table name , and right click to choose Open Declaration and then Click SQL tab on the window that opens!

Table names, and loop to describe

Working in Oracle 10g. Easy way to list all tables names (select table_name from dba_tables where owner = 'me')
But now that I have the table names, is there an easy way to loop through them and do a 'describe' on each one in sequence?
You could query against DBA_TAB_COLUMNS (or USER_TAB_COLUMNS).
Nicolas.
Not sure you can do a describe from within PL/SQL. I just tried using execute immediate 'describe some_table', that doesn't work either. Your next choice would be to query DBA_TAB_COLUMNS, or create a new file with all your describe statements (using dbms_output from pl/sql and spool to create the file) and then execute that file. Maybe like this:
spool temp_file.sql
BEGIN
/*or you could have a loop here with as many put_lines as you need, it will all end up in the new script file.*/
dbms_output.put_line('describe some_table');
END;
/
spool off
#temp_file.sql
/*I have not actually tried running this code, beware syntax errors!*/
You can do this in PL/SQL using DBMS_METADATA.GET_DDL, e.g. (example taken from docs):
SET LONG 2000000
SET PAGESIZE 0
SELECT DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') FROM DUAL;
login with the user and then run the following commands, first file will contain the describe commands and the second file will be the desired file containing all the descriptions of all tables for logged in user
spool desctables.sql
select 'describe '||table_name||';' from user_tables;
spool off
spool alltables.txt
#desctables.sql
spool off
I'd recommend querying dba_tab_columns, as N. Gasparotto suggested, but if you really want describe output then create a file mulit-describe.sql with the following:
set pagesize 0
set termout off
set feedback off
set verify off
spool mdtmp.sql
select 'describe ' || owner || '.' || table_name
from dba_tables
where OWNER = upper('&1')
/
spool off
set termout on
#mdtmp.sql
Within SQL*PLUS run by:
#multi-describe ME