Migrating SQL Plus Scripts to APEX
Hi, all:
I am trying to migrate some sql scripts which generates spool files as reports from sqlplus.
I am trying to use APEX 4.2.1 interactive report pages to provide similar but much powerful functions.
Database version 10.2.0.5.
Seems Apex IR only accept SELECT .... queries for the region source.
But the original scripts used in sqlplus do other things such as create a temporary table,
then query based on this table and generate spool files.
I would like to know is there way to simulate this table creation in APEX before handover
the process to region source query to continue?
I saw APEX sql workshop can create / save / run scripts, I guess somehow I need to call
saved scripts to create temp tables before handover to IR page to do query.
Any suggestions?
Thanks,
John
You can indeed not use a temporary table. If you create and fill one in one process, there is no guarantee that the data you inserted will still be there at the moment the IR is rendered (the select against the temp table, in another process/region) due to the connection pooling apex uses. A collection would indeed be the only way. (A normal table does work of course)
Similar Messages
-
How to detect client OS from SQL*Plus script
Sometimes in a SQL*Plus script I need to execute OS commands e.g.
host rm tempfile.bufHowever of course Windows has no "rm" command by default, so I have to edit the script to use
host del tempfile.bufNow if I could define &DELETE (for example, "cat"/"type" is another) as a substitution variable, I could just use
host &DELETE tempfile.bufMaybe I need more coffee but all I could come up with was something like this:
def rm=rm
def cat=cat
spool sqlplus_windows_defs.cmd
prompt echo def rm=del
prompt echo def cat=type
spool off
host .\sqlplus_windows_defs > sqlplus_windows_defs.sql
@sqlplus_windows_defs.sql
host &rm sqlplus_windows_defs.cmd
host &rm sqlplus_windows_defs.sqlthe idea being that you first define the variables for nix ("rm" and "cat"), then attempt to create and execute a Windows command file containing DOS versions ("dele" and "type"), which does not run under nix. Unfortunately the OS failure message (".sqlplus_windows_defs: not found" in Unix) appears on the screen despite SET TERM OFF, so I'm back where I started.
I know there are various ways to get the server OS, and you can get the SQL*Plus version with &_SQLPLUS_RELEASE and so on, but I can't see a way to determine the client OS. Any suggestions?Thanks guys. This seems to work in Windows XP - will try on Unix when I get a chance:
col DELETE_COMMAND new_value DELETE_COMMAND
col LIST_COMMAND new_value LIST_COMMAND
def list_command = TYPE
def delete_command = DEL
SELECT DECODE(os,'MSWIN','TYPE','cat') AS list_command
, DECODE(os,'MSWIN','DEL','rm') AS delete_command
FROM ( SELECT CASE WHEN UPPER(program) LIKE '%.EXE' THEN 'MSWIN' END AS os
FROM v$session
WHERE audsid = SYS_CONTEXT('userenv','sessionid') );
host &LIST_COMMAND xplan_errors.lst
host &DELETE_COMMAND xplan_errors.lstIf the user doesn't have access to v$session it will just default to the Windows commands.
http://www.williamrobertson.net/code/xplan.sql -
Execute SQL*PLUS script from VB6?
I'm using VB6 to connect to oracle 10 with oracle client 8i, I can establish the connection without any problems.
If I run a SQL statement from VB it works prefect, but when I try to run a SQL Plus script from VB I get error ORA-00900, "invalid SQL statement". Everything works fine in oracle SQLPLUS.
Is there anything I have missed about running SQL*PLUS script from VB6? Is'nt it the same as a SQL statement?Thanks for you answer Justin,
This is a short variant of SQL*Plus I wanna execute from VB and transfer the result into a listview in VB.
If this is possible from VB, can you show me how I should do?
COLUMN customer_1 NEW_VALUE customer_1 NOPRINT
COLUMN customer_2 NEW_VALUE customer_2 NOPRINT
SELECT customer customer_1
FROM (
SELECT customer,
ROW_NUMBER() OVER(PARTITION BY customer ORDER BY CustomerTotalSales DESC) CustomerTotalSalesRank
FROM (
SELECT customer,
SUM(SalesQty) OVER(PARTITION BY customer) CustomerTotalSales
FROM CustTable
WHERE year_month BETWEEN 200709 AND 200801
WHERE CustomerTotalSalesRank = 1
AND ROWNUM = 1
SELECT customer customer_2
FROM (
SELECT customer,
ROW_NUMBER() OVER(PARTITION BY customer ORDER BY CustomerTotalSales DESC) CustomerTotalSalesRank
FROM (
SELECT customer,
SUM(SalesQty) OVER(PARTITION BY customer) CustomerTotalSales
FROM CustTable
WHERE year_month BETWEEN 200709 AND 200801
WHERE CustomerTotalSalesRank = 2
AND ROWNUM = 1
SET VERIFY OFF
SELECT year_month,
SUM(
CASE CustomerTotalSalesRank
WHEN 1
THEN
SalesQty
ELSE
NULL;
END
) "&customer_1",
SUM(
CASE CustomerTotalSalesRank
WHEN 2
THEN
SalesQty
ELSE
NULL;
END
) "&customer_2"
FROM (
SELECT year_month,
SalesQty,
ROW_NUMBER() OVER(PARTITION BY customer ORDER BY CustomerTotalSales DESC) CustomerTotalSalesRank
FROM (
SELECT customer,
year_month,
SalesQty,
SUM(SalesQty) OVER(PARTITION BY customer) CustomerTotalSales
FROM CustTable
WHERE year_month BETWEEN 200709 AND 200801
WHERE CustomerTotalSalesRank <= 10
GROUP BY year_month
ORDER BY year_month
/ -
SQL*Plus script with a dynamic SPOOL File Location?
Anyone,
I have a numbr of SQL Plus scripts that I need to run many times but each run against a different database. Each script SPOOLS the output like this:
spool c:\temp\BUILD_ASSET.lis
spool c:\temp\BUILD_WORKORDER.lis
etc
(each spool is a seperate script run)
The spool is at the top of each BUILD SQL Script.
Example:
set time on
SET FEEDBACK ON
SET ECHO ON
SET TIMING ON
spool c:\temp\BUILD_ASSET.lisBut I need each run to go into its own directory based on the database I am connected to. Like this:
CONNECT DB1
@C:\temp\BUILD_ASSET.SQL =====> spool c:\temp\DB1\BUILD_ASSET.lis
@c:\temp\BUILD_WORKORDER.SQL =====> spool c:\temp\DB1\BUILD_WORKORDER.lis
etc
CONNECT DB2
@C:\temp\BUILD_ASSET.SQL =====> spool c:\temp\DB2\BUILD_ASSET.lis
@c:\temp\BUILD_WORKORDER.SQL =====> spool c:\temp\DB2\BUILD_WORKORDER.lisIs there a way to dynaically code this without having to create a version of the BUILD scripts for every single DB I connect to?
I would like to have one big script that executes all the individual BUILD scripts.
Seems very easy in concept but I can not see an easy way. I would appreciate any help I can get.
Thanks in advance,
Millercolumn db new_Value db
select sys_context('userenv','db_name') db from dual;
spool c:\temp\&db\file.lis -
SQL Plus Script Execution Time
Anyone,
Is there an easy way to start a timer inside a SQL Plus script that shows exact run time at end of script completion?
Right now I use the TIME option and have a time as the prompt start and then one at end but would like if the SQL Plus script could simply put a begin/end/run time for me as last output. TIMING is to much since I have many things execute and do not wnat to see every individual runtime.
Thanks in advance,
Millerhttp://download-east.oracle.com/docs/cd/B19306_01/server.102/b14357/ch12047.htm#i2699704
You can use TIMING with a named timer
SQL> timing start script_time
SQL> select 1 from dual;
1
1
SQL> /
1
1
SQL> /
1
1
SQL> timing stop
timing for: script_time
Elapsed: 00:00:07.37
SQL> -
My scripts all contain relative file paths e.g.
@@../directory1/file1.sql
Is there a way of setting the current working directory in SQL Developer to make this work?
In SQL*Plus for windows you simply opened the file but this does not seem to work.As I said, run them in SQL*Plus.
SQL*Developer is a development tool and database browser, not a SQLPLus clone.
It has its own methods of extracting data which may or may not suit your output requirements. You don't need col statements as much if you are viewing data in a grid, for example. [1]
I don't know what you use the sqlplus scripts for, but they could be replaced over time with some combination of Apex, SQLDeveloper reports and straight sqldeveloper queries.
As for SQL*PLus being greyed out on the menu, there are some known issues about that. I think one of them is that you need to be using tns connections. However, the sqlplus menu item is gone in sqldeveloper 1.5 so don't rely on it. Learn to run if from the command line.
[1] Despite that, I would arguethat SQLDeveloper should support all (with tiny exceptions) SQLPlus commands. Not because I see it as a sqlplus replacement, but so it can be used to develop scripts which can then be run in SQL*Plus without amendment (without the hassle of switching back and forth during development) -
How to control logic flow in SQL Plus script
Hi
I have the requirement below:
Before I start running an SQL script, I need to ensure that I am connecting as a particular user. If I am not that user, then the script should show a msg "Error - Invalid User - Log in as <<username>>"
Let's say i am executing a script A.sql.
SQL > @<path>a.sql
But before this script is executed -- i shall check for the user:
block begins...
select user into var1 from dual
if upper(var1) != 'ABCUSER' then
-- display the error message and stop execution of this script
else
@<path>a.sql (*it executes what i want :) )
end if;
block ends...
How do I write this code using an SQL script ? Pls suggesHi,
As Centinul said, SQL*Plus is poorly equipped to deal with problems like this. If possible, try to do what you want outside of SQL*Plus.
If you really have to do it in SQL*Plus, here are two ideas:
(1) run a script conditionally
(2) deliberately raise an error
(1) Using SQL*Plus substitution variables, you can decide which of several scripts to run as a sub-script, based on the results of some query.
For example, if a.sql is the script that only user ABCUSER is allowed to run, then write two other very short scripts:
(a) warning.sql, such as the following:
-- warning.sql
PROMPT You are not authorized to do this.
QUIT(b) step_1.sql, which decides whether a.sql or warning.sql should be run, and does it:
-- step_1.sql - Decide whether a.sql or warning.sql is to be run next, and do it.
-- (a) decide:
COLUMN script_name_col NEW_VALUE script_name
SELECT CASE
WHEN USER = 'ABCUSER'
THEN 'a.sql'
ELSE 'warning.sql'
END AS script_name_col
FROM dual;
-- do:
@&script_nameYou may want to use @@, or specify a complete path name.
(2) Deliberately raise an error, either in a separate script or in a.sql itself, like this:
WHENEVER SQLERROR EXIT
SELECT CASE
WHEN USER = 'ABCUSER'
THEN NULL
ELSE TO_NUMBER ('This will raise ORA-01722')
END AS "Welcome!"
FROM dual;
WHENEVER SQLERROR NONE
-- continue with the original a.sql -
Detecting clientside NLS_LANG setting in SQL*Plus script
Our company has developed an application that uses an Oracle database as it backend. Companies can buy our application and run it against their own Oracle installation.
A new version of our application often requires an update to the Oracle database schema.
Together with new application files we send our customers an update.sql script they must run against their database using SQL*Plus.
This update script may contain international characters like é for example:
UPDATE SHOPS SET DESCRIPTION = 'Café'
WHERE SHOP_ID=1We save the SQL-script file in windows-1252 encoding so the character é is encoded as 0xE9 in the script file.
When the DBA running the upgrade script has set NLS_LANG=.WE8MSWIN1252 this works perfectly.
When the DBA has set his NLS_LANG=US7ASCII the é gets replaced by an upside-down question mark. Other NLS_LANG settings can also be troublesome.
To prevent this we give very clear installation instructions. However, nobody reads them. This leads to two questions:
A) Is there a way to prevent this problem so I do not depend on the NLS_LANG setting by the DBA running the upgrade script?
B) Otherwise: is there a way to test for the NLS_LANG setting inside the script and abort if it is not WE8MSWIN1252?
Info:
Database is 10.2.0.3/10.2.0.4 and sometimes 10.2.0.5, always database character set WE8MSWIN1252.
Client software: I do not know what each DBA uses but it will be mostly 10g client software
Client OS: Some DBA's will be running the script from Windows, others from some Unix variety. I cannot control that.I agree with Sergiusz. You're much better off if you can remove character set dependencies from your scripts by using commands like this.
UPDATE SHOPS SET DESCRIPTION = unistr( 'Caf\00E9' )
WHERE SHOP_ID=1 ;If that's not possible however you can try this approach to determine the session's character set.
SQL> column USERENV_LANGUAGE format a30
SQL> column oracle_charset format a20
SQL> r
1 select
2 userenv_language
3 , substr
4 ( userenv_language
5 , instr( userenv_language, '.' ) + 1
6 ) as oracle_charset
7 from
8 ( select sys_context( 'USERENV', 'LANGUAGE' ) as userenv_language
9 from dual
10* )
USERENV_LANGUAGE ORACLE_CHARSET
AMERICAN_AMERICA.AL32UTF8 AL32UTF8 --
Joe -
Semicolon and / in SQL Plus scripts?
Anyone,
Seem to have some confusion over the use of / and ; inside PL SQL Scripts run in SQL Plus.
I seem to get two commits thereby two rows on INSERT clase that has a ; and a /.
i.e.
/* Insert record into table for recording statistics on the runtime of this script */
INSERT INTO MYTABLE ( col1, col2) VALUES ( value1, value2);
COMMIT;
/The above will get two of the same rows in the table. Is this an issue with SQL Plus settings? What do people typically use? I have a combination of SQL and DDL in my scripts and I need / for the DDL typically as I understand. How do othere intermix these and what standard is used.Dave, here are the very basics.
The SQL language does not have command separators as only a single command at a time can be issued. Thus the following is invalid SQL:
SELECT * FROM emp;
The semicolon as command separator (or terminator) is not valid SQL. This is valid SQL:
SELECT * FROM emp
PL/SQL is a programming language similar to Pascal, C and Java. Multiple commands are used in a program. These need to be separated so that the parser/compiler can know where a command starts and where it ends. In PL/SQL the semicolon is used.
The following is invalid PL/SQL as it is missing command separators:
declare
i integer
begin
i := 1234
endThe following is valid PL/SQL :
declare
i integer;
begin
i := 1234;
end;SQL*Plus is an Oracle CLI (command line interface) client. It can submit both SQL and PL/SQL to the database. It needs to know when you have stopped entering commands into its input buffer and to submit what you've entered to the database.
SQL*Plus uses two characters for this. The semicolon and the forward slash. If you want to submit the above SELECT to Oracle using SQL*Plus, SQL*Plus needs to know when to submit its input buffer's content - thus:
SQL> SELECT * FROM emp;
Or:
SQL> SELECT * FROM emp
SQL> /
When using PL/SQL in SQL*Plus, SQL*Plus "understands" that the semicolons you use are for the PL/SQL language - not an instruction from you to it to submit its buffer to Oracle for execution.
The forward slash can also be at anytime used to resubmit the current SQL*Plus input buffer again. E.g.
SQL> SELECT * FROM emp;
.. now do the last SQL (or PL/SQL) in the buffer again
SQL> /[i] -
How to use bind vars in SQL*Plus Script?
I am trying to use a bind var in my script ... does not seem to work ... something along these lines.
variable v_id number;
select max(user_id) into :v_id
from user_id_tbl;
exec trace_user(:v_id);
This is in a UNIX Script - I don't get any errors but the binding is not working - not passing a valid v_id ... Any ideas?For the record a solution is:
variable v_id number;
begin
select max(user_id) into :v_id from user_id_tbl;
end;
exec trace_user(:v_id);
-- CJ -
Migrating SQL Script to stored procedure...
I created a SQL script file to run in SQLPlus to
generate a fixed-format text file (See EDI_PrepForSend
below)
How do i do the same thing in a stored
package/procedure? Can I utilize the
script I already have working? I need to
do it in a stored procedure because I need
to do dynamic queries.
TIA
Chuck Plinta
[email protected]
++++++++++++++++++++++++++
-- EDI_PrepForSend.sql
SET CONCAT '#';
SET ESCAPE OFF;
SET TERM OFF
-- Set's for flat file
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 50
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET VERIFY OFF
SPOOL o:\ClientData\EDI-Send\&2\EDI-HCFA-Dtl-&1-&3.txt
COLUMN MC_Number FORMAT A15 HEADING MC_Number
COLUMN MC_Line_Number FORMAT A3 HEADING Ln#
COLUMN MC_CPT_Code FORMAT A5 HEADING Code
COLUMN MC_CPT_Modifier FORMAT A2 HEADING Mod
COLUMN MC_FDOS FORMAT A8 HEADING FDOS
COLUMN MC_LDOS FORMAT A8 HEADING LDOS
COLUMN MC_Line_Charge FORMAT 99999.99 HEADING Charge
SELECT MC.ICN MC_Number,
'???' MC_Line_Number,
MC.CPT_Code MC_CPT_Code,
MC.CPT_Modifier_1 MC_CPT_Modifier,
TO_CHAR(MC.First_DOS,'YYYYMMDD') MC_FDOS,
TO_CHAR(MC.Last_DOS,'YYYYMMDD') MC_LDOS,
MC.Charge MC_Line_Charge
FROM &1.Decision_Support DS,
&1.Medical_Claims MC
WHERE DS.Reprice_Status = 'U' AND
MC.CID = DS.CID
ORDER BY MC.ICN, MC.CID;
SPOOL OFFYou need to put your select statement in a parameterised function that returns a REF CURSOR. This is all well documented in the PL/SQL users guide.
Then you amend your SQL*PLUS script to call the function
VAR rc REFCURSOR
EXEC :rc := yr_function('&1')
PRINT rc
Cheers, APC -
Replacement variables SQL Plus x PLSQL x APEX
How Can I use replacement variables SQL Plus, integrating with APEX ,PLSQL?
This isn't going very well is it?
Perhaps it has something to do with a lack of knowledge of the english language, that you don't explain your case more, but I'll try to rephrase your question:
You would like to call a query in SQL*PLUS from an Apex application?
Then my answer would be simple: you can't.
But my return question would be: why would you want that?
You can also run your query in your Apex application and show the result on a page. Why is there a need to run it in SQL*PLUS? -
IF statement in SQL*Plus - how to do it
Hi,
In SQL*Plus script, I would like to keep conditional checking (IF statement) and proceed. For example, whatever is done in PL/SQL block below, want to do the same in SQL*Plus script, I know partly it can be done using VARIABLE keyword, conditional checking can be done using DECODE in SELECT statement, but I want to carry out a more complex requirement, hence I want to use IF statement somehow in SQL*Plus.
Another question, how to do spooling in PL/SQL script, it can be done using UTL_FILE, any other option is there to achieve this.
declare
v_ind_count int;
begin
select count(1) into v_ind_count from user_indexes where index_name = 'index_object_name';
IF v_ind_count > 0
THEN
dbms_output.put_line('index found');
ELSE
dbms_output.put_line('index does not exist');
END IF;
end;
/Hello,
SQL*PLUS has no scripting language. It can only execute SQL and PL/SQL scripts. There are some commands like SPOOL or SET but no commands for conditional statements. You should describe your requirements, maybe we can find a way.
Or you can search the forum, maybe your question has already been answered
[Google for SQL*PLUS + condition|https://www.google.de/search?q=site%3Aforums.oracle.com+"SQL*PLUS"+condition]
# {message:id=4189517}
# {message:id=4105290}
how to do spooling in PL/SQL scriptFrom within PL/SQL you can use dbms_output, the spool has to be started by the calling SQL script when it is executed in SQL*PLUS. Or you can use utl_file, but then you can only write to a server directory, not into a client file. To give an advice we need more information about what you want to do.
Regards
Marcus -
SQL*Plus - how to suppress the SQL in a spool file
This is my SQL*Plus script. I thought I had solved the problem, but it is back now and I don't know what I am missing. But I don't want the query at the top of the file.
SET SERVEROUTPUT ON
SET MARKUP HTML ON -SILENT
SET ECHO OFF
SET PAGESIZE 33
SET TERMOUT OFF
Spool C:\DuaneWilson.xls
SELECT *
FROM RPT_DS1_CNT_CAT_vw
WHERE ROWNUM <=100
ORDER BY CVBI_KEY;
SET MARKUP HTML OFF
SET ECHO ON
SET PAGESIZE 20
SET TERMOUT ON
SET SERVEROUTPUT OFFIt turns out when I run the script with the @ or Start with the file name, there is no SQL put out to the file. But when I just copy the text out of the file and run it at the prompt, the SQL appears in the output file. In reference to the -SILENT, I put that in after the MARKUP statement and got an error. Maybe I don't know where that goes. And I am not sure why there is a difference if it is run as a script or just pasted to the buffer. At least it should be the same in the output file, I would think.
-
Seek help to format spool file from SQL*PLUS
I am running a Unix shell script to call a Oracle 11g SQL script from a Oracle database. In this SQL script, I need to connect to many different remote databases to select data, then sool these records as one big text file to a directory. Then email the file to related Group users. In the spool file, there is a line on the top of each page like this:
DUMMY
DB_NAME
I know this is caused by connect to remote database in SQL*PLUS. My connection string is like this:
Conn system/password@Oracle_SID
How can I remove these lines or how to skip these lines into spool file? Please advise. Thanks in advnce. Wish all of you Happy New Year!!!Hi,
It sounds like you have some kind of formatting (such as SQL*Plus TTITLE) producing the output you don't want. If that's the case, temporarily stopping the spooling might not help you. Find out what is causing the output that you don't want. You say that you know it is caused by the CONNECT statements, but it must be more than that. I've written scripts with CONNECT statements that don't have anything like what you reported at the top of each page; in fact, they don't even have pages: the output is one continuous stream. Find out what's putting the unwanted output there, and that will be a big clue as to how you can stop it.
You say that you know the unwanted titles are there because of the CONNECT statements. If so, use database links instead of CONNECT. You don't have to use dbms_scheduler or utl_file; just eliminate the CONNECT statements. (I'm not saying that there's anything wrong with dbms_scheduler or utl_file; you should definitely investigate those tools. I'm just saying that using database links is independent of them.)
What would happen if you did all your connecting to different databases at the OS level? Can you write a shell script that connects to each database in turn, and runs a SQL*Plus script in each one. Each SQL*Plus script would have a SPOOL or SPOOL ... APPEND command, or maybe you could build the SPOOL into a LOGIN.SQL script.
Maybe you are looking for
-
Help! Another "Entry Point Not Found" Error
Hello, I purchased iPhone 4 earlier this week and went to synch it last night. Apple Updates updated and crashed several times, now iTunes won't even start. I already had it set up for my iPod, my iPad, and a prior iPhone, so just plugged my new iPho
-
Not a computer person but I'm wanting to get photoshop for my Chromebook and need to know what to buy?
-
Facing problem in parameters of the Query
Hi All, I made 1 query through query generation ..... The Query withput parameters is working fine but when i have applied selection criteria's like Date wise,Item Wise,Document wise,sales employee wise it is getting executed only when all the parame
-
Error -50, what does that mean?
up until recently I've been able to export movies out of Imovie, no problem. Now it's coming up as (error -50) and won't let me re-publish or export to itunes, youtube, quicktime, anywhere. I've tried restarting, rebooting, and even making more roo
-
How to Create the Function Module Exit
Hi, How to Create the Function Module Exit. Please tell me the step by step procedure.