How to export query "DESCRIBE table" ORACLE to .xls? - excel

How to export query of "DESCRIBE table" to excel file?
For example :
I have query like that "DESCRIBE Mst_Fi_Bond_Issuers;" and i have result like that :
I want export that automatically to excel table. Please help me. Thanks..

You could use an SQL block like this: [please edit as you see fit]
DECLARE
createdir VARCHAR2(2000);
directory NUMBER;
filen VARCHAR2(50);
dirn VARCHAR2(50);
filedat UTL_FILE.file_type;
BEGIN
dirn := 'DESC';
filen := 'yourfilename.csv';
createdir := q'{create directory DESC as '[your directory]'}';
execute immediate (createdir);
filedat := UTL_FILE.fopen(dirn, filen, 'W', 32767);
UTL_FILE.put_line (filedat, 'NAME;TYPE');
for rowdat in (select column_name || ';' || data_type currow from dba_tab_columns)
LOOP
UTL_FILE.put_line (filedat, rowdat.currow);
END LOOP;
UTL_FILE.fclose (filedat);
END;
/

As jera already said, it´s much better to do a query and write results to a file, because you have much more flexibility in gathering informations and formatting output.
But maybe due to restrictions, your not able to access the dba_... Objects.
Instead of that, have a look on the USER_... - Views, which are always available to a oracle-user
Everything you want to know can be found there (f.e. like USER_TABLES. USER_TAB_COLS, USER_PROCEDURES, etc.).

Related

How to create a complex PL/SQL procedure with Python, cx_Oracle

I have a multi-line PL/SQL procedure, which I have to create.
The SQL procedure is similar to the one below,
CREATE OR REPLACE PROCEDURE HELLO AS
TYPE cur_cur is REF CURSOR;
v_cur_cur cur_cur;
age NUMBER;
day VARCHAR2(10);
date DATE;
BEGIN
<Some Execute Immediate stmts>
<Some insert stmts>
commit;
END;
Currently what I am doing is,
host= "localhost"
port= 1521
sid= "abcbcadacsw.com"
user= "groups"
password= "hello!bye1209"
dsn_tns = oracledb.makedsn(host, port, service_name=sid)
print(dsn_tns)
db_conn = oracledb.connect(user=user, password=password, dsn=dsn_tns)
curs= db_conn.cursor()
curs.execute("""
CREATE OR REPLACE PROCEDURE HELLO AS
TYPE cur_cur is REF CURSOR;
v_cur_cur cur_cur;
age NUMBER;
day VARCHAR2(10);
date DATE;
BEGIN
<Some Execute Immediate stmts>
<Some insert stmts>
commit;
END;
""")
The thing is the code runs without any issues, there are not runtime errors or anything ... but when i log into the DB to check for the created procedure, its not present. When i try to execute the procedure, it says 'identifier must be declared ... '.
I have tried converting it into a single line
curs.execute("""CREATE OR REPLACE PROCEDURE HELLO AS TYPE cur_cur is REF CURSOR; v_cur_cur cur_cur; age NUMBER; day VARCHAR2(10); date DATE; BEGIN <Some Execute Immediate stmts> <Some insert stmts> commit; END;""")
This also does not work.
Please assist, ignore the correctness of the above shown procedure, i cannot put the original here, and i dont know much of SQL, i just need to know how to successfully create it in Python.
The driver doesn't (yet) return Oracle DB's 'success with info' errors so if there is a problem with the PL/SQL code you won't find out about it unless you explicitly query the error view.
In SQL*Plus:
create or replace procedure fred as
begin
f();
end;
/
would give:
Warning: Procedure created with compilation errors.
and a subsequent show errors will give
Errors for PROCEDURE FRED:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/3 PL/SQL: Statement ignored
3/3 PLS-00201: identifier 'F' must be declared
With cx_Oracle (and its new version python-oracledb) you don't get the initial indication there was a problem so you always should do the equivalent of the show errors command to check the error view. Try something like:
with connection.cursor() as cursor:
sql = """create or replace procedure fred as
begin
f();
end;"""
cursor.execute(sql)
sql = """select name, type, line, position, text
from user_errors
order by name, type, line, position"""
for r in cursor.execute(sql):
print(r)
which will show output like:
('FRED', 'PROCEDURE', 3, 20, 'PL/SQL: Statement ignored')
('FRED', 'PROCEDURE', 3, 20, "PLS-00201: identifier 'F' must be declared")
This is shown in the documentation Creating Stored Procedures and Packages.

