I'm trying to update each row in the firstName column to display FN1, FN2, FN3 etc. I know I'm missing a where clause that is causing an update to all the records. All the firstNames are different so I can't do a simple where condition i.e where firstName = 'adam'.
How can I go about updating all the rows with FN + Counter?
I was made aware I can use a row_number function but was told to use the while loop first for learning purpose.
DECLARE @Counter INT
SET @Counter = 1
WHILE (@Counter <= (Select COUNT(firstname) FROM CONSTITUENT_TEST))
BEGIN
UPDATE CONSTITUENT_TEST
SET firstname ='FN' + CONVERT(VARCHAR,@Counter)
SET @Counter = @Counter + 1
END
select * from CONSTITUENT_TEST
cursor
with awhile
loop solely for educational purposes. A set-based solution is generally preferable.firstname
loses what ever value it had before? So'Adam'
would be'FN1'
, not'Adam1'
or even'AdamFN1'
?