About SQL Server 'GO'

本文详细介绍了SQL Server实用工具中使用的GO命令。GO不是Transact-SQL语句,而是被sqlcmd和osql等工具识别的命令,用于指示这些工具将当前批处理中的Transact-SQL语句发送到SQL Server实例进行执行。

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword. The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.

USE AdventureWorks2008R2;
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
GO -- @MyMsg is not valid after this GO ends the batch.

-- Yields an error because @MyMsg not declared in this batch.
PRINT @MyMsg
GO

SELECT @@VERSION;
-- Yields an error: Must be EXEC sp_who if not first statement in
-- batch.
sp_who
GO

SQL Server applications can send multiple Transact-SQL statements to an instance of SQL Server for execution as a batch. The statements in the batch are then compiled into a single execution plan. Programmers executing ad hoc statements in the SQL Server utilities, or building scripts of Transact-SQL statements to run through the SQL Server utilities, use GO to signal the end of a batch.

Applications based on the ODBC or OLE DB APIs receive a syntax error if they try to execute a GO command. The SQL Server utilities never send a GO command to the server.

Permissions

GO is a utility command that requires no permissions. It can be executed by any user.

Examples

The following example creates two batches. The first batch contains only a USE AdventureWorks2008R2 statement to set the database context. The remaining statements use a local variable. Therefore, all local variable declarations must be grouped in a single batch. This is done by not having a GO command until after the last statement that references the variable.

USE AdventureWorks2008R2;
GO
DECLARE @NmbrPeople int
SELECT @NmbrPeople = COUNT(*)
FROM Person.Person;
PRINT 'The number of people as of ' +
CAST(GETDATE() AS char(20)) + ' is ' +
CAST(@NmbrPeople AS char (10));
GO
### SQL Server Job Scheduling and Management Usage Guide #### Understanding SQL Server Agent Jobs SQL Server provides a robust framework for automating administrative tasks through SQL Server Agent jobs. These jobs can be scheduled to run at specific times, intervals, or triggered by certain events within the database environment[^1]. A SQL Server Agent job consists of one or more steps that define what actions should occur when the job runs. Each step specifies an action such as executing Transact-SQL scripts, running operating system commands (CmdExec), sending email notifications via Database Mail, etc. ```sql USE msdb; GO EXEC sp_add_job @job_name=N'MyFirstJob'; GO ``` This script creates a new empty job named 'MyFirstJob'. To add steps into this newly created job: ```sql USE msdb ; GO EXEC sp_add_jobstep @job_name=N'MyFirstJob', @step_id=1, @step_name=N'Setup Step', @subsystem=N'TSQL', @command=N'PRINT ''Hello World'';', @retry_attempts=0, @retry_interval=0; GO ``` The above T-SQL command adds a single step which prints "Hello World". The subsystem parameter indicates whether it is going to execute T-SQL code ('TSQL'), OS Command Line utility ('CmdExec') among others depending on requirements. To schedule these jobs so they automatically start without manual intervention: ```sql USE msdb ; GO EXEC dbo.sp_add_schedule @schedule_name = N'DailySchedule', @freq_type = 4, -- Daily frequency type @freq_interval = 1, @active_start_time = 233000 ; -- Start time HHMMSS format GO EXEC sp_attach_schedule @job_name = N'MyFirstJob', @schedule_name = N'DailySchedule'; GO ``` In addition to creating schedules directly tied with individual jobs like shown here, shared schedules also exist allowing multiple different jobs share same timing configuration reducing redundancy across configurations. For monitoring purposes after setting up automated processes using agent jobs, alerts based off severity levels or performance counters could notify administrators about potential issues before becoming critical problems affecting end users negatively impacting productivity throughout organizations relying heavily upon databases daily operations [^2]. --related questions-- 1. How does one configure advanced options available under each job step? 2. What are some best practices while designing complex workflows involving numerous interdependent jobs? 3. Can you explain how alerting works alongside job scheduling in detail please? 4. Is there any way we can export/import existing job definitions between instances easily?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值