SQL Server Designer table re-creation - sql

Can I change the script the SSMS does when you use designer to alter your table which causes table re-creation?
I know the 'Prevent saving changes that require table re-creation' option but I want it to leave unchecked.

You can check this box to generate the script. From there, you can modify if you like.

Related

Can SMS generate "alter table" code when tables are beeing changed?

In PHPMyAdmin there is an option to get changes to a database in sql code (and php if I remember correctly).
Is this possible on Microsofts platform?
If I for example have this table:
create table test (
id int,
text varchar(5)
)
and when I add a column, can I get the "alter table code" for it?
Yes.
Right-click on the table name in Management studio and select Design.
Make your changes
Before you save the changes, click the Table Designer menu and select Generate Change Script..
A window will appear which will have the SQL script.
If you don't want to save it to a file, you can select the text and copy it (Ctrl-A doesn't work unfortunately)

How to generate multiple Alter Scripts in SSMS

I'm using sql server management studio 2008 to try and generate an alter script for each of my stored procedures in order to save the scripts for each revision. I can easily generate an alter script for each individual procedure, but I'm not trying to go through a hundred stored procedures manually.
I know that SSMS has an automated generate scripts function under task,
but the only options are create, drop and create, and drop.
I cant seem to figure out how to enable alter. I've already searched through many SO articles, as well as a little digging in msdn, and I've come up with nothing.
I'm hoping that the fine people of stackoverflow will be up to the challenge.
Thanks in advance
Use CHECK FOR OBJECT EXISTENCE option in Advanced Scripting Options.
Script will contain set of IF NOT EXISTS... CREATE commands and below ALTER for each stored procedure you wanted to script.
It's not a very elegant solution but it would definitely work. Why not generate create script and then just replace all occurrences of CREATE PROCEDURE with ALTER PROCEDURE.
You can generate stored procedures automatically from SQL SERVER Management Studio as following:
1) Right click on your database -> Tasks -> Generate Scripts
2) Select "specific database objects" then choose tables / stored procedures you want to generate script for them then press "Next"
3) In this window you can choose where you want to save your script, then you will find an option "Advanced", click it. Then you will find an option "Script DROP and Create",there are three options: Create, Drop, Drop and Create. Choose one as you want.
4) Then press ok, then "Next" and the script will be generated automatically.
If you want to change it from Create to Alter, just do "replace all" operation using any text editor.
Hope this answer helps others.
Well, Drop/Create is the same as alter. By stating that you would like to use alter then you are certain that the target objects exists. Why not just select the group from the object explorer and right click select DROP/Create. This should do the same.

SQL drop table and re-create and keep data

On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!
Using MS SQL Server
I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.
Right click on the desired DB in object explorer
Tasks > Generate scripts
Select the table you want to script
Then clicking on Advanced button
"Script DROP and CREATE" ->"Script DROP and CREATE"
"Types of data to script" -> "Schema and data"
Hope this helps
Here's some pseudo-code. No need to make a backup table, just make a new table with the right constraint, insert your records into it, and rename.
CREATE TABLE MyTable_2
(...field definitions)
<add the constraint to MyTable_2>
INSERT INTO MyTable_2 (fields)
SELECT fields
FROM MyTable
DROP TABLE MyTable
exec sp_rename 'MyTable2', 'Mytable'
Using SQL Server Management Studio (SSMS), you can use it's table designer to specify the final condition of the table. Before saving the changes, have it generate the change script and save that script. Cancel out of the design window, open the script and review it. SSMS may already have generated a script that does everything you need, fixing the primary-foreign key relationship while preserving all existing data. If not, you will have a script, already started, that performs most of what you need to do and should be able to modify it for your needs.
This is your only solution.
Create the backup table, empty the original one, modify the table and then insert step-by-step until you find a violation.
Update All Schema Database Old by new Schema Database .
Create script (Right click on the desired DB in object explorer Tasks > Generate scripts -> select option select specific database objects and tables ->next -> advanced-> option Type of data to script Data only -> ok ->next ->next.) to data only and backup Database to old database
Drop database old and create new database and make new DB is empty .
Excute script of Old Data only on new database .

Cannot modify table ( using microsoft sql server management studio 2008 )

I create 2 tables and another 1 with foreign keys to the other two.
I realized I want to make some changes to table no 3.
I try to update a field but I get an error "Saving changes is not permitted. The changes you have made require the following table to be dropped and re-created."
I delete those 2 relationships but when I look at dependencies I see my table still depends on those 2 and I still cannot make any change to it.
What can I do?
You can also enable saving changes that require dropping of tables by going to "tools->options->designers->Table and database designers" and unchecking "Prevent saving changes that require table re-creation"
Be careful with this though, sometimes it'll drop a table without being able to recreate it, which makes you lose all data that was in the table.
When using Microsoft SQL Server Management Studio 2012, the same message occurs.
I used the script feature to do modifications which can be seen as a rather good workaround if you wanna use the designer only within a "safe" mode.
Especially the GUI related to create a foreign key is not the best in my opinion. When using a script (alter table) for adding a fk, you are faster than using this GUI feature.
When adding/writing a 'not' in prior to null, that's not a hard issue. (Removing an 'Allow Nulls' for a column refers to "Saving changes is not permitted" when using the designer.)

Why isn't SSMS smart when it comes to adding columns?

Whenever I want to add a column to a table it usually goes something like this:
Fire up SQL Server Management Studio (SSMS)
Select "Design" on the table I want to add the column to
Add the new column to the table
Save
Get an error that SSMS can't save because it would need to drop the table (and it can't because the the table has foreign keys on it).
Get frustrated that I forgot that this is something that SSMS can't do
Construct an alter table command by hand to add the column
Move on with life.
This time I am adding a step between numbers 6 and 7. I thought I would ask why SSMS can't make a simple alter table statement to add my new column in.
(In case it matters I am running SSMS 2008 against SQL Server 2008.)
Alternatively, you can go to Tools-->Options-->Designers-->Table and Database Designers and uncheck "Prevent saving changes that require table re-creation"
Problem solved.
Here's an explanation from MSDN: http://support.microsoft.com/default.aspx/kb/956176.
When you change a table so that you
alter the metadata structure of the
table, and then you save the table,
the table must be re-created based on
these changes.