sqlplus query with comment giving duplicate rows - oracle

Updating my question:
When I run this query from unix using sqplus it gives me duplicate outputs
Query:
select sysdate from dual
/
/*comment*/
EXIT
Output
SQL> #tt.sql
12-AUG-16
12-AUG-16

Here is what is happening.
The first two lines (including the /) form a complete SQL statement. SQLPlus executes it and displays the first line.
Then /*comment*/ is OUTSIDE the SQL statement. So it is interpreted by SQLPlus, not as a SQL comment.
The point is, comments in the SQLPlus scripting language have a different syntax; they are lines that begin with REM[ARK]. /*comment*/ is NOT a comment in SQLPlus.
Instead, in SQLPlus, a slash / as the first character on a new line means repeat the last SQL command (if after that you had SQLPlus commands, like DESCRIBE or COLUMN, those are ignored). Everything AFTER the slash is ignored. To convince yourself of this, try again, but with the comment line changed to /*comment* (delete the second slash). You will see the same result.
Then you have the command EXIT, but since it is NOT followed by a slash, it is not seen as a completed command. If I copy and paste your code in my SQLPlus window, I get the screenshot below. However, if at this point you press ENTER (or if this was in a sql script and not copied and pasted from a text editor), the SQLPlus window would shut down.
Hope this helps!
SQL> select sysdate from dual
2 /
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.00
SQL> /*comment*/
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.01
SQL> EXIT

Related

what is Oracle command 'o;'

I am using the oracle version.
Oracle Database 11g Release 11.2.0.1.0
I accidentally ran the command o; in oracle delveloper.
The result is as below.
The PL/SQL procedure completed successfully.
not spooling currently
The sqlcl_int_runme alias has been removed.
I don't know what I did....
First of all, there seems to be no problem with basic table CRUD.
Has anyone had this experience?
I need an explanation of what happened...
It's an alias.
We copied over some popular commands from postgresql to SQLcl, one of those was 'o'
From the post docs
\o or \out [ filename ] \o or \out [ |command ] Arranges to save
future query results to the file filename or pipe future results to
the shell command command. If no argument is specified, the query
output is reset to the standard output.
If the argument begins with |, then the entire remainder of the line
is taken to be the command to execute, and neither variable
interpolation nor backquote expansion are performed in it. The rest of
the line is simply passed literally to the shell.
“Query results” includes all tables, command responses, and notices
obtained from the database server, as well as output of various
backslash commands that query the database (such as \d); but not error
messages.
SQL> alias
\! \? \c \cd \d \dp \dt \dt+ \e \echo \encoding \i
\o \p \prompt \q \qecho \r \save \timing \w \z clear cls
cpu fuzzy gglag locks sessions tables tables2 topsql
SQL> alias list \o
\o NULLDEFAULTS psql - desc \o [FILE_NAME] - turn spool log file on (or off if no FILE_NAME given)
--------------------------------------------------------------------------------------------------
Declare
maxpos number:=null;
BEGIN
if (:sqlcl_int_first is null) then
:sqlcl_int_runme:='spool off';
else
:sqlcl_int_runme:='spool '||:sqlcl_int_first||' ';
end if;
end;
/
alias NULLDEFAULTS sqlcl_int_runme=:sqlcl_int_runme;
sqlcl_int_runme
alias drop sqlcl_int_runme
To see it in action...
SQL> set sqlformat csv
SQL> o stackoverflow.csv
PL/SQL procedure successfully completed.
Alias sqlcl_int_runme dropped
SQL> select * from regions;
"REGION_ID","REGION_NAME"
1,"Europe"
2,"Americas"
3,"Asia"
4,"Middle East and Africa"
SQL> o
PL/SQL procedure successfully completed.
Alias sqlcl_int_runme dropped
SQL> !dir stackoverflow.csv
Volume in drive C is System
Volume Serial Number is F897-6A6F
Directory of c:\sqlcl\22.2.1\sqlcl\bin
08/30/2022 08:09 AM 170 stackoverflow.csv
1 File(s) 170 bytes
0 Dir(s) 190,156,173,312 bytes free
SQL> !type stackoverflow.csv
Alias sqlcl_int_runme dropped
"REGION_ID","REGION_NAME"
1,"Europe"
2,"Americas"
3,"Asia"
4,"Middle East and Africa"
PL/SQL procedure successfully completed.

Single command from batch file with sqlplus not working in some cases

