Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

inserting efficiently into a temp table/table variable

 
   Database Help (Home) -> Programming RSS
Next:  How to select all rows on distinct only on one co..  
Author Message
tstoneman4

External


Since: Dec 21, 2006
Posts: 1



(Msg. 1) Posted: Thu Dec 21, 2006 12:38 pm
Post subject: inserting efficiently into a temp table/table variable
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I am a newbie to Sql Server programming, and I was hoping that someone
might be able to point me in the proper direction.

I am trying to write a C function that takes an array of strings and
insert them into either a temporary table or a table variable. This
table will just be a one column table that contains this list of
strings. (If you want further background, these strings are actually
keys and I will subsequently use this temp table/table variable to do a
join with a much larger table to get a subset of rows based those keys.
I want to do this to avoid using an IN clause.)

How can I efficiently do this? If I have a string array that contains
all the keys, is there a way I could insert all these keys into the
temp table in a bulk fashion? I don't want to do tens or hundreds of
thousands of inserts into a temporary table just to do the join... is
there some other mechanism I could use other than INSERT? I saw a BULK
INSERT but this pertains to bulk loading a file it seems. Is there
some way of doing this totally in memory?

 >> Stay informed about: inserting efficiently into a temp table/table variable 
Back to top
Login to vote
Mike C#

External


Since: Jan 12, 2008
Posts: 483



(Msg. 2) Posted: Thu Dec 21, 2006 4:19 pm
Post subject: Re: inserting efficiently into a temp table/table variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

wrote in message

> How can I efficiently do this? If I have a string array that contains
> all the keys, is there a way I could insert all these keys into the
> temp table in a bulk fashion? I don't want to do tens or hundreds of
> thousands of inserts into a temporary table just to do the join... is
> there some other mechanism I could use other than INSERT? I saw a BULK
> INSERT but this pertains to bulk loading a file it seems. Is there
> some way of doing this totally in memory?

If all of your strings concatenated together with a separator character
(like a comma) between them is 8,000 characters or less you can pass the
list as one long VARCHAR comma-separated string (or other separator) to a
stored proc and split them SQL-Server side. Erland Sommarskog has an
article on this: http://www.sommarskog.se/arrays-in-sql.html

If you're using SQL 2K5 you could use the .NET 2.0 SqlClient Bulk Insert
functionality to do this also, although for the work you might not see a
huge performance gain over the above method. Another option, if you want to
do Bulk Insert, is to use the old-school bulk insert API. ODBC, OLE DB, and
even DB-Lib provide access to bulk insert from variables in memory. They
can all be a bit of a hassle to use, however.

 >> Stay informed about: inserting efficiently into a temp table/table variable 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
inserting to temp table - SQL 2k. Given the following: CREATE TABLE Customers (GroupingNum INT, CustomerName VARCHAR(50)) INSERT INTO Customers (GroupingNum, CustomerName) VALUES (0, 'Customer Name') -- this is a row of column headers ALTER TABLE Customers ADD ID [int]..

Multiple inserts inserting @@identity into the temp table? - Hi! I'm putting together values into a temp table, then I'm updating table Subscriptions with the data in my temp table. The problem is I need the @@identity in return from the inserts, inserted into the temp table for later use. So this is how I would...

Table variable performance vs. temp table - Hi, I've got an identical query, one with temp tables and one with table variables that results in two completely different execution times. The plans look virtually the same but execution time is around 2 minutes with the variables and less than a..

table variable - get the structure from an existing table - Hi all, Is it possible to declare a table variable and get the structure from an existing table? Something like "select * into @my_table" or any other recommended way Thanks, Avi

Inserting Data into Sql Table - I have a table (OEPCAN) that has 3 columns of data: First column is Company Number (DICOMP) Second column is Alternate Customer Number (DIF1NS) Third Column is Status (DIOLRY) I have a text file of 20, 000 numbers beginning with 9900000 that I need to ...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]