5

I have one question. I was creating below procedure temporary.

When I Execute it in below format it works fine:

CREATE PROCEDURE Get_TableList_By_Name
@Proc_Name VARCHAR(255)
AS
BEGIN
    SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%'
END 
GO
EXEC Get_TableList_By_Name 'norway'
GO
DROP PROCEDURE Get_TableList_By_Name 
GO

But when I execute same SQL in below format it giving error saying: "Incorrect syntax near 'GO'."

CREATE PROCEDURE Get_TableList_By_Name @Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%' END GO EXEC Get_TableList_By_Name 'norway' GO DROP PROCEDURE Get_TableList_By_Name GO

CREATE PROCEDURE Get_TableList_By_Name @Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%' END GO 1 EXEC Get_TableList_By_Name 'norway' GO 1 DROP PROCEDURE Get_TableList_By_Name GO 1

How to write same SQL with GO statement in single line? Is it possible? If not then Why?

Thanks, Vishal

3 Answers 3

13

From GO (Transact-SQL)

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

So Go needs to be on its own line, except for comments.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Astander. So there is no way to write it on single line?
There cannot be a comment after GO in the same line either ...
4

'GO' is not a SQL command. It is a batch terminator recognized by tools like Query Analyzer, SSMS, SQLCMD, etc. These tools generally require the GO to be on a separate line and send the preceding SQL statements as a batch to SQL Server when the GO is encountered

GO Statement must be written in new line as it is not T-SQL command. T-SQL statement can not occupy the same line as GO. GO statement can contain comments.

Comments

0

You can execute this:

EXEC   SP_COMMAND 1,2;
EXEC   SP_COMMAND 2,2;
EXEC   SP_COMMAND 3,2;

Set EXEC Bebore sp and ';' for end statement then select all code (SSMS) and press f5 o exec all you code by other thecnic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.