sqplplus - echo current login info - oracle

Is there a way to echo/print the current connection string in sqlplus? I have a script that will be run in multiple databases, and I'd like a line at the top that looks like:
Currently executing test_script.SQL as USER1#MY_DB
It would be very convenient if there was a way to do this without having to change the file itself.

You can use the built-in SQL*Plus substitution variables _USER and _CONNECT_IDENTIFIER.
Add the following line to your SQL*Plus script:
prompt Currently executing test_script.SQL as &_USER#&_CONNECT_IDENTIFIER
Here is the link to the documentation.

See this question:
In SQL*Plus, how do I change the prompt to show the connected user and database?

Related

What does the following expression do <<EOF>tmp

A shell script with a database connection using sqlplus contains this line:
sqlplus username/password#DBNAME<<EOF>tmp.
(here there is a select sql query)
I know <<EOF indicates the start of the command. I have the below doubts in the >tmp part.
1)Does it indicate storing into a file?
2) If yes will it put the results of the SQL query alone into the tmp file or the query and the results both.
Could you please clear me on this one?
<<EOF is known as "heredocs". It means that from the next line in the script, up to a line that says "EOF", will be read in by the shell, and offered to the command on standard input.
In your case, the sqlplus command is being fed the SQL query as if it came from standard input. Then, the output of sqlplus is being saved to the file called tmp.
The input and output are separate. However, sqlplus as a command has a habit of repeating its input to the output, unless instructed otherwise with SET ECHO OFF.

BTEQ interactive window, need arrow-up to access command history

