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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 perform 1000 selects
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

10 Posts

Posted - 02/03/2013 :  05:15:10  Show Profile  Reply with Quote
I have an array (in delphi) of 1000 pairs of keys , I need for each pair to check if it's exist in the table and get another field from the table
The most simple way would be to loop and select like
for i:=1 to 1000 do
sqlString='select anotherField from table_ where key1=vec[i].key1 and key2=vec[i].key2'

That will send 1000 queries to the sqlserver
Any idea how to make it more efficient

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 02/03/2013 :  06:30:59  Show Profile  Reply with Quote
Obviously you need to be able to pass the equivalent of a a two-column table to your stored procedure. There are two (or may be more) different approaches to doing this. See a discussion on this blog (not all of them may be applicable to you because it discusses sharing data between stored procedures). You MAY be able to use the XML method or table valued parameters.

If you want to use the XML method, compose the key-value pairs into an XML fragment, for example, like the one shown below:
Now pass that to a stored procedure and use it like this:
	table_ t1
		c1.value('k[1]','int') AS [Key],
		c1.value('v[1]','varchar(64)') AS [Value]
		@xmlparam.nodes('kvpairs') T(c)
		CROSS APPLY c.nodes('kv') T1(c1)
	) t2 ON t2.[Key] = t1.key1 AND t2.[Value] = t2.key2
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 02/03/2013 :  10:51:48  Show Profile  Reply with Quote
in ssis you could use variable of type object to hold these 1000 pairs of values and then use a for each loop with ADO Recordset enumerator to iterate through all the variables and fetch the required related data.

SQL Server MVP

Go to Top of Page

Starting Member

10 Posts

Posted - 02/04/2013 :  01:05:19  Show Profile  Reply with Quote
Thanks Guys,
I'll check it
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000