SQL数据传输

发布于 2024-07-11 10:12:24 字数 55 浏览 9 评论 0原文

我需要将数据从一个表传输到另一台已被截断的服务器中的同一个表。 做到这一点最简单的方法是什么?

I need to transfer data from one table to the same table in another server which has been truncated. What is the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

£冰雨忧蓝° 2024-07-18 10:12:24

设置链接服务器,然后在目标数据库:

INSERT INTO existingTable (col1,col2..)

SELECT col1,col2...
FROM linkedserver.dbo.database.othertable

Setup linked servers and then use the following on the destination database:

INSERT INTO existingTable (col1,col2..)

SELECT col1,col2...
FROM linkedserver.dbo.database.othertable
痞味浪人 2024-07-18 10:12:24

使用 SQL Server 导入和导出向导。 这可能是完成此任务的最简单方法。

对于更高级的数据传输,请考虑使用 bcp 实用程序、BULK INSERT 语句和 OPENDATASOURCE

Use the SQL Server Import and Export wizard. It's probably the easiest way to accomplish this task.

For more advanced data transfer, consider using bcp utility, BULK INSERT statement and OPENDATASOURCE.

天冷不及心凉 2024-07-18 10:12:24

将一台服务器上的表备份到一个文件,然后将该文件恢复到另一台服务器上的空表中...

Back up the table on the one server, to a file, and restore that file into the empty table on the other one...

你对谁都笑 2024-07-18 10:12:24

我会使用数据转换服务(又名集成服务)。

I would use Data Transformation Services (aka Integration Services).

海的爱人是光 2024-07-18 10:12:24

取决于数据传输的数量和频率。 如果这是一个低容量的一次性过程,您最好使用 T-SQL 直接插入数据。 这可以通过链接服务器或 OPENQUERY 子句来完成。

如果一次性进程容量较大,请使用 SSIS 或 BCP 实用程序。

如果其数量大、频率高,请使用复制。

Depending on the amount and frequency of your data transfer. It it's a low volume one time process, you're better off with using T-SQL to directly insert the data. This can be done either through linked servers or OPENQUERY clause.

If its high volume one time process, use SSIS or BCP utility.

If its high volume high frequency, use replication.

爱已欠费 2024-07-18 10:12:24

这是我喜欢用于此目的的脚本。 易于使用,并且基本上没有错误。 只需将下面的文本复制到查询窗口中,然后按照说明进行操作即可。 运行它后,您将拥有一堆可以在其他服务器上运行的插入语句。


/*
Use this script to create insert statements for each row in the specified table.

Instructions:
1. Set the database you want to script from as normal.

2. change the set @TableName = '<YourTableName>' line to be the
table you want to script out.

3. Run the script and copy all the text from the results below
the line with all the dashes (----).

Notes:
   If you get the error message "Invalid object name '<YourTableName>'."
   then you either forgot to set the correct database or you spelled
   your table name wrong

Credits:
  Bob Wiechman - Fix for smalldatetime support
  Richard Lesh - correct support of uniqueidentifiers, automatic
        setting of Identity off/on, add Where clause support, more detail in
      debug mode.
*/

declare @TableName sysname
declare @WhereClause  varchar(1024)
declare @IdentityInsert int
declare @ColName sysname
declare @ColType tinyint
declare @ColStatus tinyint
declare @DebugMode bit
declare @ColList nvarchar(4000)
declare @ValList nvarchar(4000)
declare @SQL1 nvarchar(1000)
declare @SQL2 nchar(10)
declare @SQL3 nchar(1000)

set @TableName = '<YourTableName>' --  '<YourTableName>'
set @WhereClause = ''               -- limit scope of inserts
set @DebugMode = 0                  -- set to 1 if you only want a script

set @IdentityInsert = 0                -- set to 1 if you want to force IDENTITY_INSERT statements