This is strange. New linux environment, accessed from my desktop via putty...
In my regular shell, I can up and down arrow to get command history. However, when I fire up BTEQ to talk to the database, I get this kind of gibberish in response to my up/down/ctrl+up/etc.
BTEQ -- Enter your SQL request or BTEQ command:
^[OA^[OB^[[A^[[B^[OA^[OA^[[D
Is this something in BTEQ? I would assume more of a setting in putty or "stty" type of command in my .profile. However, since the behavior is normal until I launch BTEQ I can't be sure.
BTEQ does not maintain a command history like the shell in your LINUX/UNIX environment. Therefore, using the up/down/ctl+up keys will be interpreted as input and not interpreted by the shell to navigate a command history.

how to create a user in oracle using SQL*PLUS by running a script from txt file that will prompt me for 2 parameters (username, password)

I have a question about creating users in Oracle using SQL*PLUS. But what I want to do is write the syntax in a txt file first (script) then when I run the script by calling it using the # or by copy/past it should prompt me for two parameters (user name and a password). I searched through the web and read 2 books and I still can't find the right syntax.
You can use the ACCEPT command to prompt the user for an input (but it's not really necessary if you use substitution variables that are prefixed with the & character.
Details are in the manual:
http://docs.oracle.com/cd/B28359_01/server.111/b31189/ch5.htm#CACIFHGB
If you want to customize the message displayed to the user when entering the value, you can use the ACCEPT command.
Again details are in the manual:
http://docs.oracle.com/cd/B28359_01/server.111/b31189/ch12005.htm#BACGJIHF

SQL*Plus - How do you hide SET ECHO OFF?

In SQL PLus, if I want to hide the command on screen, I use at the beginning of the script:
SET ECHO OFF
But what about this command? How to hide also this?
In MS-Dos you could use # in front of it, but in SQL*Plus?
EDIT:
I know the -S option for SQL*Plus, but the problem is that I am sending my script to the client and therefore I don't control how they are running SQL*Plus.
You might want to start SQL*Plus with the -S flag:
sqlplus -S user/password#db #path/to/your/script
The documentation says: SILENT Option
-S[ILENT]
Suppresses all SQL*Plus information and prompt messages, including the command prompt, the echoing of commands, and the banner normally displayed when you start SQL*Plus. If you omit username or password, SQL*Plus prompts for them, but the prompts are not visible! Use SILENT to invoke SQL*Plus within another program so that the use of SQL*Plus is invisible to the user.
SILENT is a useful mode for creating reports for the web using the SQLPLUS -MARKUP command inside a CGI script or operating system script. The SQL*Plus banner and prompts are suppressed and do not appear in reports created using the SILENT option.SILENT Option
So, with the silent option it seems that there won't be need to use set echo at all.
No, I know of no way to hide the 'set echo off' command itself, if echo is on.
However, echo is off by default. So, if echo is on when you initially start SQL*Plus, perhaps you have a login.sql or glogin.sql that contains 'set echo on'?
Check for glogin.sql in $ORACLE_HOME/sqlplus/admin, and for login.sql in the directory where you started SQL*Plus and any directory listed in $SQLPATH.
Hope that helps.

problem with oracle sqlplus with whitespace in the path of the # command

I'm running Oracle 11g on Linux and I'm trying to run a script which will create my database. This script runs fine on windows, but when I test it on Linux, I get the following error:
SP2-0556: Invalid File Name
The problem may be that the path to the file name has a space in it. I'm going to simplify the problem down to one of the many commands I run in the file to make it simple. The sample command I'm trying to run looks like this:
sqlplus [uname]/[pw] #'../database/My Schema/create_sequence.sql'
the create_sequence.sql file has two simple create sequence commands that run fine by themselves. I strongly suspect it is due to the white space because when I change the directory name from My Schema to MySchema and alter the above sqlplus command accordingly, the script runs fine.
Like I said, this script works in windows with the spaces, but not in Linux. I suspect spaces may not be supported, but I was wondering if anyone knew any different or it there is a work-around?
side note: running a command like:
more ../database/My\ Schema/create_sequence.sql
or
more "../database/My Schema/create_sequence.sql"
prints the contents of the file to the console as you would expect. So, I think this is sqlplus (and linux) specific.
I connected to one of my Linux boxes and was pretty easily able to reproduce this issue. There doesn't seem to be any way that I can find to execute the file with the '#' option from the command line so I think you're left with the following options for work arounds:
Rename the My Schema directory to no longer have a space in it (as well as updating all other scripts that reference it
Execute the file from the directory in which it resides (I confirmed that this works, see below)
Send the file into sqlplus via stdin (see below)
You should also file a report with Oracle support as there may be a simple fix that they can provide.
Example Commands:
From Directory
cd ../database/My\ Schema
sqlplus [uname]/[pw] #create_sequence.sql
Via stdin
sqlplus [uname]/[pw] < ../database/My\ Schema/create_sequence.sql
Well, if this is a Linux issue (see my comment on your question - it works fine on Solaris), you may have to try something along the lines of:
sqlplus [uname]/[pw] < '../database/My Schema/create_sequence.sql'
You run into problems if you're trying to pass parameters to your sql script, however...
EDIT: There seems to be a Metalink issue raised for a very similar problem: "Bug 7150873 SQL scripts with filename containing spaces results in SP2-0556". It is listed as affecting 10.2.0.4 and 11.1. It is supposedly fixed in 10.2.0.5 and 11.2, neither which are available yet. It does say it's a generic issue affecting most/all platforms, so I don't know if this is your problem or not.
The specific text of the issue: "The SQLPLUS START command fails to execute SQL scripts which have a space in the filename."
Just for grins, what happens if you do the following:
sqlplus [uname]/[pw]
start '../database/My Schema/create_sequence.sql'
EDIT2: I don't know if modifying your scripts wholesale is feasible or not, but a workaround might be:
cp '../database/My Schema/file2run.sql' ./temp.sql
sqlplus [uname]/[pw] #temp.sql
rm ./temp.sql
You would need to wrap each sqlplus call this way. Another option would be to create a shell script, say with a name of mysqlplus.sh:
#!/bin/sh
cp $2 ./temp$$
sqlplus $1 #$2
rm ./temp$$
Then modify your build scripts thus:
mysqlplus.sh [uname]/[pw] '../database/My Schema/create_sequence.sql'
According to this thread on the OTN site, SP2-0556 can be caused by invalid white space characters in the file that is being executed. Likely the Linux version of SQL-Plus doesn't know how to deal with Windows newline character(s). Try deleting the existing file and recreating it with your desired commands (you said there are only 2 DDL commands so it should be easy).
If you put quotes around path, it works:
SQL > START "C:\Documents and Settings\Administrator\demobuild.sql"
This does not work:
SQL > START C:\Documents and Settings\Administrator\demobuild.sql
have you tried escaping the white space?
Try:
sqlplus [uname]/[pw] #'../database/My\ Schema/create_sequence.sql'
If you're working with this under linux you can escape the space with a '\' character like such:
sqlplus [uname]/[pw] #../database/My\ Schema/create_sequence.sql
I know it's an old question but I just ran into the same situation and after a lot of tries I made it work and I want to share my solution, it was simply put the path into quotes like this:
#"C:/Users/john/AppData/Roaming/SQL Developer/test.sql";

Resources