I am in the process of migrating an old legacy system that has mostly flat database tables with way too many columns. Each new setting requires a new column etc so the tables are getting ridiculously big. I am trying to change this structure to a relational one and am struggling to migrate the old existing data into the new structure. Here is an example of the old table we have:
CREATE TABLE [dbo].[User_OLD](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FullName] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[Setting1] [bit] NULL,
[Setting1Value] [int] NULL,
[Setting2] [bit] NULL,
[Setting2Value] [int] NULL,
[Setting3] [bit] NULL,
[Setting3Value] [int] NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC));
This table is being migrated into multiple tables, here is an example:
CREATE TABLE [dbo].[User_NEW](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FullName] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC));
CREATE TABLE [dbo].[UserSetting](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[SettingName] [varchar](250) NOT NULL,
[SettingValue] [varchar](250) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
CONSTRAINT [PK_UserSetting] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT FK_UserSetting_User FOREIGN KEY ([UserId]) REFERENCES User_NEW(Id));
So the issue is that I need to take a record from User_OLD and insert it's values into User_NEW, I then need to take the User_NEW.Id and insert it into the UserSetting table with the corresponding Setting1, Setting1Value going into the new tables columns SettingName and SettingValue.
If you can help me with a script that could achieve that I would be very appreciative!