Counter of Festivals

Ashok Blog for SQL Learners and Beginners and Experts

Monday, 13 January 2014

SELECT TOP 1 Value in Variables

Hi,

declare @FirstLocationNotZero int
select @FirstLocationNotZero = top 1 Location from dbo.Table1--------------see wrong one
where value<>0
print @FirstLocationNotZero

I should obtain 2, instead of:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'top'


Solution:

declare @FirstLocationNotZero int
select top 1 @FirstLocationNotZero = Location ------------------------> see Right one
from dbo.Table1
where value<>0

print @FirstLocationNotZero