gpt4 book ai didi

sql-server - 在架构内创建多个 View - SQL Server

转载 作者:行者123 更新时间:2023-12-04 12:26:47 25 4
gpt4 key购买 nike

如何在 CREATE SCHEMA 语句中创建多个 View ?

我想创建一个 SCHEMA,并在同一语句中在其中创建两个 View ,所以所有这些语句作为一个单元工作?一起成功或失败!

来自 MSDN:http://msdn.microsoft.com/en-us/library/ms189462.aspx

"CREATE SCHEMA can create a schema, the tables and views it contains, and GRANT, REVOKE, or DENY permissions on any securable in a single statement. CREATE SCHEMA transactions are atomic. If any error occurs during the execution of a CREATE SCHEMA statement, none of the specified securables are created and no permissions are granted."

,

我该怎么做?我试过这个:

CREATE SCHEMA [MYSCHEMA] AUTHORIZATION [dbo]
CREATE VIEW [VIEW1]
AS
SELECT [ID]
,[NAME]
FROM [dbo].[TABLE1]
/* Here is the Problem */
GO

CREATE VIEW [VIEW2]
AS
SELECT [ID]
,[NAME]
FROM [dbo].[TABLE2]
GO

如果我在第一个 View 创建之后包含一个 GO 语句,那么脚本会运行,但第二个 View VIEW2 是在 dbo 模式下创建的,不在 MYSCHEMA 下,也不作为一个单元运行。

如果我在第一次查看后删除了 GO,那么它会给出一个错误提示

CREATE VIEW must be the first statement of a batch

第二个 CREATE VIEW 语句。

如何解决这个问题并创建两个 View 作为 CREATE SCHEMA 语句的一部分?

最佳答案

CREATE SCHEMA [MYSCHEMA] AUTHORIZATION [dbo]

CREATE VIEW [VIEW1] AS SELECT [ID], [NAME] FROM [dbo].[TABLE1]
CREATE VIEW [VIEW2] AS SELECT [ID], [NAME] FROM [dbo].[TABLE2]
GO

关于sql-server - 在架构内创建多个 View - SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com