问题
I'm trying to work around the SQLDeveloper linesize bug by writing my script to detect whether it's SQL*Plus or SQLDeveloper. Something like:
COLUMN SET_THE_LINE new_value TARGETLINESIZE noprint
SELECT DECODE ('&&_SQLPLUS_RELEASE.','',5,500) as SET_THE_LINE from dual;
SET LINESIZE &&TARGETLINESIZE
I know that SQL*Plus always sets the value of _SQLPLUS_RELEASE and, understandably, SQLDeveloper does not.
Unfortunately SQLDeveloper always prompts for the value of _SQLPLUS_RELEASE.
Any suggestions?
回答1:
I'm not familiar with an SQL Developer linesize bug, so not sure what the end result is supposed to be. But you can use sys_context for this:
select sys_context('USERENV', 'MODULE') from dual;
Which gives:
SYS_CONTEXT('USERENV','MODULE')
--------------------------------------------------------------------------------
SQL*Plus
... or:
SYS_CONTEXT('USERENV','MODULE')
-------------------------------
SQL Developer
So you can adapt what you have as:
column set_the_line new_value targetlinesize noprint
set termout off
select case sys_context('USERENV', 'MODULE')
when 'SQL Developer' then 5 else 500 end as set_the_line from dual;
set termout on
set linesize &&targetlinesize
And then test with
show linesize
which gives linesize 500
in SQL*Plus and linesize 5
in SQL Developer.
If you might not be connected yet in SQL*Plus, just define the value first; you don't even need to do anything special to hide the error from the select
beyond the set termout off
, though you could include a whenever sqlerror
just in case your login.sql
is setting it to exit - but then you maybe have to know to reset it afterwards.
define targetlinesize=500
whenever sqlerror continue
set termout off
column set_the_line new_value targetlinesize noprint
select case sys_context('USERENV', 'MODULE')
when 'SQL Developer' then 5 else 500 end as set_the_line from dual;
set termout on
set linesize &&targetlinesize
show linesize
The column
value will override it if the select succeeds, and not touch it if it fails. If I put that in a file called client.sql
and run it as:
sqlplus -s /nolog @client
I only get this output:
linesize 500
And the same thing runs in SQL Developer, giving linesize 5
again.
回答2:
You can query the executable name from v$session
select program from v$session where sid = sys_context('userenv','sid')
sql*plus for example will be sqlplus.exe
.
@alex's way is also good but module is easily manipulated and often used by application as an online monitoring tool. this is why i dont think should rely on it (i personally have a habit of always setting the module/action when performing long processes in pl/sql and it overrides the existing value).
when using the program keep in mind that it can also be easily manipulated by changing the executable name - it's called spoofing.
来源:https://stackoverflow.com/questions/16596918/how-can-my-sql-script-determine-whether-it-is-running-in-sqldeveloper-or-sqlplus