Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using an array of items to exclude from the select

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-11-09 : 08:18:59
Mihai writes "Ok, so say i have an array of results from a previous query, how can i pass those as a single parameter so that they would be excluded from the search (or included, or whatever)

example:



connection.Open();
System.Data.SqlClient.SqlCommand selectwithExclude = new System.Data.SqlClient.SqlCommand("SELECT * FROM MainTable WHERE Value != @Value", connection);
int [] values = {1,2};
selectwithExclude.Parameters.AddWithValue("@Value", values);
IDataReader reader = selectwithExclude.ExecuteReader();
int returnValue = 0;
while (reader.Read())
{
returnValue = reader.GetInt32(2);
}
reader.Dispose();
connection.Close();
MessageBox.Show(returnValue.ToString());



This for instance would throw me an exception saying that "No mapping exists from object type System.Int32[] to a known managed provider native type.", exception to which i aggree, as i've shown this example simply as to demonstrate what i'm after.

I would also like to avoid building a huge query string that would include a "AND Value !=@Value1 AND Value != @Value2" and so on, as the search results can get an enormous number of items.


Thank you in advance."

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-09 : 09:04:43
pass them in as varchar and split them in the stored procedure.
split function: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-09 : 11:12:50
Also refer
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -