Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQL Server中执行批处理升级?_Sql Server_Sql Server 2008_Batch File_Merge_Upsert - Fatal编程技术网

Sql server 如何在SQL Server中执行批处理升级?

Sql server 如何在SQL Server中执行批处理升级?,sql-server,sql-server-2008,batch-file,merge,upsert,Sql Server,Sql Server 2008,Batch File,Merge,Upsert,我正在使用MERGE语句向上插入SQLServer2008数据库中的行。然而,我的存储过程是一个单行操作,而实际上我更喜欢批处理这些操作。这可能吗?如果可能的话,我该怎么做?您能在程序中使用表值参数吗?看看这里,了解一些想法 然后在这个过程中,您可以对TVP使用MERGE,我创建了一个名为“upsert”的过程,它接受源表名、目标表名、要连接的字段和要更新的字段(字段用逗号分隔),然后动态地进行合并 代码如下 CREATE proc [common].[upsert](@source n

我正在使用
MERGE
语句向上插入SQLServer2008数据库中的行。然而,我的存储过程是一个单行操作,而实际上我更喜欢批处理这些操作。这可能吗?如果可能的话,我该怎么做?

您能在程序中使用表值参数吗?看看这里,了解一些想法


然后在这个过程中,您可以对TVP使用MERGE,我创建了一个名为“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

是的,能看一看就好了。如果可以,请发布一个链接-谢谢。只需编辑答案以包含代码。希望它对你有用!我发现它在很多场合都很有用。