6

When I wrote some comments before the CREATE PROCEDURE command, those comments seem to be preserved as part of procedure definition.

For example, when I create procedure like this

/*Comment header*/
CREATE OR ALTER PROCEDURE example_procedure
AS 
SELECT 1 AS Id

The /*Comment header*/ is stored as part of definition.

Fiddle

Is that documented behavior, can I always count on that? Or is that just some unreliable SQL Server quirk?

My naive understanding is that the procedure definition starts with CREATE keyword, so I am hesitant to rely on behavior that goes against my intuition.

1 Answers1

12

Yes, as long as it's in the same batch. This is intuitive in the sense that the CREATE PROCEDURE statement is the whole batch (there can't be any other statements), so everything in that batch is included as part of the procedure.

-- This comment is NOT saved
GO

/* This comment is saved*/ CREATE OR ALTER PROCEDURE example_procedure -- This comment is saved AS SELECT 1 AS Id -- This comment is saved GO

-- This comment is NOT saved

I don't know that this is documented anywhere, but it has always worked this way, and would be a major breaking change to everyone's source code version control were this to be different, so it's very unlikely MS would ever modify this behaviour.

Views, triggers and function CREATE statements have the same effect.

Charlieface
  • 17,078
  • 22
  • 44