在 Azure sql 服务器上运行脚本时出错,但在本地 sql 服务器上运行正常
Getting error when runs script on Azure sql server but working correctly on local sql server
当我 运行 在 Azure SQL 上进行查询时,出现以下错误:
错误:
此版本的 SQL 服务器不支持没有聚集索引的表。请创建聚集索引并重试。
而脚本 运行 在 本地 sql 服务器上完全没问题
脚本:
--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
上面的脚本只是循环遍历数据库中的所有 table,然后找到 'IsDeleted' 列并将其替换为 'IsActive' 列。
我需要在上面的查询中做哪些更改才能在 Azure SQL?
上 运行
我在数据库中有一个 table 没有聚簇索引。
它的架构:
--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,所以我创建了非聚集索引的复合主键。我只希望 table 像这样。
错误是因为这个 table 没有聚簇索引吗?
是的,错误是因为 table 没有聚集索引。
来自Azure SQL Database General Guidelines and Limitations
Microsoft Azure SQL Database does not support tables without clustered indexes. A table must have a clustered index. If a table is created without a clustered constraint, a clustered index must be created before an insert operation is allowed on the table.
推而广之,这也意味着更新操作。所以下面在你的脚本中动态生成的SQL会在执行时给出错误信息:
EXEC ('UPDATE '+@name+' SET [IsActive] = 1 - [IsDeleted]')
当我 运行 在 Azure SQL 上进行查询时,出现以下错误:
错误: 此版本的 SQL 服务器不支持没有聚集索引的表。请创建聚集索引并重试。
而脚本 运行 在 本地 sql 服务器上完全没问题
脚本:
--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
上面的脚本只是循环遍历数据库中的所有 table,然后找到 'IsDeleted' 列并将其替换为 'IsActive' 列。 我需要在上面的查询中做哪些更改才能在 Azure SQL?
上 运行我在数据库中有一个 table 没有聚簇索引。 它的架构:
--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,所以我创建了非聚集索引的复合主键。我只希望 table 像这样。 错误是因为这个 table 没有聚簇索引吗?
是的,错误是因为 table 没有聚集索引。
来自Azure SQL Database General Guidelines and Limitations
Microsoft Azure SQL Database does not support tables without clustered indexes. A table must have a clustered index. If a table is created without a clustered constraint, a clustered index must be created before an insert operation is allowed on the table.
推而广之,这也意味着更新操作。所以下面在你的脚本中动态生成的SQL会在执行时给出错误信息:
EXEC ('UPDATE '+@name+' SET [IsActive] = 1 - [IsDeleted]')