2011-01-07 147 views
-1

在發佈此問題之前,我嘗試了很多事情,但這對我沒有幫助。 我要重命名錶在SQL Server 2005中柱,下面的查詢我在SQL Server2005中運行:SQL服務器2005查詢未運行

1) ALTER TABLE Details RENAME COLUMN AccountID TO UID; but it gives me the error: Incorrect syntax near the keyword 'COLUMN'.

2)I have added one new column in the table by query: ALTER TABLE Details ADD BID uniqueidentifier; and then I want to set the coulmn property to not null .

我怎樣才能做到這一點?

在此先感謝 AS

回答

4

使用sp_Rename 'TableName.Column', 'NewColumnName', 'COLUMN'

爲了做你的問題的第二部分,你需要做的:

ALTER TABLE myTable 
     ADD myColumn UNIQUEIDENTIFIER NOT NULL DEFAULT 'some default value' 

如果你不想指定一個默認值,你必須首先創建列與NULL。一旦創建了該列,就可以填充所需的值,然後將該列重新更改爲NOT NULL

2

1)爲了不使用ALTER TABLE,爲什麼不使用sp_rename?例如:

EXEC sp_rename 'Details.[AccountID]', 'title', 'UID' 

2)可以使用ALTER TABLE Details ALTER COLUMN BID NOT NULL,但你可能會想它也指定一個默認值,如果表中已經有數據。