• Temp tables are created just like a persistent table- the difference is you do not typically specify the schema and you put a # before the name, such as:

    CREATE TABLE #MyTempTable (id int, firstName varchar(100) NULL, lastName varchar(200) NOT NULL)

    INSERT INTO #MyTempTable ...

    SELECT ...

    FROM #MyTempTable ...

    Sometimes you just want to "dump" the results of a query into a temp table (though I would not do this in production code- and this only works if the temporary table doesn't already exist):

    SELECT {some stuff}

    INTO #MyTemporaryTable

    FROM {some things}

    ...

    The temp table will exist for the duration of the connection (and can only be accessed by the connection that created the temp table). As soon as you disconnect, the temp table is effectively gone. The temp table is actually a real table that is created in the system db "tempdb".

    If you precede the name with ## instead of #, you will create a global temporary table. A global temporary table will exist until the server is restarted (or the table is dropped). A global temporary table can be accessed from any connection.

    An alternative is a table-type variable. These are declared like variables and exist for the duration of the BATCH (slightly different than temp tables that exist for the duration of the connection).

    DECLARE @MyTableTypeVariable TABLE (id int, firstName varchar(100) NULL, lastName varchar(200) NOT NULL)

    INSERT INTO @MyTableTypeVariable...

    SELECT...

    FROM @MyTableTypeVariable...

    Whenever possible, I use table type variables (especially in production code) because, true or not, I have the impression that it requires the least amount of overhead on the server.

    Here's an article that goes into much more depth:

    https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/

    I wonder if what you really want is a VIEW? A view is a logical construct (does not contain any data). It's basically a query that you can refer to as if it is a table.

    CREATE VIEW dbo.SalesSummary

    AS

    SELECT {some complex query}

    GO

    Then at some later point (a year from now, for instance) you could refer to the view:

    SELECT *

    FROM dbo.SalesSummary