Oracle SQL에서 세미콜론과 슬래시를 언제 사용해야합니까?
우리는 이번 주에 회사에서 SQL 스크립트 작성 방법에 대해 토론했습니다.
배경 : 데이터베이스는 Oracle 10g입니다 (곧 11로 업그레이드). DBA 팀은 스크립트를 프로덕션에 배포하기 위해 SQLPlus를 사용합니다.
이제 세미콜론과 슬래시 ( /
)를 모두 사용했기 때문에 최근 배포에 실패했습니다 . 세미콜론은 각 문장의 끝에 있었고 슬래시는 문장 사이에있었습니다.
alter table foo.bar drop constraint bar1;
/
alter table foo.can drop constraint can1;
/
스크립트에서 나중에 추가 된 트리거, 일부 뷰 및 일부 저장 프로 시저가 생성되었습니다. ;
및 /
문 을 모두 사용하면 각 문이 두 번 실행되어 오류가 발생했습니다 (특히 고유 한 삽입에서).
SQL Developer에서는 이런 일이 발생하지 않으며 TOAD에서는 이런 일이 발생하지 않습니다. 특정 명령을 실행하면 명령이 없으면 작동하지 않습니다 /
.
PL / SQL에서 서브 프로그램 (DECLARE, BEGIN, END)이있는 경우 사용 된 세미콜론은 서브 프로그램의 일부로 간주되므로 슬래시를 사용해야합니다.
내 질문은 이것입니다 : 데이터베이스가 Oracle 인 경우 SQL 스크립트를 작성하는 올바른 방법은 무엇입니까? DB가 Oracle이라는 것을 알고 있으므로 항상 /
?
선호의 문제이지만 슬래시를 일관되게 사용하는 스크립트를 선호합니다.이 방식으로 모든 작업 단위 (PL / SQL 객체 생성, PL / SQL 익명 블록 실행 및 DML 문 실행)가 가능합니다. 눈으로 더 쉽게 골랐습니다.
또한 결국 배포를 위해 Ant와 같은 것으로 이동하면 대상 정의를 단순화하여 일관된 명령문 구분 기호를 갖습니다.
나는 이것이 오래된 실이라는 것을 알고 있지만 방금 넘어졌으며 이것이 완전히 설명되지 않았다고 생각합니다.
a /
와 a 의 의미는 ;
다르게 작동하므로 SQL * Plus에는 큰 차이가 있습니다.
;
단부는 SQL 명령문의 반면, /
현재에 실행하는대로 "버퍼". 당신은을 사용할 때 그래서 ;
및/
문이 실제로 두 번 실행됩니다.
/
명령문을 실행 한 후 다음 을 사용하면 쉽게 알 수 있습니다 .
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
이 경우 실제로 오류를 알 수 있습니다.
그러나 다음과 같은 SQL 스크립트가 있다고 가정합니다.
drop table foo;
/
그리고 이것은 SQL * Plus 내에서 실행되며 매우 혼란 스럽습니다.
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
은 /
주로 내장 한 문을 실행하기 위해 필요한 ;
유사한 CREATE PROCEDURE
문을.
나는 사이에 더 많은 사용을 분명히하고 싶었다 ;
./
SQLPLUS에서 :
;
"현재 명령문을 종료하고 실행하여 SQLPLUS 버퍼에 저장"을 의미합니다.<newline>
DML (SELECT, UPDATE, INSERT, ...) 문 또는 no를 포함하는 일부 유형의 DDL (Creating Tables and Views) 문 다음에;
, 문을 버퍼에 저장하지만 실행하지는 않습니다./
버퍼에 명령문을 입력 한 후 (공백 포함<newline>
)는 " 버퍼 에서 DML 또는 DDL 또는 PL / SQL을 실행합니다.RUN
orR
is a sqlsplus command to show/output the SQL in the buffer and run it. It will not terminate a SQL Statement./
during the entering of a D.M.L. or D.D.L. or PL/SQL means "terminate the current statement, execute it and store it to the SQLPLUS buffer"
NOTE: Because ;
are used for PL/SQL to end a statement ;
cannot be used by SQLPLUS to mean "terminate the current statement, execute it and store it to the SQLPLUS buffer" because we want the whole PL/SQL block to be completely in the buffer, then execute it. PL/SQL blocks must end with:
END;
/
Almost all Oracle deployments are done through SQL*Plus (that weird little command line tool that your DBA uses). And in SQL*Plus a lone slash basically means "re-execute last SQL or PL/SQL command that I just executed".
See
Rule of thumb would be to use slash with things that do BEGIN .. END
or where you can use CREATE OR REPLACE
.
For inserts that need to be unique use
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>)
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.
I only use the forward slash once at the end of each script, to tell sqlplus that there is not more lines of code. In the middle of a script, I do not use a slash.
'IT' 카테고리의 다른 글
NVIDIA NVML 드라이버 / 라이브러리 버전 불일치 (0) | 2020.05.25 |
---|---|
Java가 파일 이름과 이름이 다른 클래스를 컴파일 할 수있는 이유는 무엇입니까? (0) | 2020.05.25 |
UIButton에서 테두리를 만드는 방법은 무엇입니까? (0) | 2020.05.25 |
adb를 사용할 때 데이터 폴더에 대한 액세스가 거부되는 이유는 무엇입니까? (0) | 2020.05.25 |
둥근 사각형 텍스트 필드처럼 UITextview의 스타일을 지정하는 방법은 무엇입니까? (0) | 2020.05.25 |