views:

38

answers:

1

I need to search in a table for items which have all of my desired values in a column i.e. I have table :

ID :    1 2 3 3 2 2 2 1 1 3
VALUE : 5 6 5 3 6 7 2 1 9 0

I want to give a StoredProc a list of values for example ("6,7,2") and it returns me all IDs that have all the given values in this case it would only returns 2

If I wanted to search for those which at least have one of the values I know I could use "IN" but to have all the values i found nothing.

Thank you in advance

Afshin Arefi

+2  A: 

In SQL Server 2008 you can use table value parameters.

These allow you to pass in a table of values to a stored procedure and treat it as any other table (use in sub-queries, joins etc).

In terms of the query - if you do use a table value parameter, you can query it for size (how many rows), use IN in conjunction with a GROUP BY on the ID field and a HAVING that counts the number of rows.

Oded
Another approach is to temp tables as input/output for a stored procedure, but table variables if applicable are a cleaner solution.
Joel Goodwin
Thank YouIt Worked :SELECT CMC_IDFROM [CommodityClass] ,[CommodityClassDynamicAttributes]WHERE (CMC_ID = CCA_CMC_ID) AND (CCA_DAT_ID IN (@CCA_DAT_ID_LIST))GROUP BY CMC_IDHAVING COUNT(CCA_DAT_ID) = @CCA_DAT_ID_COUNT