Exporting data from essbase through FDMEE - mdx

The main objective is to extract data from essbase through FDMEE.
i have written a script called extract_data.scr and placed it in the FDMEE server.
extract_data.scr:
spool on to "G:\\LOGFILE_$3.log";
set column_width 200;
/* to make spool file cleaner, return only warnings */
set message level warning;
set column_separator ~;
set column_header off;
set echo_mode off;
login $1 identified by $2 on $4;
spool on to "G:\\output.csv";
SELECT
Crossjoin(
{[Z1],[Z2],[Z3]},
{[M1]}) ON COLUMNS,
{[X1],[X2],[X3]} ON ROWS
FROM [CUBE]
WHERE [CONDITIONS];
spool off;
logout;
exit;
i can trigger the above script from cmd using below:
essmsh G:\extract_data.scr.scr test_user pass12343 CUBE ESSBASE_HOST
the above runs perfectly and spools the essbase ouput to a csv file. but i need to execute the same script through FDMEE.

Related

How to have spool file display result of query? (Having trouble)

I am trying to export a .csv file (spool file) that has the result set of a very simple query that I am running in Oracle SQL Developer. The spool file generates; however, only the query is displayed (select * FROM TABLE) with no result set. What am i doing wrong? The command I am using is as follows:
spool "C:\Temp\test.csv"
select * from table;
spool off;
Thanks in advance
Use below commands to get the output of the query in spool file
SET SERVEROUTPUT ON
SET ECHO ON
After executing the select query don't forget to spool off.
So i did some more research / experimenting and i found that the following works:
I first created a sql file with the appropriate sql script /command and placed it in a directory (C:\TEMP). Then i ran the following command:
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
set verify off
SET ECHO OFF
spool "c:\Temp\test.csv"
#c:\Temp\test.sql as script(F5);
spool off
However now i run into a road block where oracle throws me an error saying that only 5,000 rows are currently supposed in script results...
Edit: I created the above code as a .sql file (test2.sql) and ran the below script. But am still encountering the 5000 row error:
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
set verify off
SET ECHO OFF
spool "c:\Temp\test2.csv"
#c:\Temp\test2.sql as script(F5);
spool off
The following worked when I tried to increase the limit: I went up to my sql developer tool bar ( tools > prefs > database > worksheet) and was able to change the maximum output limit.

Append to spool file Oracle

I have one script file called Test.sql in D:\Scripts folder and the content of the file is given below
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Test.log;
SELECT USER_NAME FROM TUP_USER WHERE USER_ID=1432;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
I normally execute this by opening command prompt, locate to D:\Scripts and give sqlplus username/password#Database and then give #test.sql to execute this and it will generate a log file called Test.log
Every time I execute this, it replaces the old file with the new data. I need to append new data to the file using spool. Is there a way to do that?
Any help would be appreciated. Thanks in advance.
Finally got the solution for this!
Add append after Test.log
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Test.log append;
SELECT USER_NAME FROM TUP_USER WHERE USER_ID=1432;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
Just add append when you're writing the spool query:
spool d:\lab1.txt append;

Unix to Sqlplus - Log the SQL statement about to execute and result of execution

