[英]SQL Server - Creating a table with a row of strings
I have a list of names I want to put into a temp/variable table but can not think of how to do it. 我有一个要放入临时表/变量表的名称列表,但想不起来怎么做。
Here is a dummy list.... 这是一个虚拟列表。
NAMES
McLovin
Deckard
Mufasa
DeLarge
This is a string I'll get sent to me which I need to wash against actual tables so I wanted to place this in a temp field but I just can not think of how to do it. 这是我将要发送给我的字符串,需要在实际表上进行清洗,因此我想将其放置在temp字段中,但我只是想不出该怎么做。
CREATE TABLE [#test] (
NAMES varchar (20));
INSERT INTO [#test] ([NAMES])
VALUES ('McLovin');
INSERT INTO [#test] ([NAMES])
VALUES ('Deckard');
The above is the only way I can think of doing it. 以上是我想到的唯一方法。 I am new to SQL so am still learning the logic. 我是SQL新手,因此仍在学习逻辑。 Any guidance would be much appreciated. 任何指导将不胜感激。
Cheers. 干杯。
See This: Inserting multiple rows in a single SQL query? 请参见: 在单个SQL查询中插入多行?
INSERT INTO #test ([NAMES])
VALUES ('McLovin'), ('Deckard'), ('Mufasa'), ('DeLarge')
I want to add to Tedo's answer that you might not need a temporary table at all. 我想补充一下Tedo的答案,即您可能根本不需要临时表。 You can include these directly in a query: 您可以将这些直接包含在查询中:
select . . .
from (VALUES ('McLovin'), ('Deckard'), ('Mufasa'), ('DeLarge')) v(names)
Or use a CTE: 或使用CTE:
with names(name) as (
select *
from (VALUES ('McLovin'), ('Deckard'), ('Mufasa'), ('DeLarge'))
)
select . . .
Here is the fastest on the fly way: 这是运行中最快的方式:
SELECT CONVERT(VARCHAR(20), 'McLovin') NAMES
INTO #temp
UNION
SELECT 'Deckard'
UNION
SELECT 'Mufasa'
UNION
SELECT 'DeLarge'
No need to write create statements too. 也无需编写create语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.