如何在字符串中分隔 SQL 变量

How to separate SQL variable inside a string

在我的 SQL 脚本中,我可以定义如下变量:

DEFINE x=100;

并像这样使用它:

CREATE TABLE A&x AS ...;

结果将是 CREATE TABLE A100 AS ...(连接的字符串)。

但我想在类似查询中得到CREATE TABLE A100B AS ...(B后缀):

CREATE TABLE A&xB AS ...;

但是Oracle/SQL明白有xB变量

如何在SQL中分隔变量(名称):

CREATE TABLE A&{x}B AS ...;

这不行!

(例如 ${x} 我 PHP)

它只是一个很小的 ​​

SQL> define x=100
SQL> create table a&x.b as select * from dual;
old   1: create table a&x.b as select * from dual
new   1: create table a100b as select * from dual

Table created.

SQL> select * from a100b;

D
-
X

SQL>

在这里,如果你错过了:

create table a&x.b
                ^
                |
               here

有什么意义? SQL加上连接字符。默认情况下,它是一个点。它告诉 SQLPlus 变量名结束的位置。


如果您需要更多帮助:

SQL> help set

 SET
 ---

 Sets a system variable to alter the SQL*Plus environment settings
 for your current session. For example, to:
     -   set the display width for data
     -   customize HTML formatting
     -   enable or disable printing of column headings
     -   set the number of lines per page

 SET system_variable value

 where system_variable and value represent one of the following clauses:

   APPI[NFO]{OFF|ON|text}                   NEWP[AGE] {1|n|NONE}
   ARRAY[SIZE] {15|n}                       NULL text
   AUTO[COMMIT] {OFF|ON|IMM[EDIATE]|n}      NUMF[ORMAT] format
   <snip>
   ---------------------
   CON[CAT] {.|c|ON|OFF}                      [FOR[MAT]  {WRA[PPED] |
   ---------------------
   Here it is!