Question

As you know, we can use 'select into table' to copy table structure in sql server, but there's a problem when the column of source table has default value, the default value setting is missing in the newly created table.

So, how can I keep the default value setting? any clue? thanks.

Was it helpful?

Solution

I hate to break it to you, but that's NOT the only thing missing. I assume by "the default value setting" you actually mean default constraints, i.e. Specify Default Values for Columns or Defaults.

The table is created using the most basic structure and does not copy (at least the following)

  • clustered index
  • any other index for that matter
  • referential constraints
  • any other constraints for that matter

It makes more sense when you consider this scenario

select a.clustered_id a_id, b.clustered_id b_id, b.name
  from tablea a
  join tableb b on ....

Should it create a clustered index on a_id or b_id (which may not even be unique after this JOIN)!? Granted you're thinking of the scenario of a single table (direct clone), but that really is a very specific use case for a generic functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top