Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Inserting a list of ints into a temporary table

Tags:

sql-server

I have a list of IDs in a text file like this:

24641985 ,
24641980 ,
24641979 ,
24641978 ,
24641976 ,
24641974 ,
...
...
24641972 ,
24641971 ,
24641970 ,
24641968 ,
24641965)

There's tens of thousands of them.

Now I need to know which ids are in this list, that do not correspond to an ID in my table.

I guess I should put them into a temporary table, then say something like:

select theId 
  from #tempIdCollection
 where theId not in (select customerId from customers)

Problem is I don't know how to get them into the temp table!

Can anyone help? This doesn't have to be efficient. I've just got to run it once. Any solution suggestions welcome!

Thanks in advance!

-Ev

like image 646
Ev. Avatar asked May 06 '11 00:05

Ev.


People also ask

How do I insert a list into a table in SQL?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.


1 Answers

I'd use a table variable. You declare it just like a regular variable.

declare @tempThings table (items int)
insert @tempThings values (1)
like image 116
Kirk Broadhurst Avatar answered Oct 10 '22 01:10

Kirk Broadhurst