I'm trying to use the CONNECT Command from Oracle inside one of my PL/SQL-Scripts. What I want to achieve is to be able to switch to a different context inside the script itself (e.g. login as user and then switch at some point to sys dba).
My current approach is the following:
sqlplus user/password@//database:1521/orcl @ complete.sql
Content of complete.sql
DECLARE
-- declare
v_scriptInserted varchar2(1);
v_booleanFalse varchar2(1) := '0';
BEGIN
SELECT CASE WHEN COUNT(*) >= 1 THEN 1 ELSE 0 END INTO v_scriptInserted FROM SCHEMA_HISTORY WHERE SCRIPT = '&v_SCRIPTNAME';
IF v_scriptInserted = booleanFalse THEN
-- do Stuff with user privilidges and after this is done switch to sysdba
CONNECT sys/oracle@database:1521/orcl AS SYSDBA
-- do magic here with sysdba
-- SWITCH CONTEXT BACK TO User
DISCONNECT;
END IF;
END;
/
However, no matter how I try to escape the CONNECT sequence the part :1521 will be misinterpreted as bind varibale (SP2-0552).
I already digged around stack overflow and found the related articles: - How to escape ":" in Oracle dynamic SQL and also have bind variables? - Oracle PL/SQL - How to escape colon (:), being misinterpreted for bind variable However no matter what I try nothing seems to work.
My current approaches had been:
CONNECT sys/oracle@database:1521/orcl AS SYSDBA
CONNECT sys/oracle@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=database)(PORT=1521))(CONNECT_DATA=(SID=orcl)) AS SYSDBA
CONNECT sys/oracle@database\:1521/orcl AS SYSDBA
SELECT 'sys/oracle@database' || ':' || '1521/orcl AS SYSDBA' INTO v_dest_connstring FROM DUAL; CONNECT 'v_dest_connstring'
CONNECT 'sys/oracle@database:1521/orcl AS SYSDBA'
CONNECT ''sys/oracle@database:1521/orcl AS SYSDBA''
CONNECT '''sys/oracle@database:1521/orcl AS SYSDBA'''
CONNECT "sys/oracle@database:1521/orcl AS SYSDBA"
CONNECT ""sys/oracle@database:1521/orcl AS SYSDBA""
CONNECT """sys/oracle@database:1521/orcl AS SYSDBA"""
Any help is highly appreciated.
Thanks,
==============================================
EDIT:
1) Idea with CONNECT sys/oracle@"database:1521/orcl"
1.1) with "
`CONNECT sys/oracle@"database:1521/orcl" AS SYSDBA
*
FEHLER in Zeile 16:
ORA-06550: Zeile 16, Spalte 5:
PLS-00103: Encountered the symbol "CONNECT"`
1.2) same with '
CONNECT sys/oracle@'database:1521/orcl' AS SYSDBA
*
FEHLER in Zeile 17:
ORA-06550: Zeile 17, Spalte 5:
PLS-00103: Encountered the symbol "CONNECT" when expecting one of the
following
2) Leaving the port as optional since 1521 is default:
CONNECT sys/oracle@database/orcl AS SYSDBA
*
FEHLER in Zeile 17:
ORA-06550: Zeile 17, Spalte 5:
PLS-00103: Encountered the symbol "CONNECT" when expecting one of the
following
3) Missed the last bracket with the @(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=database)(PORT=1521))(CONNECT_DATA=(SID=orcl))) AS SYSDBA
CONNECT sys/oracle@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=database)(PORT=1521))(CONNECT_DATA=(SID=orcl))) AS SYSDBA
*
FEHLER in Zeile 17:
ORA-06550: Zeile 17, Spalte 5:
PLS-00103: Encountered the symbol "CONNECT" when expecting one of the
following