2017-09-12 11 views
0

我在SQL Azure中有一个数据库,我想用脚本删除所有列存储索引。在SQL Azure数据库上删除索引时出错:关键字'ON'(用户上下文= dbo)附近的语法不正确

我正在使用SQL Server的SQL管理登录使用SSMS进行连接。

我使用这个脚本:

declare @sql nvarchar(max); 
set @sql = N''; 
select @sql = @sql + N'DROP INDEX ' + OBJECT_SCHEMA_NAME(i.OBJECT_ID) + '.' + i.name + N' ON ' + OBJECT_SCHEMA_NAME(i.OBJECT_ID) + '.' + o.name + '; 
' 
FROM sys.indexes AS i INNER JOIN sys.tables AS o ON i.[object_id] = o.[object_id] 
where i.name is not null and o.name is not null and i.type_desc like '%COLUMN%' 
PRINT @sql; 
EXEC sp_executesql @sql; 

一个例子声明:

DROP INDEX [dbo].[CCI_MyTable] ON [dbo].[MyTable]; 

运行时,这会产生错误:

Incorrect syntax near the keyword 'ON'.

如果我尝试只是:

DROP INDEX [dbo].[CCI_MyTable] 

这会产生错误:

Cannot drop the index 'dbo.CCI_MyTable', because it does not exist or you do not have permission.**

在SSMS中,我可以看到SQL Server管理用户在[主]数据库中存在,但在我在工作的数据库中不存在

内。这个数据库,我正在为“DBO”:

SELECT USER_NAME()  -- DBO 
SELECT CURRENT_USER; -- DBO 

不应该DBO权限来删除索引?

要求:
什么是正确的方式去做这件事?我是否需要将管理员用户添加到此数据库?如果该用户存在,并且我使用SSMS连接,那么user_name()是那个用户而不是dbo?

+1

删除DBO [CCI_MyTable],并保持脚本的休息,因为它是 – TheGameiswar

+0

指标势必表,表由架构 – TheGameiswar

+0

区分看来。像dbo之前的INDEX名称是问题。互联网上的很多例子都使用这种表示法,所以不确定我的问题是什么。 – tbone

回答

1

看来问题出现在索引名称和模式之前(尽管我发誓我已经阅读过很多例子)。

所以正确的脚本语法是:从[DBO]

declare @sql nvarchar(max); 
set @sql = N''; 
select @sql = @sql + N'DROP INDEX ' + i.name + N' ON ' + OBJECT_SCHEMA_NAME(i.OBJECT_ID) + '.' + o.name + '; 
' 
FROM sys.indexes AS i INNER JOIN sys.tables AS o ON i.[object_id] = o.[object_id] 
where i.name is not null and o.name is not null and i.type_desc like '%COLUMN%' 
PRINT @sql; 
EXEC sp_executesql @sql; 
相关问题