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 sugges
Hi,
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
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 -
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 -
How to start/stop process flow from sql*plus?
Hi,
i know how to start a process flow via sqlplus_exec_template.sql, but i cannot find any information on how to stop (and rollback) a working flow from sql*plus. Any help would be appreciated.
Greetings
Christoph
Message was edited by:
ctrierweilerHi,
I've had a go.
How should I interpret the results of list_requests:
owner_owr@ORKDEV01> @list_requests
====================
DEPLOYMENTS
====================
Audit ID Status Name Date Owner
2706 READY Deployment Fri Nov 11-NOV-05 10:49:59 OWNER_OWR
11 10:46:37 CET 2
005
====================
DEPLOYMENT UNITS
====================
Audit ID Status Name Date Owner
2707 READY Unit0 11-NOV-05 10:49:59 OWNER_OWR
====================
EXECUTIONS
====================
Er zijn geen rijen geselecteerd.
owner_owr@ORKDEV01>
Whilst a process flow is executing the last query will list executions, all of which have status BUSY:
owner_owr@ORKDEV01> @list_requests
====================
DEPLOYMENTS
====================
Audit ID Status Name Date Owner
2706 READY Deployment Fri Nov 11-NOV-05 10:49:59 OWNER_OWR
11 10:46:37 CET 2
005
====================
DEPLOYMENT UNITS
====================
Audit ID Status Name Date Owner
2707 READY Unit0 11-NOV-05 10:49:59 OWNER_OWR
====================
EXECUTIONS
====================
Audit ID Status Name Date Owner
394512 BUSY PF_ONB01 04-MEI-06 09:11:12 OWNER_OWX
395328 BUSY ONB:FULL_PREPARE 04-MEI-06 09:11:55 OWNER_OWR
395324 BUSY PF_ONB01:ONB 04-MEI-06 09:11:55 OWNER_OWR
owner_owr@ORKDEV01>
As an aside, I will attempt to get rid of the READY deployment and deployment unit using deactive_deployment.sql
Now, if I attempt to use deactivate_execution.sql on any of the executions with status BUSY I get:
owner_owr@ORKDEV01> @deactivate_execution
Voer waarde voor 1 in: 396136
declare
FOUT in regel 1:
.ORA-20003: The object is not in a valid state for the requested operation
ORA-06512: at "OWNER_OWR.WB_RTI_EXCEPTIONS", line 94
ORA-06512: at "OWNER_OWR.WB_RTI_EXECUTION", line 774
ORA-06512: at "OWNER_OWR.WB_RT_EXECUTION", line 90
ORA-06512: at line 4
owner_owr@ORKDEV01>
So all the seems to remain is to use abort_exec_request.sql
This does the job, but the script itself hangs.
I think it has to do with the l_stream_id not being checked again after the initial IF. I think it should probably be part of the loop condition as it is again reset in the do_acks inside the loop.
Cheers & thanks,
Colin -
How to retreive this file in SQL*Plus
If I upload files which name is KOREAN, name of files is invisible.
I dont't know cause of this problem.
How is keeping korean name of file from breaking?
Also, answer how to retreive this file in SQL*Plus
Thanks
silverbellTo access ifs data from SQL, you need to (officially) setup a 'user view'. Refer to the Java API documentation on how to do this. Once you have created the user view, then you may access this view using any SQL tool.
-
How to Generate Trace Files in SQL*Plus
Hi Friends ,
How to Generate Trace Files in SQL*Plus ?
i have no idea
thanks
rajWhat trace files would you like to generate?
Are we talking SQL trace files?
ALTER SESSION SET sql_trace = TRUE;This will be generated in the user_dump_dest on the server.
show parameter dump -
How to find session information in sql plus
Can someone please tell me how to find session information in sql plus? I specifically want to know my privileges for the current session. Thanks in advance.
SELECT * FROM session_privs;
SELECT * FROM session_roles; -
How to invoke multiple sessions of sql*plus thru pl/sql program
Hi
How to invoke multiple sessions of sql*plus thru pl/sql program.
ThanksHow to invoke sql*plus in a procedure?????
I have to invoke more pl/sql sessions?????No you don't "have to".
Look at what you are trying to do.
You have a program running inside the PL/SQL engine. This is running nicely inside the Oracle database and is perfectly capable of issuing other SQL statements, PL/SQL programs etc. inside it's nice cosy Oracle environment.
You are asking for this PL/SQL to shell out to the operating system, run an external application, for which it will have to supply a username and password (are you planning on hard coding those into your PL/SQL?), and then that external application is supposed to run more SQL or PL/SQL against the database.
a) Why hold all this code external to the database when it can quite happily reside on the database itself and be executed through jobs or whatever.
b) Consider what would happen if someone were to replace the external application with their own program of the same file name... they'd be able to capture the username and password for connecting to the database, therefore a major security flaw.
The whole idea of doing what you want through external calls to SQL*Plus is ridiculous. -
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
/ -
How to Eliminate Special Character in SQL LOADER Script
How to eliminate special character from SQL LOADER script file which suppose not to insert in TABLE
example.CSV lile like this
<ABC/ , 7747>
<DEF/ , 7763>
<NEW/ , 7779>
<OLD/, 7795>
I have to remove < > and / character at the time of loading into table. How It could be done. It is not possible to remove < , > , / character manually from CSV fileOn Unix/Linux that's very easy, on Windows... I don't know...
$ cat myfile.csv
<ABC/ , 7747>
<DEF/ , 7763>
<NEW/ , 7779>
<OLD/, 7795>
$ tr -d "\057\074\076" <myfile.csv >outfile.csv
$ cat outfile.csv
ABC , 7747
DEF , 7763
NEW , 7779
OLD, 7795
$ -
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> -
How to return a value from sql plus activity
Hi,
I want to return a value from sqlplus activity to a processflow variable.
SQL PLUS activity has a property :"RESULT_CODE", whenever i run the process flow this value is always reurned as 0.
in sqlplus activity i have written some pl/sql block....
for example
begin
end;
exit
i want to do something like
begin
if v=100 then
return 1
else
return 0;
end if;
end;
exit
can some please tell me how can i return value from this pl/sql block to proessflow.
Regards,
RD_RBStable ==> function
input param from table to function. ==> input mapping paramter to store the output from the mapping.
Will this now work. -
How to get Arabic dates in SQL*Plus?
I want to do a very simple thing.
I want to type in Arabic and Display dates in Arabic.
Instead i get ????? ??????? ????? ??????? ? ? ???????, ????? for this?
Why is this?
I then changed my Windows XP "Regional and Language Options" all to Arabic. Now, I get AR (Arabic) in my language selection bar at the bottom of the Desktop. When I choose AR I can type Arabic in Notepad. ُ
Even HERE I can type in Arabic: فغحث غخعق ةثسسشلث اثقث
But in Sql*PLus I get ????? ??????? ????? ??????? ? ? ???????, ?????
If I choose AR in SQL Plus and type in Arabic I get garbage characters?
How to get Arabic in Sql*Plus?
SQL> SHOW USER
USER is "ARABDTEST"
SQL> SELECT * FROM v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
SQL> SELECT * FROM nls_session_parameters;
PARAMETER VALUE
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY $
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
17 rows selected.
SQL> ALTER SESSION set NLS_DATE_LANGUAGE = 'ARABIC';
Session altered.
SQL> SELECT TO_CHAR(SYSDATE, 'RRRR/MM/DD YEAR MONTH DAY MON DY AM PM DL') FROM dual;
TO_CHAR(SYSDATE,'RRRR/MM/DDYEARMONTHDAYMONDYAMPMDL')
2013/04/08 TWENTY THIRTEEN ????? ??????? ????? ??????? ? ? ???????, ????? 08, 2013
SQL> ALTER SESSION set NLS_DATE_LANGUAGE = 'AMERICAN';
Session altered.
SQL> SELECT TO_CHAR(SYSDATE, 'RRRR/MM/DD YEAR MONTH DAY MON DY AM PM DL') FROM dual;
TO_CHAR(SYSDATE,'RRRR/MM/DDYEARMONTHDAYMONDYAMPMDL')
2013/04/08 TWENTY THIRTEEN APRIL MONDAY APR MON PM PM Monday, April 08, 2013
SQL> SELECT * FROM nls_database_parameters;
PARAMETER VALUE
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CHARACTERSET WE8MSWIN1252
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY $
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_RDBMS_VERSION 11.1.0.6.0
20 rows selected.I am not sure SQL*Plus in character mode can display Arabic characters on Windows. OTN NLS_LANG FAQ http://www.oracle.com/technetwork/products/globalization/nls-lang-099431.html#_Toc105389288 says:
>
if you are testing with "special" characters please DO use the GUI and not the "DOS box" sqlplus.exe !
>
SQL*Plus GUI is not available with 11G: you should use SQL Developer. -
How to cancel a search in SQL*Plus
Once a search is initiated in SQL*Plus Worksheet, how can you stop it? I am on Windows NT. The normal Ctrl-C, Ctrl-Break, Esc don't work. I found an obscure reference in the SQL*Plus documentation about a cancel function, but it didn't give the keystrokes.
I actually managed to crash an 8.0.5 database by hitting Ctrl-C in the middle of a query.
Oracle Support advised me that this was a known bug. The solution?
Don't hit Ctrl-C in the middle of a query!
null
Maybe you are looking for
-
Apple Remote not working with Mac Mini
Hi there, I woke up to quite a strange problem today... My Apple Remote is no longer working with my Mac Mini! I tried all the controls, including launching front row, and the volume controls - but no go... I even tried it in Windows BootCamp under i
-
I have a Belkin 4-port USB hub that I plug my external harddrive, ipod, and printer into, which I then connect to my powerbook, all via USB. But, whenever I leave the hub plugged into my laptop and then shut my computer's monitor, putting it to sleep
-
When first using photo stream I was told I could store my photos and they would stay till I moved them. Now a lot of my holiday photos have disappeared. Can I recover them?
-
Issue with opening Deski in standalone mode through command line
Hi, I am using the below command to open deski in the background, in standalone mode. I keep getting the login prompt. Please suggest the correction that needs to be done "...\busobj.exe" -user "" -pass "" -nologo -auth "standalone" -system ""
-
"Purchase history" tab is not working on Skype Web...
Hi, I want to download some old invoices from my Skype account and for that I am logging into my account on Skype website and goging to 'Payment Settings -> Purchase History' but it says: Oops, something went wrong...We're having technical problems.