Oracle Variable name as value oracle

I have created some variables. I want to use the variable name as an input of another query.
Is there any method to get a local variable name as a string value in Oracle.
Example Scenario
declare
FASTFUNDS VARCHAR(100);
begin
FASTFUNDS := 'TEST001';
SELECT v_variable, v_value FROM v_Table WHERE v_variable = FASTFUNDS.toString()
Results
v_variable v_value
FASTFUNDS TEST001
This isn't a Java code, so there isn't any String type, but VARCHAR (as you defined)
Just remove .toString() and it'll be a valid statement:
SELECT v_variable, v_value FROM v_Table WHERE v_variable = FASTFUNDS;
declare
FASTFUNDS VARCHAR(100);
stmt varchar2(50);
begin
FASTFUNDS := 'TEST001';
stmt := 'SELECT v_variable, v_value FROM v_Table WHERE v_variable = '|| FASTFUNDS;
EXECUTE IMMEDIATE stmt;
end;
i dont know your objective, but id do something like this.

How to fetch entire multi line Statement using grep in Unix

--Oracle 11g
--Linux
I am trying to create missing objects, and want to fetch the ddl for any particular Object from the ddl of all objects in a file.
I want to fetch the ddl for one object using grep, but the create statement is broken into multi-line, and through grep only one line is getting fetched.
How to fetch complete create statement using grep or any other method to do this?
Let's say, below is the file containing many create statements for all objects, I want to fetch only create statements for only missing objects, so i grep missing object_name from the ddl_file.
file containing all create_statement dll : create_statement_ddl.txt
cat create_statement_ddl.txt
CREATE OR REPLACE TRIGGER pt_set_session_tz
AFTER LOGON ON SCHEMA
DECLARE
v_tz VARCHAR2(100);
v_no_data BOOLEAN:=FALSE;
BEGIN
v_tz:=pkg_tool.get_db_timezone;
IF (v_tz IS NOT NULL) THEN
EXECUTE IMMEDIATE 'ALTER SESSION SET TIME_ZONE=' || ''''||v_tz||'''';
END IF;
END;
/
create or replace trigger PT_AI_CMPL_STAT after INSERT on PA_CMPL_STAT for each row
begin
INSERT /*+ B1805_DDL */ INTO PH_CMPL_STAT
(CMPL_STAT_ID,
CPNT_TYP_ID,
CMPL_STAT_DESC,
PROVIDE_CRDT,
TAP_DEF_ID,
REQUIRE_ESIG,
LST_UPD_USR,
LST_UPD_TSTMP,
ACTION
)
VALUES
(:new.CMPL_STAT_ID,
:new.CPNT_TYP_ID,
:new.CMPL_STAT_DESC,
:new.PROVIDE_CRDT,
:new.TAP_DEF_ID,
:new.REQUIRE_ESIG,
:new.LST_UPD_USR,
:new.LST_UPD_TSTMP,
'I'
);
end;
/
create or replace trigger PT_AU_CMPL_STAT after UPDATE on PA_CMPL_STAT for each row
begin
INSERT /*+ B1805_DDL */ INTO PH_CMPL_STAT
(CMPL_STAT_ID,
CPNT_TYP_ID,
CMPL_STAT_DESC,
PROVIDE_CRDT,
TAP_DEF_ID,
REQUIRE_ESIG,
LST_UPD_USR,
LST_UPD_TSTMP,
ACTION
)
VALUES
(:new.CMPL_STAT_ID,
:new.CPNT_TYP_ID,
:new.CMPL_STAT_DESC,
:new.PROVIDE_CRDT,
:new.TAP_DEF_ID,
:new.REQUIRE_ESIG,
:new.LST_UPD_USR,
:new.LST_UPD_TSTMP,
'M'
);
end;
/
grep missing_object_name `cat create_statement_ddl.txt` > create_object.sql
grep pt_set_session_tz `cat create_statement_ddl.txt` > create_object.sql
What I am getting is
CREATE OR REPLACE TRIGGER pt_set_session_tz
What i want is below:
CREATE OR REPLACE TRIGGER pt_set_session_tz
AFTER LOGON ON SCHEMA
DECLARE
v_tz VARCHAR2(100);
v_no_data BOOLEAN:=FALSE;
BEGIN
v_tz:=pkg_tool.get_db_timezone;
IF (v_tz IS NOT NULL) THEN
EXECUTE IMMEDIATE 'ALTER SESSION SET TIME_ZONE=' || ''''||v_tz||'''';
END IF;
END;
/
grep is for finding regexp on individual lines and printing the result, it's the wrong tool for this. If I understand you correctly then with awk it'd just be:
awk '/pt_set_session_tz/{f=1} f; /^\//{f=0}' create_statement_ddl.txt

