模式名称菜单和表 dbo.Dinner 已存在于数据库中。
我想更改
从
dbo.Dinner
TO
menu.Dinner
使用此查询:
ALTER SCHEMA menu TRANSFER dbo.Dinner
我收到错误消息:
cannot transfer a schema bound object
我该如何解决这个问题?
答案 0 :(得分:0)
正如我评论的那样,我怀疑您在WITH SCHEMABINDING
/ CREATE
中至少有一个使用ALTER
子句的对象,因此您不能更改表对象。您需要DROP
并重新创建那些对象。例如:
USE Sandbox;
GO
CREATE TABLE dbo.Dinner (ID int, Meal varchar(20));
GO
CREATE VIEW dbo.Dinners WITH SCHEMABINDING AS
SELECT DISTINCT Meal
FROM dbo.Dinner;
GO
CREATE SCHEMA menu;
GO
ALTER SCHEMA menu TRANSFER dbo.Dinner; --Fails
GO
DROP VIEW dbo.Dinners;
GO
ALTER SCHEMA menu TRANSFER dbo.Dinner; --Succeeds
GO
CREATE VIEW dbo.Dinners WITH SCHEMABINDING AS
SELECT DISTINCT Meal
FROM menu.Dinner;
GO
--Ceal up
DROP VIEW dbo.Dinners;
DROP TABLE menu.Dinner;
DROP SCHEMA menu;
您可以使用以下方法获取相关性列表:
SELECT DISTINCT
o.[name],
o.[type_desc]
FROM sys.sql_expression_dependencies sed
JOIN sys.objects o ON sed.referencing_id = o.object_id
WHERE sed.referenced_schema_name = N'dbo'
AND sed.referenced_entity_name = N'Dinner';
答案 1 :(得分:0)
我们也收到此错误,正是因为我们使用了具有架构绑定的物化视图。
我们最终在新架构中重新创建了对象,并使用 insert
select
语句将数据复制到新架构中。
每个表的步骤是:
这是一个示例脚本:
drop table if exists [target_schema].[dim_forexchange]
go
CREATE TABLE [target_schema].[dim_forexchange](
[time_year] [int] NOT NULL,
[local_acct_curr] [nvarchar](50) NOT NULL,
[DivisorFXtoGBP] [float] NOT NULL,
[id] [uniqueidentifier] NULL
) ON [PRIMARY]
GO
ALTER TABLE [target_schema].[dim_forexchange] ADD DEFAULT (newid()) FOR [id]
GO
insert into [target_schema].[dim_forexchange] select * from [dbo].[dim_forexchange];
go