nam vo
nam vo

Reputation: 3447

SQL select @variable

I have a simple SQL statement that is to get the first rowid from question, but it gives an error thanks to too many items return. So how can I fix it ?

    DECLARE @first_id int  

select @first_id = (SELECT row_number() over (order by id) as id FROM question)

SELECT @first_id = id FROM question >> this syntax works fine but not what I want from query result.

Thanks

Upvotes: 0

Views: 2304

Answers (2)

user848765
user848765

Reputation:

Do you need the ROW_NUMBER or just the id field itself? If it is the id field it is simple:

SELECT @first_id = MIN(id) FROM question

Upvotes: 3

Ian
Ian

Reputation: 181

Try this

set @first_id = (SELECT top 1 id FROM question order by id)

Upvotes: 3

Related Questions