Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 263 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the use of GO in SQL Server Management Studio & Transact SQL?

#11
**tldr;** In most cases nowadays GO is mostly IMO optional. Using GO is best in LARGE transaction batches where you would have compiled many different scripts together in a large script and don't want errors where similar variables are used and so that parts of the transaction is committed to the server when desired instead of all of the script being rolled back due to an error.

LARGE TRANSACTION 1 --> Runs Successfully

GO; --> Is in the server

LARGE TRANSACTION 2 --> Runs Successfully

GO; --> Is in the server

LARGE TRANSACTION 3 --> Errors

GO; --> Without the other GO statements this would rollback Transaction 1 & 2


----------


Not sure the best way to provide this SO wise however I do feel like what I've read so far doesn't really sum it all up and include an example that I've come across.

As stated many times before GO simply "commits" a batch of commands to the server.

I think understanding sessions also helps with understanding the necessity (or optionality) of the GO statement.

(This is where my technicality may fail but the community will point it out and we can make this answer better)

Typically developers are working in a single session and typically just executing simple statements to the database. In this scenario GO is optional and really...all one would do is throw it at the end of their statements.

Where it becomes more helpful is probably an option given by [Jamshaid K.][1] where you would have many large transactions that you would want committed in turn instead of all transactions being rolled back when one fails.

The other scenario where this also becomes helpful (which is the only other spot I've experienced it) is where many small transactions are compiled into one large script. For example

Dev 1 makes script 1

Dev 2 makes script 2

Dev 1 makes script 3

In order to deploy them a python script is written to combine the scripts so Script Master = script1 + script 2 + script 3.

GO statements would be required in the 3 scripts otherwise there could be errors where the scripts use conflicting variables or if script 3 fails the transactions from scripts 1 and 2 would be rolled back.

Now this process is probably archaic given current CI/CD solutions out there now but that would probably be another scenario where I could see GO being helpful/expected.


[1]:

[To see links please register here]

Reply

#12
I use the `GO` keyword when I want a set of queries to get `committed` before heading on to the other queries.

*One* thing I can add is, when you have some variables declared before the `GO` command you will not be able to access those after the `GO` command. i.e

```
DECLARE @dt DateTime = GETDATE();
UPDATE MyTable SET UpdatedOn = @dt where mycondition = 'myvalue';
GO

-- Below query will raise an error saying the @dt is not declared.
UPDATE MySecondTable SET UpdatedOn = @dt where mycondition = 'myvalue'; -- Must declare the scalar variable "@dt".
GO
```
---
**Update**

I see, people requesting when to use the `Go` command, so I thought, I should add why I use the `Go` command in my queries.

When I have huge updates in the tables and I usually run these updates while going off from work (which means, I wouldn't be monitoring the queries), since it is convenient to come the next day and find the tables ready for other operations.

I use `Go` command when I need to run long operations and want to separate the queries and complete part of the transactions such as:
```
-- First Query
Update MyBigTable1 SET somecol1='someval1' where somecol2='someval2'
GO
-- Second Query
Update MyBigTable2 SET somecol1='someval1' where somecol2='someval2'
GO
-- Third Query
Update MyBigTable3 SET somecol1='someval1' where somecol2='someval2'
```
Executing above queries will individually commit the modifications without resulting in huge roll-back logs formation. Plus if something fails on third query, you know first 2 queries were properly executed and nothing would be rolled-back. So you do not need to spend more time updating/deleting the records again for the previously executed queries.

To ***sum*** it up in just one sentence, "I use the `GO` command as a check point as in the video games." If you fail after the check point (GO command), you do not need to start over, rather your game starts from the last check point.
Reply

#13
GO means asking SQL repeat this whatever number you add next to it. Just like saying in English; "Hey GO there 3 times.". Try below in SQL and the result will be rendering table 3 times.

SELECT * FROM Table
GO 3

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through