I am trying to alter a table in SQL server with a script. In the past I have always done this kind of thing through a GUI, but now I need to generate a script to do it for customers.
I have an SQL Server database table that is like this:
MyTable ------- ColA int NOT NULL ColB int NOT NULL ColC int NOT NULL ColD VARCHAR(100)
The primary key is defined across ColA, ColB, and ColC.
I want the SQL script to change the table like so:
MyTable ------- ColA int NOT NULL ColB int NOT NULL ColX int NOT NULL (new column, default 0 for existing data) ColC int NOT NULL ColD VARCHAR(100)
The primary key would now be defined by ColA, ColB, ColX, and ColC.
This is easy to do through SQL Server GUI. But when I have it generate a script from that, it seems unnecessarily complex. Basically, the script creates a temporary table with the new schema, copies all the data, indexes, and constraints from the old table into the temp table, deletes the old table, then renames the new one to the name of the old one. In addition, it has lines like this:
ALTER TABLE dbo.Tmp_MyTable ADD CONSTRAINT
MyTable21792984_ColC_DF DEFAULT ((0)) FOR ColC
I'm concerned that these random-looking numbers there (i.e. 21792984) will not be the same on all customer database instances. They look like something that the SQL server generates when creating the database that would be unique to each instance.
Is there a more straight-forward way of changing the table through SQL commands? I've looked online but what I've found is mostly basic and/or generic.
Update: From the answers I have received, it looks like the difficulty lies in putting the new column "in between" two columns. I've realized it doesn't really matter what order the columns are in (if I am wrong feel free to leave an answer correcting me). In my case, the change is much simpler if I just add the column to the end of the table, and nothing in the code is relying on the specific column order.