Use of Delcare

  • I have a query where I want to use a DECLARE statement, but the variable I am declaring can be one of about 5 options. Is it possible to use DECLARE in this way?

    ex.

    DECLARE@symbol varchar (5)

    SET@symbol IN ('ABK','UYG','AIG','SKF')

  • I am not sure what you tried to do here but technically you cannot declare and set variable values in that fashion. Otherwise, you can use table variables to store array values into rows.

    --Ramesh


  • DECLARE @symbol table (id int, alpha varchar(5))

    INSERT INTO @symbol

    SELECT 1,'ABK' UNION

    SELECT 2,'UYG'UNION

    SELECT 3,'AIG' UNION

    SELECT 4,'SKF'

    select * from @symbol;

    U do have to use table variables, and then retrieve the variable u need from the select query

  • You could set it this way, not that I'd want to:

    DECLARE @myval varchar(max)

    SET @myval = '(''5'',''4'',''3'',''2'')'

    The two single quotes will turn into one inside the variable.

    But, the real question is, what are you trying to do?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Mayank Khatri (5/27/2009)


    DECLARE @symbol table (id int, alpha varchar(5))

    INSERT INTO @symbol

    SELECT 1,'ABK' UNION

    SELECT 2,'UYG'UNION

    SELECT 3,'AIG' UNION

    SELECT 4,'SKF'

    select * from @symbol;

    U do have to use table variables, and then retrieve the variable u need from the select query

    This appears to suit my purposes. THANKS!

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

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