SQL Server: collect values in an aggregation temporarily and re-use in the same query

后端 未结 3 580
终归单人心
终归单人心 2021-01-02 16:37

How do I accumulate values in T-SQL? AFAIK there is no ARRAY type.
I want to re-use the values in the same query like demonstrated in this PostgreSQL example using array

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 17:43

    If you're just collecting some values to reuse, try a table variable rather than a temp table

    DECLARE @t TABLE 
    (
        id INT PRIMARY KEY,
        name NVARCHAR(100)
    )
    
    INSERT @t VALUES (3 , 'John')
    -- etc
    

    The table variable is in-memory only, instead of going in the tempdb database like a temp table does.

    Check Should I use a #temp table or table variable for more information.

提交回复
热议问题