column value can be inserted

I know I can use CASE statement inside VALUES part of an insert statement but I am a bit confused.
I have a statement like,
You can try also a procedure:
create or replace procedure insert_XYZ (P_ED_MSISDN IN VARCHAR2,
P_ED_OTHER_PARTY IN VARCHAR2) is
begin
INSERT INTO TABLE_XYZ ( ED_MSISDN,
ED_OTHER_PARTY,
ED_DURATION)
VALUES (P_ED_MSISDN ,
P_ED_OTHER_PARTY ,
CASE
WHEN P_ED_OTHER_PARTY = '6598898745' THEN
'9999999'
ELSE
'88888'
END);
END;
Here's a query structure that you can use (using JohnnyBeGoody's suggestion of using a SELECT statement to select the values).
INSERT INTO TABLE_XYZ (ED_MSISDN, ED_OTHER_PARTY, ED_DURATION)
SELECT
'2054896545' ED_MSISDN,
'6598898745' ED_OTHER_PARTY,
CASE
WHEN ED_OTHER_PARTY = '6598898745' THEN '9999999'
ELSE '88888'
END ED_DURATION
FROM DUAL;
You cannot self-reference a column in an insert statement - that would cause an "ORA-00984: column not allowed here" error.
You could, however, use a before insert trigger to achieve the same functionality:
CREATE OR REPLACE TRIGGER table_xyz_tr
BEFORE INSERT ON table_xyz
FOR EACH ROW
NEW.ed_duration = CASE NEW.ed_other_party
WHEN '6598898745' THEN '9999999'
ELSE '88888' END;
END;

select from variable

this might be a simple one. Basically in PL/SQL in Oracle I am selecting from another database:
select * From Store#dbstore.p009061;
What I want is that the value 061 will be coming from a variable name (myStore).
so it will be like this:
select * from STORE#DBSTORE.P||myStore||'081';
remember myStore above will hold the value 061.
But the above doesn't work? Can someone help? Thanks
hmmm, you can't do this directly in a query, you must use dynamic sql.
something like
declare
myStore varchar2(10):='061';
begin
EXECUTE IMMEDIATE 'SELECT * FROM STORE#DBSTORE.P'||myStore||'081';
end;
Or maybe it should be like this?
declare
myStore varchar2(10):='061';
sStoreCode varchar2(10):='061';
begin
EXECUTE IMMEDIATE 'SELECT * FROM STORE#BNSTORE.P'||myStore
||'081 where storecode='||sStoreCode;
end;

Resources