Connecting Tech Pros Worldwide Forums | Help | Site Map

How to use IN () syntax in asp.net datasets (xsd)

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 21 '09
Hi,

I use Visual studio datasets to create table adapters etc. for queries. This normally works fine for me with 'simple' queries (i.e. only 1 parameter of type string, int ...). However if I specify the following query in the dataset:

SELECT * from tblTable WHERE tblId IN (@idList)

the query editor accepts, but the generated code will create a function that just accepts a single integer. Changing the type of idList to string and passing in a string in the form of "1,2,3" also doesn't work.

Is it at all possible to use the IN keyword in queries using the datasets, and if so, what is the way to do this?

Thanks very much in advance!

Arthur

MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 283
#2: Sep 21 '09

re: How to use IN () syntax in asp.net datasets (xsd)


Hi Arthur,

I'm not quite sure what you're trying to accomplish with your query, but it sounds to me like you can use a subquery. Can you explain what format your table has and what output do you expect?

Steven
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 21 '09

re: How to use IN () syntax in asp.net datasets (xsd)


Hi Steven,

I like to retrieve records of which the ID is one of the IDs i supply..

so

SELECT * FROM tblTable WHERE id IN (1,2,3,4,5)

would be the thing I'm after...

However if I try this with

SELECT * FROM tblTable WHERE id IN (@idList)

I don't get it to work ...

Thanks!

Arthur
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 124
#4: Sep 21 '09

re: How to use IN () syntax in asp.net datasets (xsd)


check these methods -

DataView.RowFilter

DataTable.Select
Newbie
 
Join Date: Sep 2009
Posts: 3
#5: Sep 22 '09

re: How to use IN () syntax in asp.net datasets (xsd)


Thanks ssnaik84!

However I cant help but thinking that these functions do a 'non' MSSQL operation on the dataset. Ie aren't these functions potentially slow on large result sets?
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 124
#6: Sep 22 '09

re: How to use IN () syntax in asp.net datasets (xsd)


ok.. you need MSSQL function then..

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION [dbo].[GetTableFromCSV] ( @StringInput VARCHAR(100) )
  2. RETURNS @OutputTable TABLE ( [id] int identity(1,1), [String] NVARCHAR(4000) )
  3. AS
  4. BEGIN
  5.  
  6.     DECLARE @String    VARCHAR(10)
  7.  
  8.     WHILE LEN(@StringInput) > 0
  9.     BEGIN
  10.         SET @String      = LEFT(@StringInput, 
  11.                                 ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
  12.                                 LEN(@StringInput)))
  13.         SET @StringInput = SUBSTRING(@StringInput,
  14.                                      ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
  15.                                      LEN(@StringInput)) + 1, LEN(@StringInput))
  16.  
  17.         INSERT INTO @OutputTable ( [String] )
  18.         VALUES ( @String )
  19.     END
  20.  
  21.     RETURN
  22. END
to run it..

Expand|Select|Wrap|Line Numbers
  1. select * from dbo.GetTableFromCSV('1,2,3')
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 124
#7: Sep 22 '09

re: How to use IN () syntax in asp.net datasets (xsd)


Quote:

Originally Posted by arthurvanderwal View Post


SELECT * from tblTable WHERE tblId IN (@idList)


Expand|Select|Wrap|Line Numbers
  1. SELECT * from tblTable WHERE tblId IN (
  2. select id from dbo.GetTableFromCSV('1,2,3')
  3. )
  4.  
Reply

Tags
asp.net, dataset, mssql