Home > Archive > MS SQL Data Warehousing > February 2006 > Variable as criteria









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Variable as criteria
Greg

2006-02-07, 8:23 pm

I would like to reference a variable as the criteria in a query. I want the
criteria to be as follows: in('A','B'). I would assume you set the variable
to equal this but I am doing something wrong

declare @exclusion varchar(255)
set @exclusion = ('A','B','C')

select * from table where code in @exclusion

Any thoughts would be appreciated,

Greg

Aiwa

2006-02-08, 9:23 am

Hi Greg,

What you could do is use a table variable to hold your criteria information.

DECLARE @Criteria (CriteriaValue CHAR(1))

INSERT INTO @Criteria VALUES ('A')
INSERT INTO @Criteria VALUES ('B')

SELECT a.*
FROM Table a
JOIN @Criteria b ON b.CriteriaValue = a.code

HTH,
Eric

"Greg" wrote:

> I would like to reference a variable as the criteria in a query. I want the
> criteria to be as follows: in('A','B'). I would assume you set the variable
> to equal this but I am doing something wrong
>
> declare @exclusion varchar(255)
> set @exclusion = ('A','B','C')
>
> select * from table where code in @exclusion
>
> Any thoughts would be appreciated,
>
> Greg
>

Aiwa

2006-02-08, 9:23 am

Sorry there is a typo in my example:

DECLARE @Criteria TABLE (CriteriaValue CHAR(1))


"Aiwa" wrote:
[color=darkred]
> Hi Greg,
>
> What you could do is use a table variable to hold your criteria information.
>
> DECLARE @Criteria (CriteriaValue CHAR(1))
>
> INSERT INTO @Criteria VALUES ('A')
> INSERT INTO @Criteria VALUES ('B')
>
> SELECT a.*
> FROM Table a
> JOIN @Criteria b ON b.CriteriaValue = a.code
>
> HTH,
> Eric
>
> "Greg" wrote:
>
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com