{"id":24979582,"url":"https://github.com/stevekirks/sql-bulk-copy-merge","last_synced_at":"2025-07-26T09:37:45.655Z","repository":{"id":87848057,"uuid":"354193570","full_name":"stevekirks/sql-bulk-copy-merge","owner":"stevekirks","description":"This .NET library aims to simplify two common workflows that copy table data between SQL Server databases","archived":false,"fork":false,"pushed_at":"2021-04-09T10:19:33.000Z","size":76,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-13T12:16:11.423Z","etag":null,"topics":["dotnet","sql-server","sqlbulkcopy"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevekirks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-04-03T03:54:01.000Z","updated_at":"2023-04-12T14:53:51.000Z","dependencies_parsed_at":"2023-07-17T04:15:14.415Z","dependency_job_id":null,"html_url":"https://github.com/stevekirks/sql-bulk-copy-merge","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/stevekirks/sql-bulk-copy-merge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevekirks%2Fsql-bulk-copy-merge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevekirks%2Fsql-bulk-copy-merge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevekirks%2Fsql-bulk-copy-merge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevekirks%2Fsql-bulk-copy-merge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevekirks","download_url":"https://codeload.github.com/stevekirks/sql-bulk-copy-merge/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevekirks%2Fsql-bulk-copy-merge/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265386013,"owners_count":23756735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","sql-server","sqlbulkcopy"],"created_at":"2025-02-04T01:19:42.796Z","updated_at":"2025-07-26T09:37:45.645Z","avatar_url":"https://github.com/stevekirks.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# SQL Bulk Copy \u0026 Merge [![CI](https://github.com/stevekirks/sql-bulk-copy-merge/actions/workflows/ci.yml/badge.svg)](https://github.com/stevekirks/sql-bulk-copy-merge/actions/workflows/ci.yml) [![NuGet Version](http://img.shields.io/nuget/v/SqlBulkCopyMerge.svg?style=flat)](https://www.nuget.org/packages/SqlBulkCopyMerge/)\n\nThis .NET library aims to simplify two common workflows that copy table data between SQL Server databases.\n\n## Workflows\n\n### Copy \u0026 Merge\nUses SQLBulkCopy to copy data from a table or view in the source database to a temporary table in the target database before running SQL MERGE from the temporary table to the destination table.\n\nThis solution can be used instead of truncating the table each time before copying, which is not always possible or efficient.\nSome other solutions that do this require defining the table schemas or are dependant on a SQL stored proc.\n\nThe specific steps it performs:\n-   Checks target schema and table exists. If not, creates them\n-   Creates temp table (target table name appended with '_temp')\n-   Copies from table in source db to temp table in target db using SQLBulkCopy\n-   Runs a SQL MERGE with the temp table as source and targetTable as target\n-   Drops the temp table\n\n#### Usage:\n```\nvar copyService = new SqlBulkCopyMergeService(sourceDbConnectionString, targetDbConnectionString);\nvar result = await copyService.CopyAndMerge(sourceTableOrView, targetTable);\nConsole.WriteLine(\"Rows Inserted: \" + result.Inserted);\nConsole.WriteLine(\"Rows Updated: \" + result.Updated);\nConsole.WriteLine(\"Rows Deleted: \" + result.Deleted);\n```\n\nIf the column names between tables are different you can specify them, for example:\n```\nvar columnMappings = new List\u003cColumnMapping\u003e\n{\n    new ColumnMapping(\"id\", \"code\"),\n    new ColumnMapping(\"notes\", \"description\")\n};\nvar result = await copyService.CopyAndMerge(sourceTableOrView, targetTable, columnMappings);\n```\n\n### Copy Latest\nFor source tables that are only ever added to it is more efficient to copy only the new rows into the target table.\nThis method copies the latest data determined by a key column.\n\nThe specific steps it performs:\n-   Queries target table for the latest key column value.\n    If no key column is specified, the primary key is used. If more than one primary key, the first is used.\n-   Queries source where the key column value is greater than the latest in the target.\n    Eg. if the key column is [id], and its latest value is 2, then only rows with an [id] value greater than 2 are copied.\n-   Copies from table in source db directly into table in target db using SQLBulkCopy.\n\n#### Usage:\n```\nvar copyService = new SqlBulkCopyMergeService(sourceDbConnectionString, targetDbConnectionString);\nvar result = await copyService.CopyLatest(sourceTableOrView, targetTable, keyColumnName);\nConsole.WriteLine(\"Rows Copied: \" + result.RowsCopied);\n```\n\n## Notes\nSource database connection requires READER permission.\nTarget database connection requires READER + WRITER + CREATE TABLE + EXECUTE permissions.\n\nTested on SQL Server 2019.\n\nSpatial types (Geometry, Geography) are unsupported because SQLBulkCopy does not support them. The library ignores unsupported columns.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevekirks%2Fsql-bulk-copy-merge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevekirks%2Fsql-bulk-copy-merge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevekirks%2Fsql-bulk-copy-merge/lists"}