对于一个特定的问题,我希望表格值如下,
number | number_in_words
--------------------------
1 | one
2 | two
3 | three
4 | four
5 | five
6 | six
7 | seven
8 | eight
9 | nine
0 | zero
但是数据库中没有这样的表,我不应该在DB中创建表。
使用下面的子查询,我得到了只有一行的表,有什么办法可以使用类似的查询得到整个上表吗?
(select '1' as number, 'one' as number_in_words)
答案 0 :(得分:3)
你可以这样做,通过使用 select from values 在子查询中准备它:
(SELECT * FROM (VALUES (1,'one'),(2,'two'),(3,'three'),(4,'four'),(5,'five')) numbertable(number,number_in_words))
答案 1 :(得分:1)
我不认为您可以在 DML 中使用 DDL(创建表),请尝试检查“WITH”公用表表达式,在幕后,WITH 子句将创建一个临时表,您可以在子查询中引用它。
答案 2 :(得分:1)
您也可以使用表变量。
DECLARE @lookup TABLE (numVal INT NOT NULL, strVal varchar(10))
INSERT INTO @lookup(numVal, strVal) VALUES (1, 'one'), (2, 'two'), (3, 'three') --etc
-- and then use the value similar to a normal table using joins or whatever
select l.strVal --, other values
from yourTable as yt INNER JOIN @lookup as l ON yt.numericValue = l.numVal
答案 3 :(得分:0)
任何可以查询数据库的人都可以制作临时表。研究临时表,你可以制作一个完全一样的。
CREATE TABLE #temp (number int PRIMARY KEY, number_in_words varchar(10))
INSERT INTO #temp (number, number_in_words) VALUES
(1, 'one')
,(2, 'two')
,(3, 'three')
等
然后当你完成后:
DROP TABLE #temp