Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate Table Definition (columns) in SQL-Server

Tags:

sql

sql-server

Is there a quick and easy way to duplicate a table in SQL-Server? I just want to make a temporary table from table A because I need to refresh table B (which will interfere with the foreign keys).

I know this isn't a ridiculous task to do manually, but it seems like there should be a nice way to do it.

Edit: To clarify, I want to duplicate the table definition, not the actual data.

like image 525
jtpereyda Avatar asked Jan 29 '26 17:01

jtpereyda


2 Answers

Try this?

SELECT
  *
INTO
  #temp
FROM
  myTable
WHERE
  1=0

It won't duplicate constraints, indexes, etc. But it should duplicate the data-types and field names.

like image 185
MatBailie Avatar answered Jan 31 '26 06:01

MatBailie


Wouldn't it be a better approach to just temporarily disable constraints while you are doing your refresh

 ALTER TABLE X NOCHECK CONSTRAINT ALL
 -- Refresh SQL 
 ALTER TABLE X CHECK CONSTRAINT ALL
like image 21
Simen S Avatar answered Jan 31 '26 06:01

Simen S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!