54

Why does SQL 2008 all of a sudden want to drop my tables when I go to change the column type from say int to real? This never happened in SQL 2005 to my knowledge. Any insight would be helpful please.

2

5 Answers 5

58

I can't believe the top answer has been sitting here for so long - it is very dangerous advice!

There are few operations that you can do inplace without dropping your table:

If you find yourself in the situation where altering a column is not possible without dropping the table, you can usually use a SELECT INTO query to project your data into a new table, then drop the old table (temporarily disabling constraints) and then renaming the projected table. You will need to take your database offline for maintenance in this case though.

1
  • You can make a column not-nullable without dropping as long as the column has no nulls. You can add a new column to the end without dropping. You can sometimes alter the type (size) of a column. This answer seems very incomplete.
    – NetMage
    Commented Dec 10, 2021 at 21:02
46

Here is what I use:

-- Add new column
ALTER TABLE MyTable
ADD Description2 VARCHAR(MAX)
GO

-- Copy data to new column (probably with modifications)
Update MyTable
SET Description2 = Description
GO

-- Drop old column
ALTER TABLE MyTable
DROP COLUMN Description
GO

-- Rename new column to the original column's name.
sp_RENAME 'MyTable.Description2' , 'Description', 'COLUMN'
GO
  1. Copy the data into a new column.
  2. Drop the old column.
  3. Rename the new column to the old column's name.
7
  • How does this strategy affect the index if the column is indexed?
    – Dave
    Commented Oct 12, 2015 at 18:55
  • As far as I know, you will have to re-create the indexes on that column. The above action should remove them all (or to rephrase it better: it should remove the column from all the associated indexes). Commented Oct 13, 2015 at 7:32
  • The second statement under 'Copy data to new column' won't work because 'Description2' would be an invalid object/column
    – usefulBee
    Commented Mar 3, 2016 at 16:55
  • In addition that this answer appear to work only if the table is empty. If the table is not empty I get the following error: "ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'Description2 ' cannot be added to non-empty table 'MyTable' because it does not satisfy these conditions."
    – usefulBee
    Commented Mar 3, 2016 at 19:45
  • @usefulBee The error you get gives you the solution: use a default value from the column when creating it. Commented Mar 4, 2016 at 8:20
44

In SQL Server 2008, go to Tools >> Options. In the little window, click "Designer". Uncheck "Prevent saving changes that require ..."

=====

Edited on Sept 4th, 2015.

I have added this answer here a long, long time ago describing the way I would solve the situation described on the question above. Since then, users on the threads below have exposed several concerns on doing things the way I recommended at the time. Basically, the solution I described could be problematic on some scenarios. I then suggest you to keep on reading to check other users' comments and pick the best solution for you.

4
  • 9
    But It drops the the table! It is not a functional way for extra large tables Commented Aug 21, 2013 at 11:19
  • 34
    This is not an answer to the question, but a tweak to silently drop the table on structural changes, which is IMHO super bad!
    – Jan
    Commented Sep 2, 2013 at 14:15
  • The % of people wishing to change from a harmless nvarchar(200) to nvarchar(4000) is going to be unknown - I happened to need to do just that and thus this answer it "fine" and quick . However, I do agree with Others in that it is dangerous in many other datatype conversions etc.. Commented Sep 3, 2015 at 17:05
  • I see all the mixed feedback, however this answered the question precisely and if you have a large table, then by all means do this differently. But in a dev scenario with limited data, or where it will not adversely affect a production box, then this is fine. I recommend however that you use a tool like SQL Compare to look at and help you publish your schema changes so that you are aware of how other elements could be impacted, as even with TSQL, modifying a schembound view could drop indexes mistakenly.
    – Middletone
    Commented May 15, 2021 at 22:01
0

I have the same issue. Athough my account has sa rights if I try using another sa account it works. It seems that somehow my account does not have the ability to alter. still investigating, but it is a permission issue.

update:

I cannot expain it. but this is what I did. there two domain groups my account belonged to. One was a new AD domain group and the other was an NT legay domain group. Once I removed the legacy domain group I was able to alter the table successfully. Mind you both groups had "sa" priviliges.

The behavior was that the alter commands would result in success, but nothing changed on the table. Then when I manually tried to change the fields through the designer it complained that I was not allowed to make change if it required to drop and recreate the table. I found the setting in tools and I was able to turn that off. But this table is huge and not a good idea to do this. I woud advise others against it.

so it was a permission issue. I can't explain how, but I hope it helps someone else

0

Another way to this without totally dropping the table is

  1. Take a backup of the column values.
  2. Make the column nullable if it does not already allow nulls. Set the column values to be null by doing

    update tablename set columnname = null 
    
  3. Delete the column
  4. Insert a new column with the same name as the deleted column and the type which you want
  5. Insert the saved data into this column

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.