简体   繁体   English

仅使用脚本在MS SQL Server之间移动多个表

[英]Moving multiple tables between MS SQL Servers using only script

How can I move multiple tables between SQL servers (but not every table of the database)? 如何在SQL服务器之间移动多个表(而不是数据库的每个表)?

I have a list of the tables that need to be copied. 我有一个需要复制的表的列表。 Is there any way to create an enum (or something like that) from this list, and loop through the items, to create a dynamic script? 有什么方法可以从此列表中创建一个枚举(或类似的东西),并遍历所有项以创建动态脚本吗?

I already created a Linked Server connection, the script should work with this method. 我已经创建了一个链接服务器连接,该脚本应与此方法一起使用。 Identity insert also need to be used in the script. 标识插入也需要在脚本中使用。

What is the easiest way to do this? 最简单的方法是什么?

You only should take care of the @dest and @tables. 您只应注意@dest和@tables。

declare @tables varchar(MAX)
declare @t varchar(50)
declare @sql varchar(MAX)
declare @dest varchar(80)

set @dest = 'myNewServer.newDb..';
set @tables = '[Table1], [Table2]'


while len(@tables) > 0
begin
  --print left(@tables, charindex(',', @tables+',')-1)
  set @t = left(@tables, charindex(',', @tables+',')-1)
  set @sql = 'select * into "+ @dest + @t + ' from '+@t
  set @tables = stuff(@tables, 1, charindex(',', @tables+','), '')

  execute(@sql)

end

This methods copies all the tabes data, preserving IDs because: 此方法复制所有Tabes数据,并保留ID,因为:

"... this doesn't copy any of the metadata other than the column information (names & datatypes). So it will not have security permissions, indexes, triggers, constraints. etc. That said the Management Studio example above also has the same problem." “ ...除了列信息(名称和数据类型)外,它不会复制任何其他元数据。因此,它不会具有安全权限,索引,触发器,约束等。这表示上述Management Studio示例还具有同样的问题。” from https://msdn.microsoft.com/en-us/library/aa337553.aspx 来自https://msdn.microsoft.com/en-us/library/aa337553.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM