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

走远了吗. 提交于 2019-12-10 03:46:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!