2013-04-29 77 views
2

我在SQL服務器2008 R2中有一個表,有100萬條記錄。修改列類型並替換數據

其中一列是另一個表的外鍵。

Table Student 
ID Name AccessToLap 
1 Mike 1 

Table AccessType 
ID Name 
1 Allow 
2 Deny 

我怎樣才能改變從intAccessToLabvarchar和改變從1Allow2Deny值不刪除和重建表。

回答

4
alter table Student add AccessType varchar(50); 

update s 
set  AccessType = at.Name 
from Student s 
join AccessType at 
on  at.ID = s.AccessToLap; 

alter table Student drop column AccessToLap; 

exec sp_rename 'Student.AccessType', 'AccessToLap', 'COLUMN' 
+2

和... EXEC sp_rename 'Student.AccessType', 'AccessToLap', '列' – 2013-04-29 12:37:32

+0

@IanKenney:好一點,添加 – Andomar 2013-04-29 12:42:19

+0

你爲什麼要重命名的列? – Maro 2013-04-29 12:47:48