"배치"란 무엇이며 GO가 사용되는 이유는 무엇입니까?
MSDN 등을 읽고 읽었습니다. Ok, 배치의 끝을 알립니다.
배치를 정의하는 것은 무엇입니까? 여러 스크립트를 모두 동시에 실행하기 위해 붙여 넣을 때 왜 가야하는지 알 수 없습니다.
나는 GO를 이해하지 못했습니다. 누구든지 이것을 더 잘 설명하고 사용해야 할 때 (얼마나 많은 유형의 거래 후)?
예를 들어 각 업데이트 후에 왜 GO가 필요한가요?
UPDATE [Country]
SET [CountryCode] = 'IL'
WHERE code = 'IL'
GO
UPDATE [Country]
SET [CountryCode] = 'PT'
WHERE code = 'PT'
GO
제대로 TSQL 명령 이 아닙니다 .
대신 SQL 서버에 연결 하는 특정 클라이언트 프로그램에 대한 명령입니다 (Sybase 또는 Microsoft-Oracle의 기능에 대해 잘 모름). "go"까지 필요할 때까지 입력 된 명령 세트를 클라이언트 프로그램에 알립니다. 실행할 서버로 전송됩니다.
왜 / 언제 필요합니까?
MS SQL 서버의 GO에는 "count"매개 변수가 있으므로 "repeat N times"바로 가기로 사용할 수 있습니다.
매우 큰 업데이트는 SQL 서버의 로그를 채울 수 있습니다. 이를 피하기 위해을 통해 더 작은 배치로 분리해야 할 수도 있습니다
go
.예를 들어, 국가 코드 세트에 대한 업데이트에 로그 공간이 부족한 볼륨이있는 경우 해결책은 각 국가 코드를 별도의 트랜잭션으로 분리하는 것입니다
go
.이 코드 는 클라이언트에서로 분리하여 수행 할 수 있습니다 .작동하려면 일부 SQL 문을 다음 명령문과 GO로 구분해야합니다.
예를 들어, 최소한 Sybase (프로 시저 / 트리거 작성을위한 ditto)에서 단일 트랜잭션에서 테이블을 삭제하고 동일한 이름의 테이블을 다시 작성할 수 없습니다.
> drop table tempdb.guest.x1 > create table tempdb.guest.x1 (a int) > go Msg 2714, Level 16, State 1 Server 'SYBDEV', Line 2 There is already an object named 'x1' in the database. > drop table tempdb.guest.x1 > go > create table tempdb.guest.x1 (a int) > go >
GO
명령문이 아니라 배치 구분 기호입니다.
구분 된 블록 GO
은 처리를 위해 클라이언트에서 서버로 전송되며 클라이언트는 결과를 기다립니다.
예를 들어
DELETE FROM a
DELETE FROM b
DELETE FROM c
, 이것은 단일 행 3
쿼리 로 서버에 전송됩니다 .
당신이 쓰는 경우
DELETE FROM a
GO
DELETE FROM b
GO
DELETE FROM c
, 이것은 3
한 줄 쿼리 로 서버에 전송됩니다 .
GO
자체는 서버로 이동하지 않습니다. 그것은 순수 클라이언트 측 예약어입니다 만 인식 SSMS
하고 osql
.
사용자 정의 조회 도구를 사용하여 연결을 통해 보내면 서버는이를 인식하지 못하고 오류를 발행합니다.
많은 명령은 자체 배치에 있어야합니다. CREATE PROCEDURE
또는 테이블에 열을 추가하면 자체 배치에 있어야합니다. 동일한 배치에서 새 열을 SELECT하려고하면 구문 분석 / 컴파일시 열이 없기 때문에 실패합니다.
GO is used by the SQL tools to work this out from one script: it is not a SQL keyword and is not recognised by the engine.
These are 2 concrete examples of day to day usage of batches.
Edit: In your example, you don't need GO...
Edit 2, example. You can't drop, create and permission in one batch... not least, where is the end of the stored procedure?
IF OBJECT_ID ('dbo.uspDoStuff') IS NOT NULL
DROP PROCEDURE dbo.uspDoStuff
GO
CREATE PROCEDURE dbo.uspDoStuff
AS
SELECT Something From ATable
GO
GRANT EXECUTE ON dbo.uspDoStuff TO RoleSomeOne
GO
Sometimes there is a need to execute the same command or set of commands over and over again. This may be to insert or update test data or it may be to put a load on your server for performance testing. Whatever the need the easiest way to do this is to setup a while loop and execute your code, but in SQL 2005 there is an even easier way to do this.
Let's say you want to create a test table and load it with 1000 records. You could issue the following command and it will run the same command 1000 times:
CREATE TABLE dbo.TEST (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO
INSERT INTO dbo.TEST (ROWID) VALUES (NEWID())
GO 1000
source: http://www.mssqltips.com/tip.asp?tip=1216
Other than that it marks the "end" of an SQL block (e.g. in a stored procedure)... Meaning you're on a "clean" state again... e.G: Parameters used in the statement before the code are reset (not defined anymore)
As everyone already said, "GO" is not part of T-SQL. "GO" is a batch separator in SSMS, a client application used to submit queries to the database. This means that declared variables and table variables will not persist from code before the "GO" to code following it.
In fact, GO is simply the default word used by SSMS. This can be changed in the options if you want. For a bit of fun, change the option on someone else's system to use "SELECT" as a batch seperator instead of "GO". Forgive my cruel chuckle.
It is used to split logical blocks. Your code is interpreted into sql command line and this indicate next block of code.
But it could be used as recursive statement with specific number.
Try:
exec sp_who2
go 2
Some statement have to be delimited by GO:
use DB
create view thisViewCreationWillFail
참고URL : https://stackoverflow.com/questions/2668529/what-is-a-batch-and-why-is-go-used
'programing tip' 카테고리의 다른 글
왜 (0-6)이 -6 = False입니까? (0) | 2020.07.02 |
---|---|
포트는 IPv6에서 어떻게 작동합니까? (0) | 2020.07.02 |
xcode4의 프레임 워크와 정적 라이브러리의 차이점과 호출 방법 (0) | 2020.07.01 |
__init__ 외부에 정의 된 인스턴스 속성 attribute_name (0) | 2020.07.01 |
BREW 설치 시간이 너무 많이 걸리는 gcc (0) | 2020.07.01 |