2015-02-10 33 views
0

上正常工作,當我運行以下Azure上的SQL查詢,它給了我以下錯誤:收到錯誤時運行Azure的SQL服務器上的腳本,但本地SQL Server

錯誤: 表沒有聚集索引不在在此版本的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 '[email protected]+' SET [IsActive] = 1 - [IsDeleted]') 
        END  
       ELSE -- If 'IsActive' column is not present 
        BEGIN 
         -- Add column named 'IsActive' 
         EXEC('ALTER TABLE '[email protected]+' ADD IsActive bit') 
         -- Copy the reverse value from 'IsDeleted' column to 'IsActive' 
         EXEC ('UPDATE '[email protected]+' SET [IsActive] = 1 - [IsDeleted]') 
         -- Add default value constraint for newly added column 'IsActive' 
         EXEC('ALTER TABLE '[email protected]+' ADD CONSTRAINT DF_'[email protected]+' 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 '[email protected]+' DROP COLUMN [IsDeleted]') 
      END 

     FETCH NEXT FROM db_cursor INTO @name 
    END 

CLOSE db_cursor 
DEALLOCATE db_cursor 

上述腳本簡單地通過在數據庫中的所有表循環,然後找到「請將isDeleted」列並將其替換爲「IsActive」列。 我需要在我的上述查詢中進行哪些更改才能在上運行Azure SQL

我有一個表中沒有聚集索引的數據庫。 其模式:

--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和專案手動插入不,我創建與非聚集索引複合主鍵。我只希望這張桌子是這樣的。 錯誤是因爲此表沒有聚集索引?

回答

1

是的,錯誤是因爲表沒有聚集索引。

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 '[email protected]+' SET [IsActive] = 1 - [IsDeleted]') 
相關問題