当我在Azure SQL上运行以下查询时,它会出现以下错误:
错误: 此版本的SQL Server不支持没有聚簇索引的表。请创建聚簇索引,然后重试。
该脚本在本地sql server
上运行完全正常脚本:
--Script for removing 'IsDeleted' column from all the table and copy the reverse values to the 'IsActive' column
DECLARE @name VARCHAR(50)
DECLARE @TQ VARCHAR(500)
DECLARE @ConstrainName VARCHAR(500)
--Declare cursor and loop it on INFORMATION_SCHEMA.TABLES and get table name in variable @name one by one
DECLARE db_cursor CURSOR
FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN --If 'IsDeleTed' column is present
IF COL_LENGTH(@name, 'IsDeleted') IS NOT NULL
BEGIN -- If 'IsActive' column is present
IF COL_LENGTH(@name, 'IsActive') IS NOT NULL
BEGIN -- Copy the reverse value from 'IsDeleted' column to 'IsActive'
EXEC ('UPDATE '+@name+' SET [IsActive] = 1 - [IsDeleted]')
END
ELSE -- If 'IsActive' column is not present
BEGIN
-- Add column named 'IsActive'
EXEC('ALTER TABLE '+@name+' ADD IsActive bit')
-- Copy the reverse value from 'IsDeleted' column to 'IsActive'
EXEC ('UPDATE '+@name+' SET [IsActive] = 1 - [IsDeleted]')
-- Add default value constraint for newly added column 'IsActive'
EXEC('ALTER TABLE '+@name+' ADD CONSTRAINT DF_'+@name+' DEFAULT(1) for [IsActive]')
END
IF EXISTS ( SELECT *
FROM sysobjects o
INNER JOIN syscolumns c ON o.id = c.cdefault
INNER JOIN sysobjects t ON c.id = t.id
WHERE o.xtype = 'D'
AND c.name = 'IsDeleted'
AND t.name = @name )
BEGIN -- If default constraint exist on column 'IsDeleted', get the constraint name
SET @ConstrainName = ( SELECT o.name
FROM sysobjects o
INNER JOIN syscolumns c ON o.id = c.cdefault
INNER JOIN sysobjects t ON c.id = t.id
WHERE o.xtype = 'D'
AND c.name = 'IsDeleted'
AND t.name = @name
)
-- Drop the default constraint from the column 'IsDeleted'
EXEC('ALTER TABLE ' + @name + ' drop constraint ' + @ConstrainName)
END
-- Finally drop the column 'IsDeleted'
EXEC('ALTER TABLE '+@name+' DROP COLUMN [IsDeleted]')
END
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
上面的脚本只是循环遍历数据库中的所有表,然后找到' IsDeleted'列并将其替换为' IsActive'柱。 我在上面的查询中需要做哪些更改才能在 Azure SQL 上运行它?
我在db中有一个没有聚簇索引的表。 它的架构:
--CREATE TEMP TABLE
CREATE TABLE [dbo].[Temp](
[LayoutId] [int] NOT NULL,
[UnitTypeId] [int] NOT NULL,
[ProjectId] [int] NOT NULL,
[LayoutName] [nvarchar](150) NOT NULL,
[LayoutDescription] [nvarchar](max) NOT NULL,
[IsActive] [bit] NOT NULL,
[IsDeleted] [bit] NOT NULL,
[CreatedTs] [datetime] NOT NULL,
[ModifiedTs] [datetime] NULL,
CONSTRAINT PK_UserGroup PRIMARY KEY NONCLUSTERED ([LayoutId], [ProjectId])
)
GO
ALTER TABLE [dbo].[Temp] ADD CONSTRAINT [DF_Temp_IsActive] DEFAULT ((1)) FOR [IsActive]
GO
ALTER TABLE [dbo].[Temp] ADD CONSTRAINT [DF_Temp_IsDeleted] DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[Temp] ADD CONSTRAINT [DF_Temp_CreatedTs] DEFAULT (getdate()) FOR [CreatedTs]
GO
由于我不想手动插入layoutId和ProjectId,因此我创建了具有非聚集索引的复合主键。我希望这张桌子只是这样。 该错误是因为该表没有聚集索引吗?
答案 0 :(得分:1)
是的,错误是因为该表没有聚集索引。
来自Azure SQL Database General Guidelines and Limitations
Microsoft Azure SQL数据库不支持没有聚簇索引的表。表必须具有聚簇索引。如果创建的表没有聚簇约束,则必须在表上允许插入操作之前创建聚簇索引。
通过扩展,这也意味着更新操作。因此,脚本中的以下动态生成的SQL在执行时会给出错误消息:
EXEC ('UPDATE '+@name+' SET [IsActive] = 1 - [IsDeleted]')