How can i "Clean" output from sqlplus query? - oracle

i have got simple query and i need just one value from it = VALID
The query is:
select 'VALUE('||status||')' as value from user_indexes where index_name = '&1';
But i hve got in out:
C:\Program Files\zabbix\bin\win64\oracle>sqlplus -s #"C:\Program Files\zabbix\bi
n\win64\oracle\conn2.sql" OLAPTABLEVELSID
old 1: select status from user_indexes where index_name = '&1'
new 1: select status from user_indexes where index_name = 'OLAPTABLEVELSID'
VALID
What are this OLD and NEW strings? How can i dismiss it?
Thank you.

SET VERIFY OFF should help you. Please add such line in your script before query.

in sqplus you have substition variables. they are referenced by & or &&
when you run your script and pass it 'OLAPTABLEVELSID' - the query takes the '&1' and replaces it with ''OLAPTABLEVELSID'
SQL*Plus is telling you that in the output
SQL> set verify off
SQL> select '&1' from dual;
Enter value for 1: hello stackoverflow
'HELLOSTACKOVERFLOW
-------------------
hello stackoverflow
SQL>

Related

SQL*Plus - Spool into multiple files

I've got the next spool that stores the DDL of the user_tables into a file:
set pagesize 0
set long 90000
spool C:\Users\personal\Desktop\MAIN_USR\test.txt
select DBMS_METADATA.GET_DDL('TABLE',table_name,'MAIN_USR')
FROM user_tables ut;
spool off
exit
It returns the DDL of all user_tables into a single file, but I need it to be a little more dynamic and return them in separate files with the file name of their respective table. Something like this:
set pagesize 0
set long 90000
FOR tab_nam IN (SELECT table_name FROM user_tables) LOOP
spool C:\Users\personal\Desktop\MAIN_USR\test.txt
select DBMS_METADATA.GET_DDL('TABLE',table_name,'MAIN_USR')
FROM user_tables ut;
spool off
END LOOP;
exit
I know the one above won't work, but it's kind of an idea of what I want to do.
I appreciate any kind of help
You need to read USER_TABLES for creating N calls to DBMS_METADATA.GET_DDL each of which has its own spooled file. Spool everything to a file named out.sql and run it after spooling it off
set pagesize 0
set long 90000
SET TERMOUT OFF
spool out.sql
select 'spool C:\Users\personal\Desktop\MAIN_USR\'||REPLACE(table_name, '$', '_')||'.txt'||chr(13)||chr(10)||
'SELECT DBMS_METADATA.GET_DDL(''TABLE'','''||table_name||''',''MAIN_USR'') FROM DUAL;'||chr(13)||chr(10)||
'spool off' as cmd
FROM user_tables ut;
spool off
#OUT.SQL
exit
Using python's cx_Oracle module which enables access to Oracle Database might be elegant way for your case :
import cx_Oracle
con = cx_Oracle.connect('uname/pwd#host:port/service_name')
cur = con.cursor()
def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType == cx_Oracle.CLOB:
return cursor.var(cx_Oracle.LONG_STRING, arraysize = cursor.arraysize)
cur.outputtypehandler = OutputTypeHandler
cur.execute("select table_name from user_tables order by 1")
rec = cur.fetchall()
for r in rec:
cur.execute("select dbms_metadata.get_ddl('TABLE',:tableName) from dual",tableName=r[0])
ddl, = cur.fetchone()
file = r'C:\\Users\\personal\\Desktop\\MAIN_USR\\'+r[0]+'.txt'
with open(file,"w") as f:
f.write(ddl)
f.close()
where the table names of the whole schema are determined through the first cur.execute, and their creation DDL are done in the second, and files are created with respective table names at the last step. Important thing to consider is related to use of OutputTypeHandler is because of getting rid of processing the CLOB result steming from Dbms_Metadata.Get_Ddl function. The compiler wouldn't want to get a CLOB value during the creation of the files, or DDL would be so long that will exceed the length 4000 chars which would prevent to_char conversion with raising error without using OutputTypeHandler.

How to replace single quote with space in column in oracle database

I have a problem in updating a column in oracle which has a single quote.
The following example will clear the problem.
Lets Client name is Lucy'Mark
Now, I want to replace the Single quote with space
After output, it will be Lucy Mark
Now when I tried the following query it is not working as the query will be
select replace (Lucy'Mark , '''', '') from gen_clientvendor_m;
Please let me know the query.
I am using SQL developer
Use the column with client name and add space to the replace statement:
select replace (client_name , '''', ' ') from gen_clientvendor_m;
Multiple single quotes cause headache :) so - have a look at this option:
SQL> with test (name) as
2 (select q'[Lucy'Mark]' from dual)
3 select name,
4 replace(name, chr(39), ' ') result
5 from test;
NAME RESULT
--------- ---------
Lucy'Mark Lucy Mark
SQL>

Getting Unknown Command error on IF-THEN-ELSE

I have the following query that I am using in Oracle 11g
IF EXISTS (SELECT * FROM EMPLOYEE_MASTER WHERE EMPID='ABCD32643')
THEN
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1' where EMPID='ABCD32643' ;
ELSE
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE) values('A62352','JOHN DOE','1') ;
END IF;
On running the statement I get the following output:
Error starting at line : 4 in command -
ELSE
Error report -
Unknown Command
1 row inserted.
Error starting at line : 6 in command -
END IF
Error report -
Unknown Command
The values get inserted with error when I run it directly. But when I try to execute this query through my application I get an oracle exception because of the error generated :
ORA-00900: invalid SQL statement
And hence the values are not inserted.
I am relatively new to Oracle. Please advise on what's wrong with the above query so that I could run this query error free.
If MERGE doesn't work for you, try the following:
begin
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1'
where EMPID='ABCD32643' ;
if SQL%ROWCOUNT=0 then
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE)
values('A62352','JOHN DOE','1') ;
end if;
end;
Here you you the update on spec, then check whether or not you found a matching row, and insert in case you didn't.
"what's wrong with the above query "
What's wrong with the query is that it is not a query (SQL). It should be a program snippet (PL/SQL) but it isn't written as PL/SQL block, framed by BEGIN and END; keywords.
But turning it into an anonymous PL/SQL block won't help. Oracle PL/SQL does not support IF EXISTS (select ... syntax.
Fortunately Oracle SQL does support MERGE statement which does the same thing as your code, with less typing.
merge into EMPLOYEE_MASTER em
using ( select 'A62352' as empid,
'JOHN DOE' as empname,
'1' as emptype
from dual ) q
on (q.empid = em.empid)
when not matched then
insert (EMPID,EMPNAME,EMPTYPE)
values (q.empid, q.empname, q.emptype)
when matched then
update
set em.empname = q.empname, em.emptype = q.emptype
/
Except that you're trying to update empid as well. That's not supported in MERGE. Why would you want to change the primary key?
"Does this query need me to add values to all columns in the table? "
The INSERT can have all the columns in the table. The UPDATE cannot change the columns used in the ON clause (usually the primary key) because that's a limitation of the way MERGE works. I think it's the same key preservation mechanism we see when updating views. Find out more.

output/echo a meesage in hql/ hive query language

I need to create a hive.hql as follows.
HIVE.hql:
select * from tabel1;
select * from table2;
My question is: can i echo any message to my console like " results from table1 is obtained " in the hql code after table one is created like
select * from tabel1;
echo/print/output ("table 1 results obtained");
select * from table2;
In the *.hql file insert a line as below in between the two hive queries.
!echo "table 1 results obtained";
You can add a comment by editing your HIVE.hql :
select * from tabel1;
!sh echo "table 1 results obtained";
select * from table2;
In beeline just use the below
--add your comment after 2 hypens and its printed in the console
Other solutions didnt work probably they are removed in current version

Table record count to unix log file

I need the count of records of a data base table from unix.
I am calling one sql script from unix and need the record count to any log file .
You can put the following into a test.sql file:
SET HEADING OFF;
SELECT COUNT(*) FROM dual;
QUIT;
and call it via SQL*Plus via script.
It will output:
1
since table dual has only one row. You should be able to write that into a log file.
You cant to append the output off your script to a named file by rediecting it like this.
$ sqlplus username/password#SID #your_script.sql >> /tmp/whatever.log
If your want to have more than a bald count in the output you'll need to include the boilerplate in the projectors:
SQL> select to_char(sysdate, 'YYYYMMDDHH24MISS')||'::Number of emps = '
2 , count(*)
3 from emp
4 group by to_char(sysdate, 'YYYYMMDDHH24MISS')||'::Number of emps = '
5 /
TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS COUNT(*)
--------------------------------- ----------
20100210133747::Number of emps = 16
SQL>

Resources