SELECT * INTO Table2
FROM Table1
WHERE 1=2
What’s the point of WHERE 1=2 for SELECT INTO table query?
You add the WHERE 1=2 clause to the SELECT INTO query so that you can make a copy of the table’s structure without any actual data.
If you did this:
SELECT *
INTO Table2
FROM Table1
Table2 will be an exact copy of Table1, including all the data rows.
If you’re only interested in the structure of Table1 and not its data, you can use a WHERE clause to filter out all the data from the table. This way, you’ll obtain the table’s structure without any actual data.
SELECT *
INTO Table2
FROM Table1
WHERE 1=2
When your WHERE clause doesn’t yield any rows as a result, no data will be inserted into the new table. As a result, you’ll achieve an identical schema to the original table, but without any data. This outcome is what you’re aiming for in this scenario.
The same effect can be achieved with TOP (0), for example:
SELECT TOP (0) *
INTO Table2
FROM Table1;
Note: The SELECT INTO will not duplicate the source table’s indexes, constraints, triggers, or partition schema.
Putting it all together, when you use SELECT INTO
with WHERE 1=2
, you are essentially creating an empty table that has the same structure as the source table but doesn't contain any data. It's a quick way to clone the structure of a table without copying any records.