I am on AIX using ksh. Created a unix script which generates the CREATE OR REPLACE VIEW ... and GRANT .. statements, which are placed in a single .txt file. Now I need to execute the contents in the file (there are around 300 view creation and grant statements) in Oracle and I need to log the sql statement that is about to execute and the feedback of Oracle whether the view is created or not..
My excerpt goes as
sqlplus -s username/password#servername <<EOF
SET ECHO ON;
SET NEWPAGE 0;
SET PAGESIZE 0;
SET LINESIZE 200;
SET LONG 10000000;
SET TRIMSPOOL ON;
SET HEADING OFF;
SET FEEDBACK ON;
SET VERIFY ON;
SET TERMOUT OFF;
SET SQLBLANKLINES ON;
SPOOL ${drctry}/${v_timestamp}_sql_execution_log.txt
#${drctry}/${v_date}_sql_statements.txt
SPOOL OFF;
EXIT;
EOF
If the contents of the file _${v_date}sql_statements.txt is
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
GRANT SELECT ON V_TABLE1 TO USER1;
...
CREATE OR REPLACE VIEW V_TABLE300 AS SELECT * FROM TABLE300;
GRANT SELECT ON V_TABLE300 TO USER300;
Expected output:
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
View created
GRANT SELECT ON V_TABLE1 TO USER1;
Grant succeeded
...
CREATE OR REPLACE VIEW V_TABLE300 AS SELECT * FROM TABLE300;
View created
GRANT SELECT ON V_TABLE300 TO USER300;
Grant succeeded
After some search, noticed List option; But it only records only the last statement that was executed if we have more than 1 statement, which doesn't fit here. In Teradata Bteq the ECHOREQuired attribute can be set to ON for this task. But I am not sure in Oracle. Also tried
sqlplus -s username/password#servername <<EOF > ${drctry}/${v_timestamp}_unix_sql_log.txt But still no luck. Will change the password hardcode once I overcome this issue;
The -s[ilent] option:
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.
So by including the -s flag you are overriding the set echo on directive.
If you omit that then the commands are echoed in the spool file, but (a) you also see the banner on stdout and (b) you see the SQL> prompt and the script name spool off echoed too. You can fix the first part by redirecting output, with the risk you may miss something you care about if there is a problem, by changing your shell command to do:
sqlplus -s username/password#servername >/dev/null <<EOF
And you can partly fix the spooled output by changing the prompt, adding:
SET SQLPROMPT ""
With those changes the shell sees no output, and the spool file contains:
#/path/to/v_date_sql_statements.txt
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
View created.
GRANT SELECT ON V_TABLE1 TO USER1;
Grant succeeded.
...
SPOOL OFF;
You could potentially post-process that to remove the first and last line, and (if you want) blank lines. You can also hide the script name by including the text file in the heredoc instead of using start/#:
SPOOL ${drctry}/${v_timestamp}_sql_execution_log.txt
`cat ${drctry}/${v_date}_sql_statements.txt`
SPOOL OFF;
I made a sqplus bash script to do that.
link to github: https://github.com/javierB-/ORACLE.git
It create two files, one with the sql sentences, and the other with the sql sentences and their output.
The script core is:
tee -a $FOUT $FOUT_TRAZA | sqlplus $# | tee -a $FOUT
I use the script throught the alias:
alias sqlpluss='$ADMIN_HOME/oracle/sqlpluss.sh'
it's in spanish, i have traslated the output to write here:
sqlpluss
USE ONLY INTERACTIVELY:
sqlplus enriched to save input and output trace.
Use:
sqlpluss [-f fout] user/password#conection #scripts ...
(fout will be opened in add mode and it will default to yyyymmdd.log)
summary:
tee -a fout | sqlplus $# | tee -a fout
the full script its too long to paste here, but if someone want, i'll do it.

Deleting last blank lines in SPOOL with DBMS_OUTPUT

Using SPOOL and PL/SQL in SQL Developer (I must not use SQL Plus via command line) I need to create a file with some info. The problem is that at the end of the file I'm getting 2 blank lines with are causing me trouble when importing this file somewhere else.
Using the next code (called externally to avoid sql statements):
SET SERVEROUTPUT ON;
SET TERMOUT OFF;
SET HEAD OFF;
SET VERIFY OFF;
SET FEEDBACK OFF;
SPOOL 'C:\myfile.csv'
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello');
END;
/
SPOOL OFF;
I'm getting:
Hello
(blank line)
(blank line)
I can handle having 0 or 1 blank line, but not 2. I have tried SET TRIMSPOOL ON but SQL Developer skips this command and others too (TRIM, TRIMS, SQLBLANKLINES)
It really does look like it is not possible to remove that extra CRLF in SQL Developer. I can only suggest using SQLPlus which doesn't add that extra CRLF. If you absolutely can't use that, consider using sed to strip that last line.
For any further discussion and to save future readers' time, here are all the options I tried without success:
SET SERVEROUTPUT ON TERMOUT OFF HEAD OFF FEEDBACK OFF VERIFY OFF
SET ECHO OFF NEWPAGE NONE PAGESIZE 0 RECSEP OFF --TRIMSPOOL ON
SPOOL 'myfile.txt'
--EXEC DBMS_OUTPUT.PUT_LINE('Hello')
SELECT 'Hello' FROM DUAL;
SPOOL OFF
ED myfile.txt

Deleting empty spool file when no data

I have a sql script which creates a spool file.
When I do not have data, I am getting an empty file created. I do not want the file to get created in that case.
How can I do this?
set termout off;
set newpage 0;
set space 0;
set linesize 255;
set pagesize 0;
set echo off;
set feedback off;
set heading off;
set verify off;
set trimspool on;
UNDEFINE p_xml_filename
DEFINE p_xml_filename=&1
spool &p_xml_filename
SELECT * FROM emp;
spool off;
set feedback on
set verify on
set heading on
set echo on
exit
Instead of handling it in SQL*Plus, I would rather do it at OS level. In your scenario, you want to spool the file only if data exists. But, to do that, you need to check the COUNT of rows in the table, thus it is an overhead to the process.
Alternatively, I would:
Let the spool happen always
Do a grep of the spool file to check the content
Remove the file if no content found. rm filename