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 2008 Forums
 SSIS and Import/Export (2008)
 perform 1000 selects

Author  Topic 

nirnir
Starting Member

10 Posts

Posted - 2013-02-03 : 05:15:10
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-03 : 06:30:59
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

52326 Posts

Posted - 2013-02-03 : 10:51:48
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 - 2013-02-04 : 01:05:19
Thanks Guys,
I'll check it
Go to Top of Page
   

- Advertisement -