sqlsql-serverselectconstraintsselect-into

Copying table constraints from select * into


Does select * into B from A also copy constraints of A on B ? If not, then how can I copy constraints?


Solution

  • No, not in SQL-Server. You would need to specify the constraints and indexes on the new table manually. If you're using SSMS, using the Script As... Create To functionality can get you a sample script to create a new table with the same constraints and structure.

    https://learn.microsoft.com/en-us/sql/t-sql/queries/select-into-clause-transact-sql?view=sql-server-ver15

    Indexes, constraints, and triggers defined in the source table are not transferred to the new table, nor can they be specified in the SELECT...INTO statement. If these objects are required, you can create them after executing the SELECT...INTO statement.