0

In SQL, if I want to select a hardcoded value, I can do SELECT 1; for example, which would return a table with one column and one row with value 1. How can I select the hardcoded array in SQL?

So, I want the output to contain one column with two rows, say with values 1 and 2. How can I do that?

I tried SELECT 1, 2; but that returns a table with one row but two columns, what I do not want. I also tried SELECT (1, 2); and SELECT IN(1, 2); but none of them works.

I use the Microsoft Structured Query Language server.

7
  • 3
    I guess you want SELECT 1 UNION ALL SELECT 2, but what's the purpose of that strange idea? Commented Sep 21, 2023 at 16:14
  • This depends on the version of sql server you are using. Anything earlier than 2022 I would use a tally table. For 2022 I would use GENERATE_SERIES. In any version using a hard coded list of values is rather cumbersome and strange. Commented Sep 21, 2023 at 16:26
  • 2
    MS SQL server doesn't support array data type, if you meant to get multiple rows from values, then you could use from (values (1), (2), (3) ...) t(ColName) syntax. Commented Sep 21, 2023 at 16:37
  • maybe this could help stackoverflow.com/questions/1732613/… Commented Sep 21, 2023 at 16:56
  • 1
    Are you looking for the TABLE VALUE CONSTRUCTOR? SELECT a,b FROM (VALUES (1,2)) a (a,b) learn.microsoft.com/en-us/sql/t-sql/queries/… Commented Sep 21, 2023 at 18:07

1 Answer 1

4
select *
from (values (1),(2)) v(value)

or

select 1 value
union all
select 2 value

or (for SQL Server 2022+ and Azure SQL)

select value
from generate_series(1,2)
Sign up to request clarification or add additional context in comments.

1 Comment

Hurray for generate_series(). Now just to wait for my vendor to support 2022.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.