I have 2 databases that are identical. I make a backup of DB1 and restore as DB2.
2 days later I want to copy 5 tables from DB1 into DB2. All tables start with 'AG'. Is there a simple way of doing this? I would like to do this in a T-SQL script so I can do this often and easily.
SQL Server – How to Copy Specific Tables Between Databases on the Same Server
sql server
Related Question
- SQL Server – How to Copy Database and Clear Data Older Than N Days
- SQL Server Restored Database – Differences from Original
- Sql-server – What’s the best method to refresh only a few tables within a test database from production
- How to Copy One Database to Another on the Same Server in SQL Server
- Sql-server – Restore Database from Database with Different Name
- SQL Server – Copy Tables from sys.tables to New Database
Best Answer
Below script will help you :
Another method would be to script out schema and then use BCP OUT and BULK INSERT INTO the destination database. BCP method will be much faster when you have large number of data to move.
Other alternatives are SSIS (when you have to repeatedly do the same task often) or PowerShell.
Make sure to empty (delete rows if the tables have foreign key relationship or truncate them if they dont have). Be careful with identity fields when you use delete or truncate.