skip to Main Content

How to select more than one value from a ‘non-existing’ table.

For example:

SELECT ('val1', 'val2') as "test"

enter image description here

How to make a column "test" with two values/rows val1 and val2?

3

Answers


  1. you can use union two rows in table like below code:

    select 'val1' as "test"
    union
    select 'val2' as "test"
    

    enter image description here

    Login or Signup to reply.
  2. If you use SQL Server family, you can use a query similar to this :

    declare @tmpTable as table (test varchar(50))
    
    
    insert into @tmpTable
    select 'val' Go 100
    

    now you can use @tmpTable

    Login or Signup to reply.
  3. To temporarily generate rows with values, PostgreSQL has following options:

    1. VALUES Lists like VALUES (1, 'one'), (2, 'two'), (3, 'three');
    2. WITH Queries (Common Table Expressions) common-table-expression
    3. CREATE TABLE TEMPORARY to create a table for temporary use in this session

    For small numbers of rows I would suggest the VALUES expression.
    For more rows and usage in complex queries I would suggest the temporary table.

    See also:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search