I have the following command in a batch file.
set tableName=%1
select count(1) from %tableName% where to_char(DATEVALUE,'yyyy-mm-dd hh24:mi:ss')^>(select to_char(max(DATEVALUE),'yyyy-mm-dd hh24:mi:ss') from FOO_TABLE); | sqlplus !connectionString!
This statement doesn't work. I can see that it connects to the database and then disconnects. But the following works:
select count(1) from %tableName% where to_char(DATEVALUE,'yyyy-mm-dd hh24:mi:ss')=(select to_char(max(DATEVALUE),'yyyy-mm-dd hh24:mi:ss') from FOO_TABLE); | sqlplus !connectionString!
I am guessing the problem could be with the greater than > symbol. I tried ^>,> and \>. None of them works. How can I get this sql statement to work.
(I have connectionString already set in my batch file in earlier lines).
The output in the command line is
Connected to:
Oracle Database ... (more db info)
SQL> Disconnected from Oracle Database ... (more db info)
It looks like you need to escape the ^ escape character as well; depending on exactly how you're running this, either:
... where to_char(DATEVALUE,'yyyy-mm-dd hh24:mi:ss')^^>(select ...
or
... where to_char(DATEVALUE,'yyyy-mm-dd hh24:mi:ss')^^^>(select ...
In a batch file where the query is echoed and piped the triple-escape works:
#setlocal EnableDelayedExpansion
#set connectionString=x/y#z
#set tableName=bar
#echo select count(1) from %tableName% where to_char(DATEVALUE,'yyyy-mm-dd hh24:mi:ss')^^^>(select to_char(max(DATEVALUE),'yyyy-mm-dd hh24:mi:ss') from FOO_TABLE); | sqlplus !connectionString!
Running that batch script shows the statement being run (and erroring in my case with ORA-00942, which is expected). With a single or double ^ it has nothing to run at the SQL prompt and a file is created instead, which seems to be what you're seeing.

PL SQL output is not getting displayed

I have fairly simply code ..running in Oracle Virtualbox. However for some reason it is not displaying pl/sql output.
Here is code snippet
SQL> set serveroutput on
SQL> list
1 Create or Replace procedure mytz
2 IS
3 v_mytz TIMESTAMP WITH TIME ZONE DEFAULT '2013-05-05 12:00:00 AM';
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE ('Default timestamp is ' );
6* end mytz ;
SQL> /
Procedure created.
SQL>
Is there anything I need to do special to see the output on SQL prompt ?
You have to actually run the procedure, not just create it, e.g.:
set serverputput on
exec mytz;
The set serveroutput SQL*Plus command has to be in the session the procedure is executed, not the one where it is created (if they are different).
You are not showing the value of your variable at the moment; maybe you wanted this?
dbms_output.put_line('Default timestamp is: ' || v_mytz);

using SQL query on unix [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Select columns into local variable from sql script using shell script
im trying to write a unix script that will retrieve a parameter using sql query and run a script afterwards with this parameter.
for the time being, im just tryin to make it echo the retrieved parameter.
the sql query that works fine on toad (oracle 8) is :
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043
the above query give a number.
now the script i wrote is:
#!/bin/bash
echo "this script will print the billcycle date"
v_bc=`sqlplus -s /#bscsprod <<EOF
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043`
echo "billcycle number is $v_bc"
the result when i run the file is
billcycle number is
with no number that follows.
any ideas what's wrong ? maybe the syntax for connecting to the sql server ?
thanks
Assaf.
The duplicate question APC linked to shows a working example, but to clarify you have two problems. The first is non-fatal and is just that you don't have EOF, as Rembunator pointed out (though it's in the wrong place in that answer).
More importantly though you don't have a terminating ; in your query, so SQL*Plus won't execute it - it just exits with no output.
If you typed your original query in as the SQL*Plus command prompt it would leave you at a further prompt waiting for input, and then go back to a normal prompt if you just hit return again, without actually executing the query:
SQL> select billcycle from bc_run
2 where billcycle not in (50,16)
3 and control_group_ind is null
4 and billseqno=6043
5
SQL>
You also probably want at least some formatting of the output. So this should work:
v_bc=`sqlplus -s /#bscsprod <<EOF
set pagesize 0
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043;
EOF`
I think you should end with EOF as well:
v_bc=`sqlplus -s /#bscsprod <<EOF
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043
EOF`
Edited: Oops, as corrected by Alex Poole.

Oracle 11g Report Problem

Hey friends i am using Oracle 11G. I wanted to generate reports for printing , SO i write this script
rem Employee Salary Report
set headsep !
ttitle 'Salary Report'
btitle 'From Employees'
column employee_id format 999.99
column first_name format a20
column last_name format a20
column Salary format 999.99
break on employee_id skip 1 on report
set linesize 80
set pagesize 5
set newpage 0
set feedback off
set pause 'More...'
set pause on
spool activity.lst
select employee_id,first_name,last_name,salary from Employees order by employee_id ;
spool off
When running this script oracle gives
line 3: SQLPLUS Command Skipped: set headsep !
line 15: SQLPLUS Command Skipped: set linesize 80
line 16: SQLPLUS Command Skipped: set pagesize 5
line 17: SQLPLUS Command Skipped: set newpage 0
and then it executes query and gives output. But my report doesn't include any title in it.Means reports is not generated properly. It just simply executes the select query and gives an output which is not a report.
Did you have a question?
What you show us looks very much like the output we would expect if your script was run from the SQL Worksheet in Oracle SQL Developer.
Those commands that are being skipped are specific to SQL*Plus, and are not supported in SQL Developer (at least, in the version I'm running).
To get a formatted report produced by SQL*Plus, I would run the sqlplus executable from the OS e.g.
> $ORACLE_HOME/bin/sqlplus /
SQL> #/home/spencer7593/myreport.sql
SQL> exit
>
I'm not sure that answers a question, since I don't know what your question was.

Resources