Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Management Studio to use ALTER TABLE instead of DROP/CREATE

I'm wondering if is there a way to force MSSQL Management Studio to produce a script like this:

ALTER TABLE Mytable
ADD MyCol bit NOT NULL
CONSTRAINT MyColDefault
DEFAULT 0 WITH VALUES


ALTER TABLE [dbo].Mytable
ALTER COLUMN MyCol2 int NULL

GO 

when I alter a very simple property of a column on a table. If I do this in the designer and ask for the produced script, the script doesn't do such simple tasks, but instead copies all the data in a tmp table, drops the original table, renames the tmp table with the original table name. And, of course, drops and recreates every constraint and relationships.

Is there any option I can change to change this behaviour? Or, this may be possible, is there some danger I don't see in using the simple ALTER TABLE above?

thanks.

like image 735
marco Avatar asked Mar 12 '10 10:03

marco


People also ask

How can I modify a table without dropping the table?

So to do that go to SQL Server and within Tools select Options. Now in the option window expand Designers and under that "Table and Database Designers" and uncheck the check box "Prevent saving changes that require table re-creation" then click OK.

How do I alter a table in SQL Server Management Studio?

We can also use the ALTER command to change the column's data type into the specified table. SQL Server provides the ALTER TABLE ALTER COLUMN statement to modify the column data type. We can do this by using the following syntax: ALTER TABLE table_name ALTER COLUMN column_name new_data_type(size);

How enable Alter to option in SQL Server?

Right-click on the table you want to alter and choose Design. Add new columns, change field types, set your fields to accept NULLS or not, etc. Once you are done, click the Generate Change Script toolbar button (or right-click on any column or in the white space).


2 Answers

Just to augment @marc_s's answer. In case you still can't generate the change script 'cause your table needs to be recreated, you need to go to Tools -> Options and under Designers -> Table and Database Designers unmark the option Prevent saving changes that require table re-creation.

enter image description here

like image 93
dcg Avatar answered Sep 20 '22 12:09

dcg


Yes you can!

In SQL Server Management Studio, go into the table design mode for the table in question, and make your changes. However: do not click on the "Save" button, but instead right-click in the table design view, there should be a "Generate Change Script" item at the end of your context menu.

alt text

Click on that menu item and you'll get presented a pop-up dialog box which contains the T-SQL script needed to do those changes that you made to the table in the designer. Copy or save that T-SQL code, and cancel out of the designer, and voila - you have your change script!

UPDATE: Marco, sorry, I don't think there's any option to change the default behavior, at least not right now. You might want to file an enhancement request with Microsoft on Microsoft Connect to propose that - good idea, I would think!

like image 23
marc_s Avatar answered Sep 18 '22 12:09

marc_s