How to create mirrored temporary table in another

1

Is it possible to create a temporary table that is mirrored in another?

Example: Let's say I have in my bank a table with 60 fields. I would like to create a temp named #tabela with the same structure as the table in the database, but without typing the field names and types again. Is this possible?

    
asked by anonymous 19.04.2017 / 14:04

4 answers

1

Simply:

SELECT * INTO #tabela
FROM TabelaOrigem
    
19.04.2017 / 14:07
2

To create an identical structure, but without any content, you can use the following form:

-- código #1
IF Object_ID('tempDB..#tabela','U') is not null DROP TABLE #tabela;

SELECT *
  into #tabela
  from minhatabela
  where 1 = 0;

Because the WHERE clause restriction is false, only the structure will be copied.

    
19.04.2017 / 14:53
1

View can be defined as a virtual table composed of rows and columns of data coming from related tables in a query (a grouping of SELECT's, for example).

CREATE VIEW active_users AS
SELECT id, username, ...
FROM users
WHERE active=True

In this way you will have a virtual table called active users

link

    
19.04.2017 / 14:06
1

The action of creating a temporary table based on another table in SQL Server can be done during any select any. You can create referencing the entire table or just a few fields.

Example:

SELECT * INTO #TEMP FROM TABELA

You can test the result immediately by checking the records in the temporary table.

SELECT * FROM #TEMP;

If you want only one sample of the table, you can impose a limit on the type select SELECT TOP 100 or SELECT TOP 1000 etc.

Remembering that you will not be "copied" by CONSTRAINT .

    
19.04.2017 / 14:13