2

I have a table in SQL server 2008 R2 with 100 Million records.

One of the column is a foreign key to another table.

Table Student 
ID   Name    AccessToLap
1    Mike    1

Table AccessType
ID   Name
1    Allow
2    Deny

How can i change the column AccessToLab from int to varchar and change the values from 1 to Allow and from 2 to Deny without dropping and recreating the table.

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

3 Comments

and... EXEC sp_rename 'Student.AccessType', 'AccessToLap', 'COLUMN'
why are you renaming the column?
The query adds a new column (AccessType) which is temporary. It updates the column to the right values using the old column (AccessToLap.) At the end, it drops the old column and renames the new column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.