仅使用 SELECT 语句模拟具有多行的表 [英] Simulate a table with multiple rows just with SELECT statement

查看:40
本文介绍了仅使用 SELECT 语句模拟具有多行的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我可以执行以下select语句来创建一个具有一个值的表

If I can do the following select statement to create a table with one value

 SELECT 'myname' AS 'Name' 

这将返回一个列 = Name 和一个值 = myname 的表

this will return a table with column = Name and one value = myname

我该如何解决这个问题,以便仅从 select 语句中返回具有多个值的一列

how can I work around this to return one column with multiple values from just the select statement

我不想这样做:

 DECLARE @tmp TABLE (Name varchar(50)) 

 INSERT INTO @tmp (Name) VALUES ('myname1'),('myname2') 
    SELECT * FROM @tmp

只要来自单个 SELECT 语句

推荐答案

如果您想仅使用 SELECT 语句来模拟具有多行的表,通常可以使用 来完成UNION 行:

If you're wanting to simulate a table with multiple rows just with SELECT statement, this can typically be done with UNION of rows:

SELECT 'myname1' AS 'Name' UNION
SELECT 'myname2' UNION
SELECT 'myname3'
-- etc

演示:http://www.sqlfiddle.com/#!3/d41d8/12433

这篇关于仅使用 SELECT 语句模拟具有多行的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