-2
DECLARE @JOBCODE NVARCHAR(MAX);
SET @JOBCODE = (SELECT TOP 1 JOBCODE FROM EMPLOYEES WHERE EMPLOYEENAME = 'SCHIPPERT, RALF')

I am trying to declare a variable but getting error:

Must declare the scalar variable

Tushar
  • 3,527
  • 9
  • 27
  • 49
Amit Amit
  • 9
  • 3

1 Answers1

0

The error is because you're trying to assign a SELECT statement directly to a variable without using the SELECT itself for the assignment.

DECLARE @JOBCODE NVARCHAR(MAX);
SELECT TOP 1 @JOBCODE = JOBCODE FROM EMPLOYEES WHERE EMPLOYEENAME = 'SCHIPPERT, RALF';

Here you use the SELECT statement to assign the result directly to the @JOBCODE variable via using @JOBCODE = JOBCODE

fiddle

pauliec
  • 406
  • 2
  • 8