问题
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!
回答1:
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.
回答2:
insert into #myPIDs (ID)
select 'U388279963'
union
select 'U388631403'
union
select 'U389925814'
回答3:
If you original table (from witch comes IDs) is accessible and always have a row with each ID :
SELECT originalTable.ID
INTO #myPIDs
FROM originalTable
WHERE originalTable.ID IN ('U388279963', 'U388631403', 'U389925814')
来源:https://stackoverflow.com/questions/11958710/how-would-i-create-a-temp-table-in-sql-server-when-i-have-a-big-list-of-ids