Access Oracle setenv parameters within sqlplus - sql

I'd like to write an SQL script to create a database. I'd like to parametrise it to be able to reuse it for future databases. As a base I'd like to use a script from Oracle documentation page:
CREATE DATABASE mynewdb
USER SYS IDENTIFIED BY sys_password
USER SYSTEM IDENTIFIED BY system_password
LOGFILE GROUP 1 ('/u01/app/oracle/oradata/mynewdb/redo01.log') SIZE 100M,
GROUP 2 ('/u01/app/oracle/oradata/mynewdb/redo02.log') SIZE 100M,
GROUP 3 ('/u01/app/oracle/oradata/mynewdb/redo03.log') SIZE 100M
MAXLOGFILES 5
MAXLOGMEMBERS 5
MAXLOGHISTORY 1
MAXDATAFILES 100
CHARACTER SET US7ASCII
NATIONAL CHARACTER SET AL16UTF16
EXTENT MANAGEMENT LOCAL
DATAFILE '/u01/app/oracle/oradata/mynewdb/system01.dbf' SIZE 325M REUSE
SYSAUX DATAFILE '/u01/app/oracle/oradata/mynewdb/sysaux01.dbf' SIZE 325M REUSE
DEFAULT TABLESPACE users
DATAFILE '/u01/app/oracle/oradata/mynewdb/users01.dbf'
SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
DEFAULT TEMPORARY TABLESPACE tempts1
TEMPFILE '/u01/app/oracle/oradata/mynewdb/temp01.dbf'
SIZE 20M REUSE
UNDO TABLESPACE undotbs
DATAFILE '/u01/app/oracle/oradata/mynewdb/undotbs01.dbf'
SIZE 200M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
To run Oracle-sqlplus one has to set some system variables, like ORACLE_SID.
How can I access this ORACLE_SID from within the script? Eg. I'd like to replace CREATE DATABASE mynewdb with CREATE DATABASE ORACLE_SID
In my case, '/u01/app/oracle/oradata/mynewdb/redo01.log' is equal to '/oradata/ORACLE_SID/redo01.log' -> how can I embed this variable in the statement?
I hope my question is clear enough. Any hints appreciated.

Alex has given you the best practical help, but for anyone interested or for reference, see below.
If ever you need to reference shell environment variable in sqlplus, the method I use is to run a script that translates shell variables to sqlplus DEFINE statements, e.g.
cat shell2define.sh
set | grep '=' | sed 's/^/define /' > shell.sql
Then in sqlplus:
SQL> ! ./shell2define.sh
SQL> #shell.sql
SQL> define
Now you can refer to the shell variables as you would any sqlplus DEFINEd variable, e.g. &ORACLE_SID. The last "define" command just lists all the variables. Extend the scripts to remove/handle special variables like $_, and ones with quotes, or just use it to include variables you require. Don't forget also the use of $ORACLE_HOME/sqlplus/admin/glogin.sql to invoke this automatically should it be required every time.

Related

Large File export to postgreSQL

I need to export a 50gb file with inserts to a table in postgreSQL to be able to count the time it takes to perform the inserts, but I can't find any way to load that file, can someone help me?
If the file have you have contains syntactically valid SQL (like INSERT statements), this is very straightforward using the command line psql client that comes with a Postgres installation:
psql DATABASE_NAME < FILE_NAME.sql
You may also want to replace DATABASE_NAME with a connection string like postgres://user:pass#localhost/database_name.
This causes your shell to read the given file and pass it off to psql's stdin, which will cause it to execute commands against the database it's connected to.

Namespace error while importing to oracle 12c

