need help with assigning a value to a variable

  • Hi

    I am trying to assign the value of the  variable using the output of a query but I am getting an error.  How do I fix it?

    Query:

    DECLARE @LATEST_DATE DATE;

    SET @LATEST_DATE = SELECT MAX(MY_DATE) FROM DBO.TABLE

    Error:

    Msg 156, Level 15, State 1, Line 8
    Incorrect syntax near the keyword 'SELECT'.

    Thank you

  • Either below; the first  is more typical:

    DECLARE @LATEST_DATE DATE;

    SELECT @LATEST_DATE = MAX(MY_DATE) FROM DBO.TABLE

    --or:

    SET @LATEST_DATE = (SELECT MAX(MY_DATE) FROM DBO.TABLE);

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • ScottPletcher wrote:

    Either below; the first  is more typical:

    DECLARE @LATEST_DATE DATE;

    SELECT @LATEST_DATE = MAX(MY_DATE) FROM DBO.TABLE

    --or:

    SET @LATEST_DATE = (SELECT MAX(MY_DATE) FROM DBO.TABLE);

      It works.  Thank you so much!

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

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