SQL脚本无效的列名称

时间:2016-09-15 14:57:01

标签: sql sql-server

BEGIN TRANSACTION
BEGIN TRY
DECLARE @tblRCatOmitTemp TABLE (ID INT IDENTITY(1,1),
RCatId          int     NOT NULL,
CountryId   int     NOT NULL)

INSERT INTO @tblRCatOmitTemp
    SELECT  RCatId, CountryId 
    FROM    dbo.tblRCatOmit TABLOCKX

DROP TABLE dbo.tblRCatOmit

CREATE TABLE dbo.tblRCatOmit(   
            ID     INT IDENTITY(1,1),
        RCatId     INT NOT NULL,
        CountryId  INT NOT NULL) ON [PRIMARY]

ALTER TABLE dbo.tblRCatOmit  
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);  

ALTER TABLE dbo.tblRCatOmit   
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)     
    REFERENCES dbo.tblRCat (RCatID);   

ALTER TABLE dbo.tblRCatOmit    
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)     
    REFERENCES dbo.tblCountry (CountryID)        

SET IDENTITY_INSERT dbo.tblRCatOmit ON
INSERT INTO 
    dbo.tblRCatOmit(
        ID,
        RCatID,
        CountryID)
SELECT
    ID,
    RCatID,
    CountryID
FROM 
    @tblRCatOmitTemp
SET IDENTITY_INSERT dbo.tblRCatOmit OFF

COMMIT TRANSACTION tblRCatOmit;
END TRY
BEGIN CATCH
    PRINT N'Something Happend!'
    ROLLBACK;
END CATCH

我必须在表格中添加一个新的IDENTITY字段以及PK和FK。我正在尝试将数据保存到临时表中,删除原始表,然后重新创建它。

当我运行此脚本时出现错误:

  

无效的列名'ID'

我猜它正在进行预编译,让我知道当前表中不存在ID。

我需要做些什么才能修改此脚本以便它运行?

当我双击Invalid column name 'ID'消息时,它会将我带到脚本的这一部分:

INSERT INTO 
    dbo.tblRCatOmit(ID,
            RCatID,
            CountryID)

1 个答案:

答案 0 :(得分:2)

BEGIN TRANSACTION
BEGIN TRY

IF OBJECT_ID('tempdb..#tblRCatOmitTemp') IS NOT NULL
    BEGIN
        DROP TABLE #tblRCatOmitTemp
    END

CREATE TABLE #tblRCatOmitTemp (
    RCatId int NOT NULL,
    CountryId int NOT NULL)

INSERT INTO #tblRCatOmitTemp
    SELECT  RCatId, CountryId 
    FROM    dbo.tblRCatOmit TABLOCKX

DROP TABLE dbo.tblRCatOmit

CREATE TABLE dbo.tblRCatOmit(   
            ID     INT IDENTITY(1,1),
        RCatId     INT NOT NULL,
        CountryId  INT NOT NULL) ON [PRIMARY]

ALTER TABLE dbo.tblRCatOmit  
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);  

ALTER TABLE dbo.tblRCatOmit   
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)     
    REFERENCES dbo.tblRCat (RCatID);   

ALTER TABLE dbo.tblRCatOmit    
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)     
    REFERENCES dbo.tblCountry (CountryID)        

INSERT INTO 
    dbo.tblRCatOmit(
        RCatID,
        CountryID)
SELECT
    RCatID,
    CountryID
FROM 
    #tblRCatOmitTemp

COMMIT TRANSACTION tblRCatOmit;
END TRY
BEGIN CATCH
    PRINT N'Something Happend!'
    ROLLBACK;
END CATCH
  • 如果您有任何数据量,请使用临时表而不是表变量,因为性能会好很多!
  • 接下来不要在临时表上构建ID,如果你这样做是任意的,并且在插入到新重新创建的表时,它将为你构建它。也意味着无需设置身份插入。

如果您不想使用临时表并添加该插件,您还可以使用sp_rename重命名现有的表....

如果你不关心列的顺序位置,那么只需添加列然后添加键。

ALTER TABLE dbo.tblRCatOmit
ADD ID INT IDENTITY(1,1)

ALTER TABLE dbo.tblRCatOmit  
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);  

ALTER TABLE dbo.tblRCatOmit   
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)     
    REFERENCES dbo.tblRCat (RCatID);   

ALTER TABLE dbo.tblRCatOmit    
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)     
    REFERENCES dbo.tblCountry (CountryID)