SQL SERVER – Correct Syntax for Stored Procedure SP

Just a day ago, I received interesting question about correct syntax for Stored Procedure. Many readers of this blog will think that it is very simple question. The reason this is interesting is the question behavior of BEGIN … END statements and GO command in Stored Procedure.

Let us first see what is correct syntax.

Correct Syntax:
CREATE PROCEDURE usp_SelectRecord
AS
BEGIN
SELECT
*
FROM TABLE
END
GO

I have seen many new developers write statements after END statement. This will not work but will probably execute first fine when stored procedure is created. Rule is anything between BEGIN and END will be created as part of the stored procedure and will work fine. However, anything after GO statement will be not part of Stored Procedure. Nesting of BEGIN … END statement is allowed in Stored Procedure.

Incorrect Syntax : (This may not throw an error)

CREATE PROCEDURE usp_SelectRecord
AS
BEGIN
SELECT
*
FROM TABLE
END
GO
SELECT *
FROM TABLE

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Stored Procedure
Previous Post
SQL SERVER – 2005 – List All Stored Procedure in Database
Next Post
SQL SERVER – Database Interview Questions and Answers Complete List

Related Posts

51 Comments. Leave new

  • Query to find duplicate tables in the database

    Select * from sys.tables where name in (Select name from sys.tables group by name having COUNT (*) = 1)

    Reply
  • which is correct to make changes in stored procedure or to drop the created procedure and create a new one?
    If so what is the syntax to alter the stored procedure?

    Reply
  • I need the exact syntax explanation for the stored proccedure..?
    Could you help me out on this.

    Reply
  • i want to know how to clear the expression of non boolian type specified context where condition is expected near begin?

    Reply
  • Thanks a lot

    Reply
  • kalpesh parakh
    December 2, 2012 8:46 am

    It is he most helpful pos I have ever seen…. Thanx to u all who is makin it easy for begginers to understand sql

    Reply
  • i want to understand concept of stored procedure for sql and what is difference for stored procedure in sql vs oracle

    Reply
  • thanks

    Reply
  • Milind Patel
    April 1, 2013 7:33 am

    give me a proper definition of store procedure please replay?

    Reply
  • I am totally beginner to PL /SQl, and learning Procedures, but can identify when to use IS and when AS, (or both are same) in following syntax-

    CREATE [OR REPLACE] PROCEDURE procedure_name
    [(parameter_name [IN | OUT | IN OUT] type [, …])]
    {IS | AS}
    BEGIN

    END procedure_name;

    Plz help. Thank you.

    Reply
  • hello Pinal,

    how to see script of table through query not with wizard (not like generate script of table)

    Reply

Leave a Reply