set @ColList = ''
set @ValList = ''
set @SQL1 = 'select replace(''insert into ' + @TableName + ' ('
set @SQL2 = ') values ('
set @SQL3 = ')'', ''''''null'''''', ''null'') from ' + @TableName

if @DebugMode = 1 print '-- StmtShell: ' + @sql1 + @sql2 + @sql3

declare csrColumns cursor local fast_forward for
  select c.name, c.xtype, c.status
  from syscolumns c
    inner join sysobjects o
      on o.id = c.id
  where o.name = @TableName
    and o.xtype in ('U', 'S')
  order by ColID

open csrColumns
fetch next from csrColumns into @ColName, @ColType, @ColStatus

while @@fetch_status = 0
begin
  set @ColList = @ColList + ' ' + @ColName
  if @ColType in (173, 104, 106, 62, 56, 60, 108, 59, 52, 122, 48, 165)    -- numeric types (nulls not supported yet)
    set @ValList = @ValList + ' ''+convert(varchar(200),' + @ColName + ')+'''
  else if @ColType in (175, 239, 231, 231, 167)                            -- uid and string types
    set @ValList = @ValList + ' ''''''+isnull(' + @ColName + ',''null'')+'''''''
  else if @ColType in (58, 61)                                             -- dates (nulls not supported yet)
    set @ValList = @ValList + ' ''''''+convert(varchar(200),' + @ColName + ')+'''''''
  else if @ColType = 36                                                    -- uniqueidentfiers (nulls not supported yet)
    set @ValList = @ValList + ' ''''{''+convert(varchar(200),' + @ColName + ')+''}'''''
  if @DebugMode = 1             begin print '-- @ValList: ' + rtrim(@ValList) end
  if (@ColStatus & 0x80) = 0x80 begin set @IdentityInsert = 1 end          -- Check if column has Identity attribute
  fetch next from csrColumns into @ColName, @ColType, @ColStatus
end

close csrColumns
deallocate csrColumns

set @ColList = replace(ltrim(@ColList), ' ', ', ')
set @ValList = replace(ltrim(@ValList), ' ', ', ')

if @IdentityInsert = 1
  print 'set identity_insert ' + @TableName + ' on'

if @DebugMode = 1
  print @SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause
else
  exec (@SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause)

if @IdentityInsert = 1
  print 'set identity_insert ' + @TableName + ' off'

This is the script I like to use for this. Easy to use, and is mostly bug-free. Just copy the text below into your query window, and follow the directions. After you run it you will have a bunch of insert statements that you can run on the other server.


/*
Use this script to create insert statements for each row in the specified table.

Instructions:
1. Set the database you want to script from as normal.

2. change the set @TableName = '<YourTableName>' line to be the
table you want to script out.

3. Run the script and copy all the text from the results below
the line with all the dashes (----).

Notes:
   If you get the error message "Invalid object name '<YourTableName>'."
   then you either forgot to set the correct database or you spelled
   your table name wrong

Credits:
  Bob Wiechman - Fix for smalldatetime support
  Richard Lesh - correct support of uniqueidentifiers, automatic
        setting of Identity off/on, add Where clause support, more detail in
      debug mode.
*/

declare @TableName sysname
declare @WhereClause  varchar(1024)
declare @IdentityInsert int
declare @ColName sysname
declare @ColType tinyint
declare @ColStatus tinyint
declare @DebugMode bit
declare @ColList nvarchar(4000)
declare @ValList nvarchar(4000)
declare @SQL1 nvarchar(1000)
declare @SQL2 nchar(10)
declare @SQL3 nchar(1000)

set @TableName = '<YourTableName>' --  '<YourTableName>'
set @WhereClause = ''               -- limit scope of inserts
set @DebugMode = 0                  -- set to 1 if you only want a script

set @IdentityInsert = 0                -- set to 1 if you want to force IDENTITY_INSERT statements

set @ColList = ''
set @ValList = ''
set @SQL1 = 'select replace(''insert into ' + @TableName + ' ('
set @SQL2 = ') values ('
set @SQL3 = ')'', ''''''null'''''', ''null'') from ' + @TableName

if @DebugMode = 1 print '-- StmtShell: ' + @sql1 + @sql2 + @sql3

declare csrColumns cursor local fast_forward for
  select c.name, c.xtype, c.status
  from syscolumns c
    inner join sysobjects o
      on o.id = c.id
  where o.name = @TableName
    and o.xtype in ('U', 'S')
  order by ColID

open csrColumns
fetch next from csrColumns into @ColName, @ColType, @ColStatus

while @@fetch_status = 0
begin
  set @ColList = @ColList + ' ' + @ColName
  if @ColType in (173, 104, 106, 62, 56, 60, 108, 59, 52, 122, 48, 165)    -- numeric types (nulls not supported yet)
    set @ValList = @ValList + ' ''+convert(varchar(200),' + @ColName + ')+'''
  else if @ColType in (175, 239, 231, 231, 167)                            -- uid and string types
    set @ValList = @ValList + ' ''''''+isnull(' + @ColName + ',''null'')+'''''''
  else if @ColType in (58, 61)                                             -- dates (nulls not supported yet)
    set @ValList = @ValList + ' ''''''+convert(varchar(200),' + @ColName + ')+'''''''
  else if @ColType = 36                                                    -- uniqueidentfiers (nulls not supported yet)
    set @ValList = @ValList + ' ''''{''+convert(varchar(200),' + @ColName + ')+''}'''''
  if @DebugMode = 1             begin print '-- @ValList: ' + rtrim(@ValList) end
  if (@ColStatus & 0x80) = 0x80 begin set @IdentityInsert = 1 end          -- Check if column has Identity attribute
  fetch next from csrColumns into @ColName, @ColType, @ColStatus
end

close csrColumns
deallocate csrColumns

set @ColList = replace(ltrim(@ColList), ' ', ', ')
set @ValList = replace(ltrim(@ValList), ' ', ', ')

if @IdentityInsert = 1
  print 'set identity_insert ' + @TableName + ' on'

if @DebugMode = 1
  print @SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause
else
  exec (@SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause)

if @IdentityInsert = 1
  print 'set identity_insert ' + @TableName + ' off'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文