How to assign variable in WHILE EXISTS() Loop

Viewed 3643

I would like to iterate through table rows where a certain condition is met. On each iteration I would like to assign two of the current row's value to a variable. This is what I got so far:

    WHILE EXISTS(SELECT TOP 1 *
FROM [Communications] c
WHERE [communicationTypeID] = 2
AND [status] = 0)

SET @communicationId = c.[id]
SET @message = c.[Message]
BEGIN
....

It display an error:

Msg 4104, Level 16, State 1, Line 25
The multi-part identifier "c.id" could not be bound.
Msg 4104, Level 16, State 1, Line 26
The multi-part identifier "c.Message" could not be bound.

Could someone please direct me to the correct direction? I am pretty new to SQL. Thank you in advance. Peter

1 Answers
Related