Do I have to write the word "GO" to execute the SQL server statement?

I have almost no experience with TSQL and SQL Server, so in MySQL, when I want to execute a statement, I just write:

Select * from users 

... and press ENTER.

However, I now see many SQL Server tutorials in which you have the word GO right after each statement. Should I write this? For instance:

 Select * from users; GO 

Or I can just write:

 Select * from users; <enter key pressed...> 
+4
source share
5 answers

In SQL Server, go separates query packets. It can be used in most cases.

In earlier versions of SQL Server, you had to go after modifying the table, for example:

 alter table MyTable add MyColumn int go select MyColumn from MyTable 

If you have not done so, SQL Server will analyze the query package and complain that MyColumn does not exist. See MSDN :

SQL Server utilities interpret GO as a signal that they need the current batch of Transact-SQL statements in an instance of SQL Server. The current batch of statements consists of all statements entered since the last GO, or from the beginning of the session or script, if this is the first GO.

+7
source

GO separates parties, as Andomar wrote.

Some SQL statements (such as CREATE SCHEMA ) must be the first or only statements within a package. For example, MSDN claims

The CREATE PROCEDURE operation cannot be combined with other Transact-SQL in the same batch.

Local variables are also package limited and therefore not available after GO.

+2
source

Go is optional, no need to write this in your SQL statements.

+1
source

You don’t have to. What GO will do is execute each statement (at least on the Sql server).

0
source

As the other defendants in front of me said, YOU DON'T NEED A Go .

There is only one case when you should use it, and when you want to create a table or view it, and then select it.

For instance:

 create view MyView as select * from MyTable go select * from MyView 

Without Go , Sql Server will not do this because the select statement is invalid because the view does not currently exist.

0
source

Source: https://habr.com/ru/post/1345438/


All Articles