【问题标题】:How do I do batch upserts in SQL Server?如何在 SQL Server 中进行批量更新插入?
【发布时间】:2011-02-24 19:46:07
【问题描述】:

我正在使用 MERGE 语句来更新 sql server 2008 数据库中的行。但是,我的 sproc 是单行操作,而实际上我更喜欢批处理这些操作。这甚至可能吗?如果可以,我该怎么做?

【问题讨论】:

    标签: sql-server sql-server-2008 batch-file merge upsert


    【解决方案1】:

    你能在你的过程中使用表值参数吗?看看这里http://www.sommarskog.se/arrays-in-sql-2008.html#TVP_in_TSQL 以获得一些想法

    然后在 proc 中你可以对 TVP 使用 MERGE

    【讨论】:

      【解决方案2】:

      我创建了一个名为“upsert”的过程,它接受源表名、目标表名、要加入的字段和要更新的字段(字段用逗号分隔),然后动态合并。

      代码如下。

          CREATE proc [common].[upsert](@source nvarchar(100), @target nvarchar(100), @join_field nvarchar(100), @fields nvarchar(200))
          as
      
          --@source is the table name that holds the rows that you want to either update or insert into @target table
          --@join_field is the 1 field on which the two tables will be joined...you can only join on 1 field right now!
          --@fields are the comma separated fields that will either be updated or inserted into @target. They must be the same name in @source and @target
      
      
          declare @sql nvarchar(max)
      
          set @sql = '
              merge '+ @target +' as target
              using '+ @source +' as source
              on target.'+ @join_field +' = source.'+ @join_field +'
              when matched then
                  update set
                      ' + common.upsert_update_fields_string_builder('source', 'target', @fields) + '
              when not matched then
                  insert ('+ @join_field +', '+ @fields +')
                  values (source.'+ @join_field +',' + common.upsert_insert_fields_string_builder('source', @fields) +');
          '
      
      
          exec(@sql)
      
      
      
      
      
      
      
          CREATE function [common].[upsert_insert_fields_string_builder](@source nvarchar(100), @fields nvarchar(200))
          returns nvarchar(1000)
          as
          begin
          declare @string nvarchar(max)
      
          select @string = coalesce(
                  @string + ',' + @source + '.' + items,        
                  @source +'.' + items) 
          from common.split_string(@fields,',')
      
          return @string  
          end
      
      
      
      
      
      
      
          CREATE function [common].[upsert_update_fields_string_builder](@source nvarchar(100), @target nvarchar(100), @fields nvarchar(200))
          returns nvarchar(1000)
          as
          begin
          declare @string nvarchar(max)
      
          select @string = coalesce(
                  @string + ', '+ @target + '.' + items + '=' + @source + '.' + items,        
                  ''+ @target +'.' + items + '='+ @source +'.' + items) 
          from common.split_string(@fields,',')
      
          return @string  
          end
      

      【讨论】:

      • 是的,看看就好。如果可以,请发布链接 - 谢谢。
      • 刚刚编辑了答案以包含代码。希望这对你有用!我发现它在很多场合都很有用。
      猜你喜欢
      • 1970-01-01
      • 2012-06-16
      • 2011-04-16
      • 2015-11-08
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多