Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I create a temp table in SQL Server when I have a big list of ID's

I have a list of raw IDs that I am supposed to put into a temp table. I am unsure about how this works in SQL Server:

I know the general format:

select PID into #myPIDs
from ... ?

I already have a list of about 30 PIDs which I am to use. They look like so:

'U388279963',
'U388631403',
'U389925814'

How do I go about doing this? Thanks!

like image 574
Caffeinated Avatar asked Aug 14 '12 18:08

Caffeinated


People also ask

How big can be the temp table in SQL Server?

No, there is no records limit for temporary table (the limit is the disk space). But be careful, because temporary tables are physically created in tempdb database, and this database must be placed on the disk with appropriate size.

How do I create a temporary table in SQL Server?

To create a Global Temporary Table, add the “##” symbol before the table name. Global Temporary Tables are visible to all connections and Dropped when the last connection referencing the table is closed. Global Table Name must have an Unique Table Name.

What is the maximum name length for temporary table type?

Question: All objects can have minimum 1 and maximum of 128 characters in their names in SQL Server. Only exception is local, temporary tables that can have a maximum of 116 characters.


1 Answers

Your format will work for creating a new temp table for one insert. If you need to copy and paste your IDs then the following will work.

CREATE TABLE #myPIDs
(
     PID VARCHAR(30)
);

INSERT INTO #myPIDs
VALUES
(....),
(....);

Copy and paste your PIDs and use find and replace with regular expressions to replace each line with the regular expression option checked. Remove the last ',' and you're good.

Find - ^{U:z+}$

Replace - ('\1'),\n

Alternatively you can have sql server read your ids from a file on the system. If you elaborate on your needs I can give you a better answer.

like image 98
jtimperley Avatar answered Sep 18 '22 13:09

jtimperley