Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near the keyword 'TOP'. SQL Server

Tags:

sql

sql-server

I have a function whose return value I am assigning it to a variable and I am getting an error

Incorrect syntax near the keyword 'TOP'. SQL Server

if @Miracle is null OR @Miracle   =''
select @Miracle  = TOP(1) M.MiracleName
FROM Miracle M where M.MiracelID = @MiracelID

how can I assign functions like TOP to a variable ??

like image 939
msbyuva Avatar asked Oct 19 '25 17:10

msbyuva


1 Answers

I think you just need to move TOP 1 before your variable:

select TOP 1 @Miracle = M.MiracleName
...

Your aren't assigning TOP to a variable, but rather using TOP to tell SQL Server to only return a single row.

Good luck.

like image 79
sgeddes Avatar answered Oct 21 '25 06:10

sgeddes