在Oracle SQL中什么时候需要使用分号而不是斜杠?

y3bcpkx1  于 2022-12-11  发布在  Oracle
关注(0)|答案(9)|浏览(145)

We have been having some debate this week at my company as to how we should write our SQL scripts.
Background: Our database is Oracle 10g (upgrading to 11 soon). Our DBA team uses SQLPlus in order to deploy our scripts to production.
Now, we had a deploy recently that failed because it had used both a semicolon and a forward slash ( / ). The semicolon was at the end of each statement and the slash was between statements.

alter table foo.bar drop constraint bar1;
/
alter table foo.can drop constraint can1;
/

There were some triggers being added later on in the script, some views created as well as some stored procedures. Having both the ; and the / caused each statement to run twice causing errors (especially on the inserts, which needed to be unique).
In SQL Developer this does not happen, in TOAD this does not happen. If you run certain commands they will not work without the / in them.
In PL/SQL if you have a subprogram (DECLARE, BEGIN, END) the semicolon used will be considered as part of the subprogram, so you have to use the slash.
So my question is this: If your database is Oracle, what is the proper way to write your SQL script? Since you know that your DB is Oracle should you always use the / ?

1tuwyuhd

1tuwyuhd1#

I know this is an old thread, but I just stumbled upon it and I feel this has not been explained completely.
There is a huge difference in SQL*Plus between the meaning of a / and a ; because they work differently.
The ; ends a SQL statement, whereas the / executes whatever is in the current "buffer". So when you use a ;and a / the statement is actually executed twice.
You can easily see that using a / after running a statement:

SQL*Plus: Release 11.2.0.1.0 Production on Wed Apr 18 12:37:20 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning and OLAP options

SQL> drop table foo;

Table dropped.

SQL> /
drop table foo
           *
ERROR at line 1:
ORA-00942: table or view does not exist

In this case one actually notices the error.
But assuming there is a SQL script like this:

drop table foo;
/

And this is run from within SQL*Plus then this will be very confusing:

SQL*Plus: Release 11.2.0.1.0 Production on Wed Apr 18 12:38:05 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning and OLAP options

SQL> @drop

Table dropped.

drop table foo
           *
ERROR at line 1:
ORA-00942: table or view does not exist

The / is mainly required in order to run statements that have embedded ; like CREATE PROCEDURE , CREATE FUNCTION , CREATE PACKAGE statements and for any BEGIN...END blocks.

8fsztsew

8fsztsew2#

我想澄清一下;/之间的更多用法
在SQLPLUS中:

  1. ;表示“终止当前语句,执行该语句并将其存储到SQLPLUS缓冲区”
  2. <newline>在D.M.L.(SELECT、UPDATE、INSERT...)语句或某些类型的D.D.L(创建表和视图)语句(不包含;)之后,这意味着将语句存储到缓冲区但不运行它。
  3. /表示“在缓冲区中运行D.M.L.或D.D.L.或PL/SQL。
  4. RUNR是sqlsplus命令,用于显示/输出缓冲区中的SQL并运行它。它不会终止SQL语句。
  5. /在输入D.M.L.或D.D.L.或PL/SQL期间表示“终止当前语句,执行它并将其存储到SQLPLUS缓冲区”

**注:**由于;用于PL/SQL结束语句,因此SQLPLUS不能使用;来表示“终止当前语句,执行它并将其存储到SQLPLUS缓冲区”,因为我们希望整个PL/SQL块完全位于缓冲区中,然后执行它。PL/SQL块必须以以下内容结束:

END;
/
agxfikkp

agxfikkp3#

这是一个偏好的问题,但我更喜欢看到始终使用斜杠的脚本-这样,所有的工作“单元”(创建PL/SQL对象、运行PL/SQL匿名块和执行DML语句)都可以更容易地用肉眼分辨出来。
此外,如果您最终使用Ant之类的工具进行部署,它将简化目标的定义,使其具有一致的语句分隔符。

6pp0gazn

6pp0gazn4#

几乎所有的Oracle部署都是通过SQLPlus(DBA使用的奇怪的小命令行工具)完成的。在SQLPlus中,一个单独的斜杠基本上意味着“重新执行我刚刚执行的最后一个SQL或PL/SQL命令”。
请参阅
http://ss64.com/ora/syntax-sqlplus.html
经验法则是在执行BEGIN .. END或可以使用CREATE OR REPLACE的情况下使用斜杠。
对于需要唯一的插入件,请使用

INSERT INTO my_table ()
SELECT <values to be inserted>
FROM dual
WHERE NOT EXISTS (SELECT 
                  FROM my_table
                  WHERE <identify data that you are trying to insert>)
kgsdhlau

kgsdhlau5#

From my understanding, all the SQL statement don't need forward slash as they will run automatically at the end of semicolons, including DDL, DML, DCL and TCL statements.
For other PL/SQL blocks, including Procedures, Functions, Packages and Triggers, because they are multiple line programs, Oracle need a way to know when to run the block, so we have to write a forward slash at the end of each block to let Oracle run it.

ygya80vv

ygya80vv6#

我只在每个脚本的末尾使用一次正斜杠,告诉sqlplus没有更多的代码行。在脚本的中间,我不使用斜杠。

at0kjp5o

at0kjp5o7#

use semicolon in sql script files to separate sql statements that tell client software (SQLPlus, SQL Developer) what are the single statements to be executed.
use slash in sql script files to separate pl/sql blocks that tell client software (SQL
Plus, SQL Developer) what are the single pl/sql blocks to be executed.
use slash in SQL*Plus command line when you want to execute buffered statement (yes it is a single sql statement without the semicolon or pl/sql block without the slash).

egmofgnx

egmofgnx8#

在以“end;“,否则不使用它。

rggaifut

rggaifut9#

在Oracle中建立对象类型之前,会建立虚拟类型来参照尚未定义的其他类型。斜扛用于执行最近的类型定义变更,或以取代的类型取代SQL缓冲区中的现有类型。
参考:https://docs.oracle.com/cd/E18283_01/server.112/e16604/ch_twelve004.htm

相关问题