Home All Groups Group Topic Archive Search About
Author
23 Apr 2006 3:32 PM
Sreppohcdoow
Is there a way to select to find multiple conditions?

I.e., in SQL, you can typically SELECT from myTable where someColumn in
('x','y','z')
instead of passing in explicit strings, I'd like to pass in an array of
values, is this possible, i.e., perhaps just a single column as a dataview?

Can a nested select be used as can be used in SQL?

I haven't seen may in-depth Datatable.Select examples that demonstrate such
functionality... if they are out there, please point the way!

Thx,
MS

Author
23 Apr 2006 6:36 PM
William (Bill) Vaughn
Sure. First consider that the IN clause can't accept a parameter so you'll
have to deal with this in another way. One approach that I discuss in my new
book is to create a Table-Value Function that converts a delimited list into
a SQL Table-datatype variable. This is passed to the IN clause with a SELECT
as in:

.... WHERE myValue IN (SELECT valCol FROM myTVF(@DelimitedListParm))

This TVF can be implemented in TSQL or by using a CLR function--both are
illustrated in the book... and elsewhere on the web.
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Show quote
"Sreppohcdoow" <Sreppohcd***@asdf.com> wrote in message
news:%23pTC3suZGHA.4788@TK2MSFTNGP02.phx.gbl...
> Is there a way to select to find multiple conditions?
>
> I.e., in SQL, you can typically SELECT from myTable where someColumn in
> ('x','y','z')
> instead of passing in explicit strings, I'd like to pass in an array of
> values, is this possible, i.e., perhaps just a single column as a
> dataview?
>
> Can a nested select be used as can be used in SQL?
>
> I haven't seen may in-depth Datatable.Select examples that demonstrate
> such functionality... if they are out there, please point the way!
>
> Thx,
> MS
>
>
Author
24 Apr 2006 7:27 AM
Miha Markic [MVP C#]
Hi,

Yes, it sucks, I know. There are several ways but perhaps the most easy one
is to use dynamically built sql command if your list consists of numbers
(there is no danger of sql injection in this case as you can check whether
only numbers are being passed).

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Sreppohcdoow" <Sreppohcd***@asdf.com> wrote in message
news:%23pTC3suZGHA.4788@TK2MSFTNGP02.phx.gbl...
> Is there a way to select to find multiple conditions?
>
> I.e., in SQL, you can typically SELECT from myTable where someColumn in
> ('x','y','z')
> instead of passing in explicit strings, I'd like to pass in an array of
> values, is this possible, i.e., perhaps just a single column as a
> dataview?
>
> Can a nested select be used as can be used in SQL?
>
> I haven't seen may in-depth Datatable.Select examples that demonstrate
> such functionality... if they are out there, please point the way!
>
> Thx,
> MS
>
>
Author
24 Apr 2006 4:03 PM
Peter Johnson
My preferred method for this is to build an xml string within the
calling application and then pass that into the stored procedure

from there you can swiftly parse the xml using built in sql server
functions and then use the results in an IN statement.

See this knowledge base article for further information: -
http://support.microsoft.com/default.aspx?scid=kb;en-us;555266
Author
24 Apr 2006 5:40 PM
Miha Markic [MVP C#]
Good solution, but you need a stored procedure...

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Peter Johnson" <peternjohn***@gmail.com> wrote in message
news:1145894621.035392.264490@i40g2000cwc.googlegroups.com...
> My preferred method for this is to build an xml string within the
> calling application and then pass that into the stored procedure
>
> from there you can swiftly parse the xml using built in sql server
> functions and then use the results in an IN statement.
>
> See this knowledge base article for further information: -
> http://support.microsoft.com/default.aspx?scid=kb;en-us;555266
>
Author
24 Apr 2006 9:25 PM
Garth Wells
Try this...


-----
SET NOCOUNT ON

CREATE TABLE Employees
(
EM_ID int PRIMARY KEY IDENTITY,
EM_Class char(1) NOT NULL
);

INSERT Employees VALUES ('A');
INSERT Employees VALUES ('B');
INSERT Employees VALUES ('C');
INSERT Employees VALUES ('D');
go

CREATE PROCEDURE pr_Employees_GetByClass
@EM_Class varchar(100)
AS

DECLARE @T1 TABLE (F1 varchar(100))
INSERT @T1
SELECT @EM_Class


WHILE (CHARINDEX(',',@EM_Class)>0)
BEGIN
  INSERT INTO @T1 (F1)
  SELECT VALUE = LTRIM(RTRIM(SUBSTRING(@EM_Class,1,CHARINDEX(',',@EM_Class)-1)))
  SET @EM_Class =
SUBSTRING(@EM_Class,CHARINDEX(',',@EM_Class)+LEN(','),LEN(@EM_Class))
END


SELECT *
FROM Employees
WHERE EM_Class IN (SELECT F1 FROM @T1)
go

EXEC pr_Employees_GetByClass 'A,C,D,'

-- Results --

EM_ID       EM_Class
----------- --------
1           A
3           C
4           D



Show quote
"Sreppohcdoow" <Sreppohcd***@asdf.com> wrote in message
news:#pTC3suZGHA.4788@TK2MSFTNGP02.phx.gbl...
> Is there a way to select to find multiple conditions?
>
> I.e., in SQL, you can typically SELECT from myTable where someColumn in
> ('x','y','z')
> instead of passing in explicit strings, I'd like to pass in an array of
> values, is this possible, i.e., perhaps just a single column as a dataview?
>
> Can a nested select be used as can be used in SQL?
>
> I haven't seen may in-depth Datatable.Select examples that demonstrate such
> functionality... if they are out there, please point the way!
>
> Thx,
> MS
>
>

AddThis Social Bookmark Button