I have inherited a database I need to work with. All the numeric fields have been set to bigint (without reason they are all sub 5000).
How can I programmticaly change all the columns of big int to int? Is this possible will it cause issue with any existing constraints etc?
I only want to change tables and only in the specific database I am working on.
I am using SQL Server 2008 R2
I need to do this for hundreds of fields I am looking for something that I can run once and it will do all the updates for all table fields. I want to keep any existing constraints, null status and default values.
So basically a database wide change of bigint to int without changing anything apart from the field type.
Thanks
Well, I had come across this kind of problem. I had to change
inttobigint. This is harder, but possible. It is very easy to change datatype using the following statement:Alter table myTable alter column targetcolumn int not nullHowever if your columns are involved in constraint relationship then you have to drop your constraints then alter and then recreate your constraints.
EDIT
If you have a lot of constraints then changing it is a real pain in the butt. If there are a lot of tables with constraints and no extreme urge at changing don’t do it.
EDIT
Then you can do the following. Connect to Sql Server via Management Studio, right click on the necessary database => Tasks => Generate scripts.
Next => Next
At that point press advanced. There will be a popup. Set
Type of data to scriptto schema and data. Choose whatever output is comfortable for you (file, query window)? Press ok and proceed. It will produce you a complete DDL and DML, like this:Change all your datatypes appropriately, then run.
As you said all your data is below 5000 rows. So there is no need to modify insert statements.
Be ready it will take a long time.
Hope this was useful.
EDIT
This will generate you a new database, so be ready to rename your original or newly created db.