這篇短文將介紹幾種拷貝 SQL Server 表的方法。第一種方式是最簡單的在同一個數據庫里將表拷貝到另外一個表。你需要記住的是,拷貝表的時候並不會拷貝表的約束和索引。下面是代碼模板和簡單的使用方法:
1 select * into <destination table> from <source table> 2 3 Example: 4 Select * into employee_backup from employee
我們也可以只拷貝某些字段:
1 select col1, col2, col3 into <destination table> 2 from <source table> 3 4 Example: 5 Select empId, empFirstName, empLastName, emgAge into employee_backup 6 from employee
下面的方法僅拷貝表結構,不包含數據:
1 select * into <destination table> from <source table> where 1 = 2 2 3 Example: 4 select * into employee_backup from employee where 1=2
而下面方法可將表拷貝到另外的 SQL Server 服務器上:
select * into <destination database.dbo.destination table> from <source database.dbo.source table> Example: select * into Mydatabase2.dbo.employee_backup from mydatabase1.dbo.employee
