为什么我不能在 BEGIN ... END 块内创建视图 [英] Why can't I create a view inside of a BEGIN ... END block

查看:38
本文介绍了为什么我不能在 BEGIN ... END 块内创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码不起作用,返回错误:

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'.

为什么?

注意事项:

  • GO 语句的存在好像没什么区别

  • The presence of the GO statement seems to make no difference

内部语句在代码块分隔符之外工作正常.

The inner statement works fine outside of the code block delimiters.

这是更大查询的一部分,但照原样进行隔离测试在这里展示.

This is part of a larger query but tested in isolation just as it is presented here.

推荐答案

这是因为 CREATE VIEW 必须是批处理中的第一个语句,如 此 MSDN 参考.

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

这篇关于为什么我不能在 BEGIN ... END 块内创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