sql-serversql-server-2008-r2

Change all SQL Server Columns From BigInt to Int


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


Solution

  • Well, I had come across this kind of problem. I had to change int to bigint. This is harder, but possible. It is very easy to change datatype using the following statement:

    Alter table myTable alter column targetcolumn int not null

    However if your columns are involved in constraint relationship then you have to drop your constraints then alter and then recreate your constraints.

    Alter table myTable drop constraint [fkconstraintname]
    Alter table myTable alter column targetcolumn int not null
    Alter table othertable alter column targetcolumn int not null
    Alter table myTable add constraint [fkconstraintname] foreign key (targetcolumn) references othertable(targetcolumn)
    

    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

    Necessary window

    At that point press advanced. There will be a popup. Set Type of data to script to 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:

    USE [master]
    GO
    /****** Object:  Database [Zafarga]    Script Date: 02/02/2012 19:31:55 ******/
    CREATE DATABASE [Zafarga] ON  PRIMARY 
    
    GO
    ALTER DATABASE [Zafarga] SET COMPATIBILITY_LEVEL = 100
    GO
    IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
    begin
    EXEC [Zafarga].[dbo].[sp_fulltext_database] @action = 'enable'
    end
    GO
    ALTER DATABASE [Zafarga] SET ANSI_NULL_DEFAULT OFF
    GO
    ALTER DATABASE [Zafarga] SET ANSI_NULLS OFF
    GO
    ALTER DATABASE [Zafarga] SET ANSI_PADDING OFF
    GO
    ALTER DATABASE [Zafarga] SET ANSI_WARNINGS OFF
    GO
    ALTER DATABASE [Zafarga] SET ARITHABORT OFF
    GO
    ALTER DATABASE [Zafarga] SET AUTO_CLOSE OFF
    GO
    ALTER DATABASE [Zafarga] SET AUTO_CREATE_STATISTICS ON
    GO
    ALTER DATABASE [Zafarga] SET AUTO_SHRINK OFF
    GO
    ALTER DATABASE [Zafarga] SET AUTO_UPDATE_STATISTICS ON
    GO
    ALTER DATABASE [Zafarga] SET CURSOR_CLOSE_ON_COMMIT OFF
    GO
    ALTER DATABASE [Zafarga] SET CURSOR_DEFAULT  GLOBAL
    GO
    ALTER DATABASE [Zafarga] SET CONCAT_NULL_YIELDS_NULL OFF
    GO
    ALTER DATABASE [Zafarga] SET NUMERIC_ROUNDABORT OFF
    GO
    ALTER DATABASE [Zafarga] SET QUOTED_IDENTIFIER OFF
    GO
    ALTER DATABASE [Zafarga] SET RECURSIVE_TRIGGERS OFF
    GO
    ALTER DATABASE [Zafarga] SET  ENABLE_BROKER
    GO
    ALTER DATABASE [Zafarga] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
    GO
    ALTER DATABASE [Zafarga] SET DATE_CORRELATION_OPTIMIZATION OFF
    GO
    ALTER DATABASE [Zafarga] SET TRUSTWORTHY OFF
    GO
    ALTER DATABASE [Zafarga] SET ALLOW_SNAPSHOT_ISOLATION OFF
    GO
    ALTER DATABASE [Zafarga] SET PARAMETERIZATION SIMPLE
    GO
    ALTER DATABASE [Zafarga] SET READ_COMMITTED_SNAPSHOT OFF
    GO
    ALTER DATABASE [Zafarga] SET HONOR_BROKER_PRIORITY OFF
    GO
    ALTER DATABASE [Zafarga] SET  READ_WRITE
    GO
    ALTER DATABASE [Zafarga] SET RECOVERY FULL
    GO
    ALTER DATABASE [Zafarga] SET  MULTI_USER
    GO
    ALTER DATABASE [Zafarga] SET PAGE_VERIFY CHECKSUM
    GO
    ALTER DATABASE [Zafarga] SET DB_CHAINING OFF
    GO
    EXEC sys.sp_db_vardecimal_storage_format N'Zafarga', N'ON'
    GO
    USE [Zafarga]
    GO
    /****** Object:  Table [dbo].[Category]    Script Date: 02/02/2012 19:31:56 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Category](
        [CategoryId] [bigint] IDENTITY(1,1) NOT NULL,
        [CategoryName] [nvarchar](max) NULL,
    PRIMARY KEY CLUSTERED 
    (
        [CategoryId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object:  Table [dbo].[Product]    Script Date: 02/02/2012 19:31:56 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Product](
        [ProductId] [bigint] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](max) NULL,
        [Price] [decimal](18, 2) NOT NULL,
        [CategoryId] [bigint] NOT NULL,
    PRIMARY KEY CLUSTERED 
    (
        [ProductId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object:  ForeignKey [Category_Products]    Script Date: 02/02/2012 19:31:56 ******/
    ALTER TABLE [dbo].[Product]  WITH CHECK ADD  CONSTRAINT [Category_Products] FOREIGN KEY([CategoryId])
    REFERENCES [dbo].[Category] ([CategoryId])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [Category_Products]
    GO
    

    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.