Keywords: SQL*Plus | Column Width | Output Formatting | SET LINESIZE | COLUMN Command
Abstract: This technical paper provides an in-depth analysis of resolving column output truncation issues in Oracle SQL*Plus environment, focusing on the core functionality of SET LINESIZE command and its interaction with system console width. Through detailed code examples and configuration explanations, the article elaborates on effective methods for adjusting column display width, formatting specific data type columns, and utilizing COLUMN command for precise control. The paper also compares different configuration scenarios and offers complete solutions to optimize query result display.
Analysis of SQL*Plus Column Output Truncation Issues
During Oracle database management, column output truncation frequently occurs when executing queries in SQL*Plus, particularly when handling long text fields or complex DDL statements. Users initially attempted combinations of commands such as SET SERVEROUTPUT ON SIZE 1000000, SET LINESIZE 50000, set pagesize 50000, and set long 50000, but found that output results remained truncated. This situation typically stems from insufficient understanding of SQL*Plus output mechanisms.
Core Functionality of SET LINESIZE Command
Through detailed analysis, the SET LINESIZE command emerges as the key solution for column output truncation problems. This command sets the maximum width of SQL*Plus output lines, with syntax formats SET LINESIZE 200 or the abbreviated form SET LIN 200. When an appropriate line width is set, SQL*Plus attempts to display complete column content within the specified width.
However, an important limitation must be noted: in Windows command prompt environments, the "Screen Buffer Size Width" property of the console window overrides SQL*Plus LINESIZE settings. Even if LINESIZE is set to a large value, output will still automatically wrap or truncate if the actual console window width is insufficient. Therefore, both console window width and SQL*Plus line width settings need adjustment in practical applications.
Precise Control with COLUMN Command
For specific column width control, the COLUMN command provides a more refined solution. Using the format COLUMN col_name FORMAT Ax (where x represents the desired character length) allows individual setting of each column's display width. This method is particularly suitable for handling query results containing one or two extra-large columns, enabling display of column value summaries on the console screen.
For example, for a column named sql_text, the command COLUMN sql_text FORMAT A120 can set its display width to 120 characters. The advantage of this approach lies in enabling personalized settings based on different column characteristics, avoiding excessive overall line width increases due to single wide columns.
Comprehensive Configuration Solution
Based on practical application experience, the following comprehensive configuration is recommended to resolve column output truncation issues:
set termout off
set verify off
set trimspool on
set linesize 200
set longchunksize 200000
set long 200000
set pages 0
column txt format a120
This configuration combination ensures output completeness through synergistic effects of multiple parameters: set termout off disables terminal output to reduce interference; set verify off prevents variable substitution verification; set trimspool on removes trailing spaces in spool files; set longchunksize and set long jointly control retrieval and display size of LONG type columns; set pages 0 eliminates paginated display; finally, specific columns are formatted using the column command.
Data Types and Formatting Strategies
Different data type columns require distinct formatting strategies. For character type columns, use the FORMAT A format model followed by a number representing character width. For NUMBER type columns, numeric format models containing 9s can define display formats, with additional formatting elements like commas and dollar signs.
DATE type columns have default width determined by the database's NLS_DATE_FORMAT parameter, but this can be overridden using the COLUMN command. For large object types like LONG and LOB, their display width is controlled by the smaller value between SET LONGCHUNKSIZE and SET LONG.
WRAP Control and Text Wrapping
SQL*Plus SET WRAP variable controls text wrapping behavior for all columns. Default setting is ON, automatically wrapping column content to the next line when exceeding specified width. If set to OFF, exceeding content is truncated. The global WRAP setting can be overridden for specific columns using WRAPPED, WORD_WRAPPED, and TRUNCATED clauses of the COLUMN command.
The WORD_WRAPPED option is particularly useful, ensuring wrapping at word boundaries to avoid breaking words in the middle, thereby improving output readability. However, note that NCLOB, BLOB, BFILE, or multibyte CLOB columns cannot use the WORD_WRAPPED option.
Practical Application Scenarios and Best Practices
In actual database management work, appropriate column width settings are crucial when handling scenarios like DDL statement extraction and long text queries. A progressive configuration approach is recommended: first use appropriate SET LINESIZE values, then employ COLUMN command for precise adjustment of specific wide columns.
For query results needing file storage, combining SPOOL command with suitable formatting settings can generate clean output files. In Windows environments, always check and adjust the command prompt's screen buffer size, ensuring its width is not less than the LINESIZE value set in SQL*Plus.
By mastering these SQL*Plus output formatting techniques, database administrators can effectively resolve column output truncation issues, obtain complete and readable query results, thereby improving work efficiency and data analysis accuracy.