SQL – add a table column to an existing table and assign it values from a variable

I would like to add a new column to an existing table (the type of the column is UNIQUEIDENTIFIER DEFAULT NULL), and I want to populate the column with a value grabbed from a subquery like below:

ALTER TABLE [Kohi].[dbo].[CalendarNote]
ADD [PlannerId] UNIQUEIDENTIFIER DEFAULT NULL

DECLARE @COLLECTION_ID UNIQUEIDENTIFIER
SELECT @COLLECTION_ID = [ShiftCollectionId] FROM [Kohi].[dbo].[ShiftCollection] WHERE [Name] = 
'XXXXX'
UPDATE [Kohi].[dbo].[CalendarNote]
SET [PlannerId] = @COLLECTION_ID
WHERE [PlannerId] IS NULL

When I run the query, I have got a error saying 'Invalid column name 'PlannerId''. Does anyone know how to get this to work? Thank you!



Read more here: https://stackoverflow.com/questions/66482270/sql-add-a-table-column-to-an-existing-table-and-assign-it-values-from-a-variab

Content Attribution

This content was originally published by Wayne at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: