SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

nirnir
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'
end

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

James K
Flowing Fount of Yak Knowledge

3741 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:
<kvpairs>
  <kv>
    <k>1</k>
    <v>Smith</v>
  </kv>
  <kv>
    <k>2</k>
    <v>Jones</v>
  </kv>
  <kv>
    <k>3</k>
    <v>Murray</v>
  </kv>
</kvpairs>
Now pass that to a stored procedure and use it like this:
SELECT
	anotherfield 
FROM
	table_ t1
	INNER JOIN 
	(
		SELECT
		c1.value('k[1]','int') AS [Key],
		c1.value('v[1]','varchar(64)') AS [Value]
	FROM
		@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

visakh16
Very Important crosS Applying yaK Herder

India
52325 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
http://visakhm.blogspot.com/

Go to Top of Page

nirnir
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  
 New 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.05 seconds. Powered By: Snitz Forums 2000