Results 1 to 2 of 2

Thread: Storing Part of an Express to a Variable

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Storing Part of an Express to a Variable

    I know this can be done, because I saw it and tried it once. But did not use it at the time and consequently forgot how.

    I am storing an expression to a variable @Var Varchar(50)

    @Var = ('aa','bb','cc')

    I want to:

    Select * from Table WHERE Field IN @Var

    Substituting my variable for the " ('aa',bb','cc') which I can build dynamically...thus the use.

    Purpose:
    I have an aggregate query that currently has a CASE statement based on the contents of ('aa',bb','cc'). However, those contents can change. The aggregate will not allow the subquery (SELECT Field FROM Table).

    What I remember looked something like:

    SELECT * FROM Table WHERE field in ' & @Var &'.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Tried this?

    declare @Var Varchar(50), @cmd varchar (200)
    set @Var = '(''aa'',''bb'',''cc'')'
    set @cmd = 'Select * from Table WHERE Field IN ' + @Var
    exec (@cmd)


    Where '' are two single '.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •