如何在 SQL Server 中批量更新插入?

发布于 2024-09-03 13:30:58 字数 100 浏览 4 评论 0原文

我正在使用 MERGE 语句在 sql server 2008 数据库中更新插入行。然而,我的存储过程是单行操作,而实际上我更喜欢对这些操作进行批处理。这是否可能?如果可以,我该怎么做?

I'm using the MERGE statement to upsert rows in an sql server 2008 database. However, my sproc is a single-row operation, whereas in fact I'd prefer to batch these. Is this even possible and, if so, how do I do it?

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

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

发布评论

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

评论(2

滥情哥ㄟ 2024-09-10 13:30:59

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

然后在过程中您可以对 TVP 使用 MERGE

Can you use Table-Valued Parameters in your proc? Take a look here http://www.sommarskog.se/arrays-in-sql-2008.html#TVP_in_TSQL to get some ideas

Then in the proc you can use MERGE against the TVP

终止放荡 2024-09-10 13:30:59

我创建了一个名为“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

i have created a proc called 'upsert' which takes a source table name, target table name, fields to join on, and fields to update (fields are separated by commas) and then does the merge dynamically.

code is below.

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文