问题
I am trying to use a PL/SQL script in SQL*Plus to set the linesize and pagesize based on a developer input on whether a report should print in landscape or portrait orientation. If it is landscape, I want linesize 132 and pagesize 60. If it is portrait, I want 88 and 80 respectively.
I am trying to use substitution variables to do so like this:
DEFINE PRINT_ORIENTATION 'PORTRAIT'
COLUMN LINESIZE_VALUE NOPRINT new_value LINE
COLUMN PAGESIZE_VALUE NOPRINT new_value PAGE
SELECT DECODE('&PRINT_ORIENTATION', 'PORTRAIT', 88, 'LANDSCAPE', 132) AS LINE,
DECODE('&PRINT_ORIENTATION', 'PORTRAIT', 80, 'LANDSCAPE', 60) AS PAGE
FROM DUAL
/
SET LINESIZE &LINE
SET PAGESIZE &PAGE
However, the system then just prompts me to manually enter values for line and page. What do I need to do to use a substitution variable in the SET command?
回答1:
With the column .. new_value ... syntax:
COLUMN Q_LINE NEW_VALUE LINE
COLUMN Q_PAGE NEW_VALUE PAGE
SELECT DECODE('&PRINT_ORIENTATION', 'PORTRAIT', 88, 'LANDSCAPE', 132) AS Q_LINE,
DECODE('&PRINT_ORIENTATION', 'PORTRAIT', 80, 'LANDSCAPE', 60) AS Q_PAGE
FROM DUAL
/
SET LINESIZE &LINE
SET PAGESIZE &PAGE
That allows you to define a substitution value from a query result.
回答2:
With a "shell" substitution variable: What we do here to handle a similar issue, is we generate the correct sizing settings in a temporary sql file.
Not sure it'll help, but here's a hint on how to set the number of columns from the size of your terminal, with this in @$HOME/sqlmlogin.sql
:
def_editor=emacs
set null °
set arraysize 1
set pagesize 0
set serveroutput on
host > $HOME/sqltmp_size.sql ; tty -s && ( eval $( resize ) ; echo "set lines $COLUMNS\n set pages 0" >> $HOME/sql/tmp_size.sql )
-- then call the temp script
@$HOME/sqltmp_size.sql
-- remove afterwards
host rm -f $HOME/sqltmp_size.sql
And call sqlplus
like this:
sqlplus / @$HOME/sqlmlogin.sql
So you can also generate the static values you want for your users from a script like that.
来源:https://stackoverflow.com/questions/47353806/how-do-i-set-linesize-and-pagesize-with-a-substitution-variable