I want to get a backup of a single table with its data from a database in SQL Server using a script.
How can I do that?
ベストアンサー1
SELECT * INTO mytable_backup FROM mytable
This makes a copy of table mytable
, and every row in it, called mytable_backup
.
(It will not copy any indices, constraints, etc. Just the structure and data).
(It will fail if you have an existing table named mytable_backup
, so if you want to use this code regularly - for example, to backup daily or monthly - you'll need to run drop mytable_backup
first. But consider avoiding this in those cases, and use SQL Server's proper official built-in backup tools instead. Those will provide better features. This is more for a temporary one-time backup before running a risky update script in test, etc).