SQL SERVER, I’m working with a table that uses a guid for the key instead of an int but for the integration I’m working on, I need it to be an int. So, I want to write something that will create an ID column if it doesn’t exist and populate it with the next highest ID. I’m not really sure how to do this though. Does anyone have code that does this?
i’ve tried this but the update doesn’t work because they’re null
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'c_Product'AND COLUMN_NAME = 'ProductId')
BEGIN
ALTER TABLE c_Product ADD ProductId INT
END
UPDATE c_Product SET ProductId = (SELECT Max(ProductId) + 1 END FROM c_Product)
Am I being dense here. Are you not just wanting to do:
Which will assign identity values for all existing rows, and then provide new numbers as new rows are inserted.