ITPub博客

首页 > 数据库 > Oracle > [20240930]关于sqlplus的define相关问题.txt

[20240930]关于sqlplus的define相关问题.txt

原创 Oracle 作者:lfree 时间:2024-09-30 19:45:36 0 删除 编辑

[20240930]关于sqlplus的define相关问题.txt

--//最近在sqlplus下写sql脚本,遇到define相关问题,做一个记录。

1.环境:
SCOTT@book01p> @ver2
==============================
PORT_STRING                   : x86_64/Linux 2.4.xx
VERSION                       : 21.0.0.0.0
BANNER                        : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
BANNER_FULL                   : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
BANNER_LEGACY                 : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
CON_ID                        : 0
PL/SQL procedure successfully completed.

2.问题1:
--//定义数字,而实际上类型却是字符类型。
SCOTT@book01p> define v='4'
SCOTT@book01p> define v
DEFINE V               = "4" (CHAR)

SCOTT@book01p> define v=2
SCOTT@book01p> define v
DEFINE V               = "2" (CHAR)
--//视乎不能直接赋值number。

2.问题2:
--//继续:
SCOTT@book01p> column deptno new_value v
SCOTT@book01p> select deptno  from dept where deptno=20;

    DEPTNO
----------
        20

SCOTT@book01p> define v
DEFINE V               =         20 (NUMBER)

SCOTT@book01p> set numw 20
SCOTT@book01p> define v
DEFINE V               =                   20 (NUMBER)

--//注意define显示的宽度前面有1串空格,受显示数字宽度的影响。
--//结果导致sql语句前面有许多i空格。

SCOTT@book01p> set echo on
SCOTT@book01p> set verify  on
SCOTT@book01p> select &v from dual ;
old   1: select &v from dual
new   1: select                   20 from dual

                  20
--------------------
                  20

3.如果已经定义为数字,加入单引号输出:

SCOTT@book01p> select '&v' v1 , '20' v2  from dual ;
old   1: select '&v' v1 , '20' v2  from dual
new   1: select '                  20' v1 , '20' v2  from dual

V1                   V2
-------------------- --
                  20 20

--//前者前面替换后存在空格。
--//这些在实际的编写代码中很容易出现困惑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-3035000/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7970381