如何在字段之间添加列或更改SQL中的现有列?

时间:2011-08-16 23:38:47

标签: asp.net sql sql-server-2008

是否有人知道如何在特定字段之间或特定字段之后在SQL 2005/2008中向表中添加列?那么,如何改变现有列的位置?

示例:

AHADRS, AHCITY, AHZIPC

我想在AHCITYAHZIPC

之间添加状态

这是我到目前为止所尝试的但它们无法正常工作

ALTER TABLE AHASNF00 ADD AHZIPC varchar(max) AFTER AHCITY
ALTER TABLE AHASNF00 ADD AHZIPC varchar(max) FIRST AHZIPC

任何帮助都会非常感激,谢谢。

3 个答案:

答案 0 :(得分:6)

在存储列时,SqlServer没有“位置”的概念。

如果您需要不同的列顺序,请更改select语句中的位置。

答案 1 :(得分:4)

通常,这可以通过创建一个临时表来解决,该临时表具有您希望拥有它们的列的临时名称。然后将数据从当前表复制到另一个表,之后销毁旧表并将临时表重命名为原始表。

当然,这一切都必须与删除和读取主要和外键引用相协调。

例如,当我尝试在名为“Term”的现实示例表的其他列中间添加列时,这是SQL Server Management Studio生成的更改脚本。

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Term
    DROP CONSTRAINT FK_Term_TermUnit
GO
ALTER TABLE dbo.TermUnit SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Term
    (
    Id int NOT NULL IDENTITY (1, 1),
    Label varchar(50) NOT NULL,
    TermUnitId int NOT NULL,
    NewColumnInTheMiddle bit NULL,
    UnitCount int NOT NULL
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Term SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_Term ON
GO
IF EXISTS(SELECT * FROM dbo.Term)
     EXEC('INSERT INTO dbo.Tmp_Term (Id, Label, TermUnitId, UnitCount)
        SELECT Id, Label, TermUnitId, UnitCount FROM dbo.Term WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Term OFF
GO
ALTER TABLE dbo.TreasuryIndexRate
    DROP CONSTRAINT FK_TreasuryIndexRate_Term
GO
ALTER TABLE dbo.TreasuryIndexField
    DROP CONSTRAINT FK_TreasuryIndexField_Term
GO
DROP TABLE dbo.Term
GO
EXECUTE sp_rename N'dbo.Tmp_Term', N'Term', 'OBJECT' 
GO
ALTER TABLE dbo.Term ADD CONSTRAINT
    PK_Term PRIMARY KEY CLUSTERED 
    (
    Id
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.Term ADD CONSTRAINT
    FK_Term_TermUnit FOREIGN KEY
    (
    TermUnitId
    ) REFERENCES dbo.TermUnit
    (
    Id
    ) ON UPDATE  NO ACTION 
     ON DELETE  NO ACTION 

GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TreasuryIndexField ADD CONSTRAINT
    FK_TreasuryIndexField_Term FOREIGN KEY
    (
    TermId
    ) REFERENCES dbo.Term
    (
    Id
    ) ON UPDATE  NO ACTION 
     ON DELETE  NO ACTION 

GO
ALTER TABLE dbo.TreasuryIndexField SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TreasuryIndexRate ADD CONSTRAINT
    FK_TreasuryIndexRate_Term FOREIGN KEY
    (
    TermId
    ) REFERENCES dbo.Term
    (
    Id
    ) ON UPDATE  NO ACTION 
     ON DELETE  NO ACTION 

GO
ALTER TABLE dbo.TreasuryIndexRate SET (LOCK_ESCALATION = TABLE)
GO
COMMIT

答案 2 :(得分:2)

不幸的是,我不相信使用T-SQL可以轻松实现这一目标。你必须做一些事情,比如用你想要列的顺序创建一个新表,将旧表中的数据插入到新表中,删除旧表,重命名新表。