Why can't I create a view inside of a BEGIN … END block

后端 未结 2 472
梦如初夏
梦如初夏 2020-12-31 05:32

This code does not work, returning the error:

BEGIN
  CREATE VIEW [dbo].[dummy] AS SELECT 1 AS Dummy
END
GO`

Incorrect syntax near the keyword \'VIEW\'.
         


        
2条回答
  •  抹茶落季
    2020-12-31 06:08

    It's because CREATE VIEW must be the first statement in a batch as described in this MSDN reference.

    Instead, you could do: e.g.

    .....
        BEGIN 
            EXECUTE('CREATE VIEW [dbo].[dummy] AS SELECT 1 AS Dummy')
        END
    

提交回复
热议问题