I'm a newbie in Oracle 12c who is trying to follow this tutorial.
I've create a new connection orcl/SYSTEM/oracle and logged in as SYSTEM/oracle in SQL developer command line.
The problems is that when I'm trying to import twitter_data.imp from that demo.zip file:
imp dmuser/dmuser file=twitter_data.dmp log=twitter_data.log full=y
It says there's no "TBS_1" namespace. How can I globally create this namespace for oracle 12c (in my new connection). I'm kinda thinking about it should be namespace for table (but there's none, right?).
Thanks.
It's possible you're missing a TABLESPACE; try the following
CREATE TABLESPACE TBS_1 DATAFILE 'TBS_1_dat' SIZE 500K AUTOEXTEND ON NEXT 300K MAXSIZE 100M;
If you have a Oracle SQL Developer, then go to the database connection you have created and click on Manage Database. You would be provided with the list of all tablespace along with the capacity and free space.
twitter_data.dmp file might have been created on the tablespace "TBS_01". While restoring it will search for the same.
Create the tablespace as below while connecting as SYSDBA -
CREATE TABLESPACE tbs_perm_02 DATAFILE 'tbs_01.dat' SIZE 500M
The path of the dat file to be created is up to you, plus auto-extend and other options you choose freely.

SQL Plus SP2-0606

Using SQL tool, I want to create a text file that has a VARCHAR records.
Using SQL Developer I have successfully created the output text file by calling a script. But based on my research, the TRIMSPOOl command is currently not supported by SQL Developer, making my output to have trailing spaces.
Here is the code in the script:
spool on
set echo off
set verify off
set feedback off
set heading off
set trimspool on
set termout off
spool C:\SQLFiles\Output.txt;
select cust_name || cust_addr as Cust_Details
from customer_db;
spool off;
I now have the SQL Plus 11g, and I'm trying to run the script i created in SQL Developer. I'm getting an SP2-0606 error, saying:
SP2-0606: Cannot create SPOOL file "on.LST:
Based on the research I did, it is because of the Spool command, and I don't have the right to access the default folder??..
Can you help please on what setup I should change do to make the desired result in SQL Plus.
You have to put the file name in double quotes - I guess SQLPlus uses C-style escape sequences, giving the backslash \ a special meaning. Also, you should remove the semicolon ';':
So if you replace
spool C:\SQLFiles\Output.txt;
with
spool "C:\SQLFiles\Output.txt"
it should work as expected (if C:\SQLFiles exists and is writable for your account).
UPDATE
As #LalitKumarB has pointed out, spool works perfectly fine without the double quotes. The real problem is elsewhere:
spool on
This tells SQLPlus to create an output file on in the current directory. So you'll get this error message if you don't have access to the directory you're starting SQLPlus from.
You are getting this error because of permission issues. Either open your command prompt window in administrator mode or save your file to a location where admin permission is not required.
From the docs,
SP2-0606 Cannot create file file_name
Cause: The STORE command was unable to create the specified file. There may be insufficient disk space, too many open files, or
read-only protection on the output directory.
Action: Check that there is sufficient disk space and that the protection on the directory allows file creation.
My test case shows there is nothing wrong with the spool command. I don't think there is any need of quotes at all.
For example,
Client: 12c
OS : Windows 7
SQL> spool D:\text.txt
SQL> select ename||to_char(empno) from emp;
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
SMITH7369
ALLEN7499
WARD7521
JONES7566
MARTIN7654
BLAKE7698
CLARK7782
SCOTT7788
KING7839
TURNER7844
ADAMS7876
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
JAMES7900
FORD7902
MILLER7934
14 rows selected.
SQL> spool off;
And the spool file looks like:
So it works without any double-quotation marks. I guess you need to really look at the directory you are using to spool.
this error is just because of , not having "WRITE" privileges on directory where you are creating the spool file.
Solution
give or ask to give the permission to you who is owner of that directory, if its not owned by you using chmod. for ex- full permission to all chmod 777 <file/directory name>
When you get this error check for spaces. For example, below you see both single quotes (') but the first one has a space after it and the second one doesn't.
SP2-0606: Cannot create SPOOL file ' E:\SQL CLASS\New folder\sql class prt'
SQL> spool 'E:\SQL CLASS\New folder\sql class prt'

Unexpected output from using "create database"

My current task is to create a .bat file that can manually create an Oracle database, so that a Database Configuration Assistant is no longer necessary.
I am following this guide.
I am stuck at "Creating the database". Upon typing:
SQL> create database ORA10
I do not get the expected output as described on the guide:
SQL>create database ora10
logfile group 1 ('D:\oracle\databases\ora10\redo1.log') size 10M,
group 2 ('D:\oracle\databases\ora10\redo2.log') size 10M,
group 3 ('D:\oracle\databases\ora10\redo3.log') size 10M
character set WE8ISO8859P1
national character set utf8
datafile 'D:\oracle\databases\ora10\system.dbf'
size 50M
autoextend on
next 10M maxsize unlimited
extent management local
sysaux datafile 'D:\oracle\databases\ora10\sysaux.dbf'
size 10M
autoextend on
next 10M
maxsize unlimited
undo tablespace undo
datafile 'D:\oracle\databases\ora10\undo.dbf'
size 10M
default temporary tablespace temp
tempfile 'D:\oracle\databases\ora10\temp.dbf'
size 10M;
Instead I get a bunch of numbered input requests:
SQL> create database ORA10
2 and
3 it
4 doesn't
5 seem
6 to
7 stop
8 asking
9 for
10 inputs
Other sources/guides I've googled look similar to the aforementioned guide. As far as I know (I might not be using the right keywords), my output is not supposed to happen. I am unable to identify what is going on here.
Please note that I don't actually know much about SQL or using command prompt. My background is limited to classroom HTML/CSS/Java/Python. I have been dared to complete a number of programming related tasks within a certain period of time (a week) without any instruction or preparation - though I am allowed to use the internet for assistance. So far so good until now.
Any assistance will be appreciated, thank you in advance.
All the lines in your listing from create database ORA10 to size 10M; including the last semicolon are not output, but instead part of one single statement. The semicolon terminates it and tells the command interpreter to execute what you've written. Therefore it is continuing to ask you for input until you tell it you're done (with the semicolon).
If you simply want to create a database without specifying any other options, you can simply add a semicolon. The following will create a database with the name "ORA10" in the default configuration:
CREATE DATABASE ORA10;

Oracle Table millions of row, pull and save into file

I have oracle table that holds more than 30 million records, I need to pull all that data into file and store it.. Can anyone suggest me what will be easiest way to do that and what kind of file do I have to use to store all that data. If there is a way I can put data into multiple files that will work too.. You can tell me manual or programatical method. Thank you
Ask Tom has an answer http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::p11_question_id:88212348059
Also, you could do it in SQL*Plus by doing a select with spool on (from a shell script)
sqlplus /nolog <<EOF
conn /as sysdba
set pagesize 0 heading off feedback off verify off echo off trimspool on
spool test.log
select 'dsmc inc "'||file_name||'">>hot_WISDOM_$BCKNAME.log' from dba_data_files
where rownum<5;
spool off;
exit
EOF
More info on the spool stuff at http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:25323432223677
That page is where I found that snippet above.
You will want to replace the connection details with your username / pw.
Starting with Oracle 10g you can create a new table with ORGANIZATION EXTERNAL and INSERT records from the original table into it. See this example from the Oracle 10gR2 documentation.
One of the easiest formats to store data in is comma seperated value (.csv). You can define your delimiter to be any character (comma is a default) as long as you know the delimiter when you are parsing it. (For instance, the ~ character would be a good decision). Usually this format can easily be opened in a spreadsheet program like Excel. It can also be easily parsed to be re-inserted into a database of your choosing.
To export from oracle, you can do something like:
spool backup.csv;
select column1||','||column2||','|| ... from table;
spool off;
After exporting, here is a resource for working with csv files in perl:
http://perlmeme.org/tutorials/parsing_csv.html
Apache commons has a great library for java:
http://commons.apache.org/sandbox/csv/apidocs/org/apache/commons/csv/CSVParser.html