I am still new to DB stuff and this one is giving me an issue.
I am normalizing my table and I created a new one where [Status] is now FK.
I was able to insert all of the values from the original table into the new one, minus the [Status]
I am now trying to insert the [Status] using a select case.
Attempt 1:
USE [GESS]
GO
DECLARE @int_status int;
SELECT @int_status =
(CASE
WHEN [Status] = 'Alumni' THEN 5
WHEN [Status] = 'Faculty' THEN 6
WHEN [Status] = 'Friend' THEN 7
WHEN [Status] = 'Student' THEN 8
ELSE 9
END)
FROM [dbo].[GESSContact]
INSERT INTO dbo.GESSContact2 ([Status]) VALUES (@int_status)
GO
But it says only 1 row is affected.
Attempt 2:
USE [GESS]
GO
INSERT INTO [dbo].[GESSContact2]([Status])
Select (
CASE
WHEN o.[Status] = 'Alumni' THEN 5
WHEN o.[Status] = 'Faculty' THEN 6
WHEN o.[Status] = 'Friend' THEN 7
WHEN o.[Status] = 'Student' THEN 8
ELSE 9
END) As [Status]
From [dbo].[GESSContact] o
GO
This one says that all of the rows are affected but the [Status] is still set to null when I check.
Thanks for any help/suggestions
UPDATE:
While people were helping me out, The response that I marked as an answer made me realize I needed to be doing an UPDATE
instead of INSERT
. Here is my SQL that I used to make this work.
USE [GESS]
GO
UPDATE [dbo].[GESSContact2]
SET [dbo].[GESSContact2].[Status] =
CASE
WHEN o.[Status] = 'Alumni' THEN 5
WHEN o.[Status] = 'Faculty' THEN 6
WHEN o.[Status] = 'Friend' THEN 7
WHEN o.[Status] = 'Student' THEN 8
ELSE 9
END
FROM [dbo].[GESSContact2] JOIN [dbo].[GESSContact] o
ON [dbo].[GESSContact2].id = o.id
Thank you to everyone who helped!