'Error when I try to assign a value from a table to a variable in SQL

DECLARE UserIdToUpdate 

SELECT UserIdToUpdate = customer_id 
FROM dbo.orders

I get this error message (translated from the Spanish version):

Msg 137, Level 15, State 1, Line 13
You must declare the scalar variable '@UserIdToUpdate'



Solution 1:[1]

You need first to decalre the user defined varoable before using it

CREATE tABLe orders( customer_id INTEGER)
GO
DECLARE @UserIdToUpdate  INTEGER
SELECT TOP  1  @UserIdToUpdate = customer_id FROM dbo.orders
GO

db<>fiddle here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nbk