# command does not work in SQL PLUS (OS HP-unix) - sql

I have a sql file (abc.sql) which contains some code. I am able to run this using "run" command as
SQL> run abc.sql
1* select 1 from dual
1
SQL>
But not able to execute using "#" command. If i execute using # it just return to the SQL prompt without executing this file.
SQL> #abc.sql
SQL>
SQL>
Could you please help me in resolving this issue?
FYI, I m using Oracle 8.1.7.4.0 on HP-unix (Tru64 UNIX V5.1B (Rev. 2650)

run does not execute a file's content. Rather, it runs the content of the buffer.
The docu (
http://download.oracle.com/docs/cd/E11882_01/server.112/e16604/ch_twelve037.htm#sthref1803 ) says
R[UN]
Lists and executes the SQL command or PL/SQL block currently stored in the SQL buffer.
The buffer has no command history list and does not record SQL*Plus commands.
Usage
RUN causes the last line of the SQL buffer to become the current line.
The slash command (/) functions similarly to RUN, but does not list the command in the SQL buffer on your screen. The SQL buffer always contains the last SQL statement or PL/SQL block entered.
You are probably misstaking run for the start command.

within your file do have
select 1 from dual
/
you need to tell the sql engine to execute the line (/)
Edit
after the # type in "EDIT" and see what the buffer says:
SQL> #a.sql
1
----------
1
SQL> ed
Wrote file afiedt.buf
...
select 1 from dual
/

Related

Oracle Setting Up a Globalization Support Environment

We want to set this SET NUMFORMAT 99999999999999999.00 at the user/schema level, for all sessions. Currently ,when set this command , it is getting applicable for that session only. Can we do this globally , so that when ever we open the connection , this works >
SET NUMFORMAT is a SQL*Plus command. In general, it is a client-side setting to display the number.
You could always store the SQL*Plus settings in login.sql and glogin.sql. Whenever SQL*Plus starts up, it looks for a file named glogin.sql under the directory $ORACLE_HOME/sqlplus/admin. If such a file is found, it is read and the containing statements are executed. Additionally, after reading glogin.sql, sql*plus also looks for a file named login.sql in the directory from where SQL*Plus was and in the directory that the environment variable SQLPATH points to and reads it and executes it. Settings from the login.sql take precedence over settings from glogin.sql.
If you are just displaying the number, and want it to be displayed in desired format, then use TO_CHAR at individual SQL statement level.
For example,
SQL> select to_char(123.456,'999.9') VALUE from dual
2 /
VALUE
------
123.5
Bottomline, this is a SQL*Plus command, not an Oracle SQL or PL/SQL command. This will only affect how the data is displayed from SQL*Plus, not from other programs that access the database. There should be something similar in whatever you are using to display your data instead of SQL*Plus.

From unix to Sql

I am making a shell script where I am reading inputs from one file. File contains data
123
1234
121
I am reading the inputs from this file using while read line do condition and putting all inputs in SQL statements.Now in my shell script i am going on SQL Prompt and running some queries. In one condition, I am using EXECUTE IMMEDIATE STATEMENT in SQL.
as
EXECUTE IMMEDIATE 'CREATE TABLE BKP_ACDAGENT4 as SELECT * FROM BKP_ACDAGENT WHERE DATASOURCEAGENTID IN ('123','1234','121')';
I want this to be execute, but somehow its not working.
Can anyone help me in executing it?
You need to escape the single quotes which you have used for the predicates in your IN list, that is the single quotes in
WHERE DATASOURCEAGENTID IN ('123','1234','121')';
are causing the issue here. You need to escape the single quotes using two single quotes
EXECUTE IMMEDIATE 'CREATE TABLE BKP_ACDAGENT4 as SELECT * FROM BKP_ACDAGENT WHERE DATASOURCEAGENTID IN (''123'',''1234'',''121'')';
The above will work on all Oracle version.
If you're one Oracle 10g or above, you can use q keyword
EXECUTE IMMEDIATE q'[CREATE TABLE BKP_ACDAGENT4 as SELECT * FROM BKP_ACDAGENT WHERE DATASOURCEAGENTID IN ('123','1234','121')]';

Create text file in oracle using sql

I need to grab data from some oracle database tables and format it into a fixed width text file.
I want to know if its possible to create a text file using sql.
I looked at some stuff and found the bc and xp_cmdshell but they are a bit confusing.
I am pretty new to sql and oracle databases.
Is this possible and how can I begin?
I don't need to open a file or check for existing file, overwriting is fine, what ever makes it easiest.
I don't need anything big or complex, a simple script to grab values and create a text file.
Just an update:
I don't think bcp works in the toad for oracle editor.
I found this tutorial here: http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell
but the first bcp command does not compile, it says invalid sql query
If you are using the SQL*Plus client, you can spool to an output file. Here is a sample SQL*Plus file:
set serveroutput on size 1000000
set linesize 150
spool C:\path_to_file\filename.extension
-- Your SQL statement
select columns
from table
where somecondtion;
-- Your next SQL Statement
select ...
from ...;
spool off
I think you can use sqlplus command line tool todo this. See oracle manual for formatting hints.

What does the "#" do in SQL*Plus

I found this line in a sql code:
#../../sql_scripts/create_tables.sql
What does it do? I know that ##file.sql means that file.sql is run and # could be used when we want to supply parameter values later, but here I have # followed by a filename. I know that there is a similar question but it covers only # in queries.
Here the # is not part of the SQL language. It is likely a command for the SQL interpreter which is probably Oracle SQL*Plus.
SQL*Plus has many single-character commands like # or / (which executes buffered SQL), ; which can be puzzling when you encounter them in an .sql file.
# is documented here in Oracle 9i documentation. There you will see the differences with ##.
documentation for Oracle 11g Release 2, click Next section for ## reference.
The # allows you to import another script into the sql script you're running in SQL*Plus.
For example, this executes the contents of otherscript.sql at the specified point:
PROMPT about to run other script
#otherscript.sql
PROMPT finished running other script
Another example, this inserts the contents of another file into the middle of a statement to be executed in SQL*Plus:
SELECT * FROM mytable WHERE
#predicates_for_mytable.sql
AND bla = 1;
The only condition is that # must appear at the 1st character on the line.

SQL script cshell

I am doing this in my cshell script sqlplus $ORA_UID/$ORA_PSWD #${SQL}test.sql ${DATA}${ext1}thats trying to get output from test.sql script.. in my sql script i am dumping output to spool &1 .. but when i run my script my files are blank i am not getting anything from database.. can someone tell what wrong with this
normally it is not a good idea to have your userid and password displayed in the processlist, as is happening now. Most of the times when sql scripts don't produce the expected output it is because the end of SQL marker is missing. Default the end of SQL is the ';' Reading the end of SQL marker actually starts the SQL statement.
First try the script with feedback on and check the error message in the spoolfile.
Is the spoolfile location OK ?
sqlplus /nolog <<eof
connect $ORA_UID/$ORA_PSWD
#${SQL}test.sql ${DATA}${ext1}
eof
This construction prevents the display of the credentials on the processlist.
In the sqlscript could be
select * from dual;
or
select * from dual
/
but each and every SQL statement has to have an end of SQL marker.