Using SET, ORDER BY and TOP together to populate a variable

  • Hi,

    I am trying to set a value from the following SELECT statement but I am getting an error:

    DECLARE @Tmp varchar (20)

    SET @Tmp = TOP 1 Value FROM Table ORDER BY NewID()

    but I am getting an error near the keyword 'TOP'

    Is this sort of statement possible or is there an alternative way to achieve what I am doing. By the way I am using the ORDER by NewID() to randomly select from a table.

    Cheers,

    Chris

  • Manage to solve it myself - missing the brackets.

    Should be: SET @Tmp = (SELECT TOP 1 Value FROM Table ORDER BY NewID())

  • little better....

    SELECT TOP 1 @Tmp = Value FROM Table ORDER BY NewID()

    --Ramesh


  • Thanks Ramesh, I forgot variables could be assigned that way.

    Cheers,

    Chris

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply