Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

Inserting into a column with an identity column

I have two tables Alpha and Beta. I want to copy all of the records from Alpha into Beta, except for the "Id" column values of Alpha, which should be ignored. They should be ignored because the Beta "Id" values are auto incremented. So in other words, I just want to copy the "Name" column of Alpha into Beta. Assuming that when the insert occurs, the Beta Id values will automatically be added because they are auto incremented values. Can someone help me write the insert statement. I can't get it to work, because I don't know how to make the insert statement ignore the Id column on insert.

USE [Gimbro]
GO
/****** Object:  Table [dbo].[Alpha]    Script Date: 3/18/2015 4:41:08 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Alpha](
      [Id] [int] NULL,
      [Name] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Beta]    Script Date: 3/18/2015 4:41:08 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Beta](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [nvarchar](50) NULL
) ON [PRIMARY]

---------------Now add data to Alpha table----
GO
INSERT [dbo].[Alpha] ([Id], [Name]) VALUES (0, N'Malicter')
GO
INSERT [dbo].[Alpha] ([Id], [Name]) VALUES (0, N'James')
GO
INSERT [dbo].[Alpha] ([Id], [Name]) VALUES (0, N'Jelly')
GO
INSERT [dbo].[Alpha] ([Id], [Name]) VALUES (0, N'Germosoli')
GO
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brgdotnet

ASKER

My friend, I posted this because I was actually working with a bigger table and just made some small examples. I could not get it to work, and now I feel silly, because it works now. Blush, Blush. Thank you for you're help.

INSERT INTO Beta select Name from Alpha
I made a mistake, and did not realize an error I had made, but now my code is working. Thanks.
Glad I could help,  while I do not know the purpose, I would recommend preserving the Id reference from alpha table in beta table.
Presumably based on your last comment, you Alpha table was designed in a way that you now see a reason to restructure it.
Preserving the ID will/could maintain other table references to simplify transitions.

I.e. You have alpha, gamma, that have a relationship to alpha's ID. If my guesstimate in the reason is correct, you would also need to transition gamma table as well to match the new beta tables ID column.