Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 GO

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2009-08-04 : 08:40:35
Hello,

I am creating the Tables, Constrains and Stored Procedures in this order on my database. I start with:

USE MyDatabase;
GO

And then I have the code to create the Tables, Constraints and SP.
Do I need to have GO after each Create Table or Alter Table (For Constraint), etc?

I never know very well how to use GO.
Usually i have USE as I mentioned or a GO at the end of all code.

Thanks,
Miguel

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-04 : 08:42:15
The parser will tell you.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-08-04 : 08:57:32
Hi


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.


USE Database;
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






-------------------------
R..
http://code.msdn.microsoft.com/SQLExamples/
http://msdn.microsoft.com/hi-in/library/bb500155(en-us).aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-08-04 : 09:47:18
also see

http://sqlblogcasts.com/blogs/madhivanan/archive/tags/GO/default.aspx
Go to Top of Page
   

- Advertisement -