How does Management Studio know to save my T-SQL c

2019-06-21 05:45发布

问题:

I'm confused about the way that SQL Server Management Studio saves the comments of views, stored procedures, etc.

Let's say I'm altering a view and I put some comments in before the ALTER statement:

USE [SomeDatabase]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- Let's add some comments about this view!
ALTER VIEW [dbo].[MyView]
AS
SELECT Stuff
FROM   TableOfStuff
-- To get the other stuff, we have to do an inner join
INNER JOIN OtherStuff
ON     TableOfStuff.OtherKey = OtherStuff.StuffKey

GO

When I run the above code in Management Studio, my view will be altered AND the comments will be saved. If I later do a Script View As --> ALTER TO --> New Query Window, the comments will reappear.

So how does Management Studio know that those comments 'belong with' the view? Does it have something to do with SET QUOTED_IDENTIFIER ON?

回答1:

What ever is between the GO before ALTER VIEW and the GO after ALTER View will be saved

Go is a batch terminator, so everything between those 2 GO statements is a batch, and that is what is sent



回答2:

It's not management studio but sql server it self

Whatever you create in your server (index, SP, whatever), sql server will save the text you used to create the object.