{"id":138815,"date":"2024-10-31T15:55:17","date_gmt":"2024-10-31T07:55:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/138815\/"},"modified":"2024-10-31T15:55:18","modified_gmt":"2024-10-31T07:55:18","slug":"sql-server%e6%b5%b7%e9%87%8f%e6%95%b8%e6%93%9a%e5%b0%8e%e5%85%a5%e7%9a%84%e6%9c%80%e7%b0%a1%e6%bd%94%e7%9a%84%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/138815\/","title":{"rendered":"SQL Server\u6d77\u91cf\u6578\u64da\u5c0e\u5165\u7684\u6700\u7c21\u6f54\u7684\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"sql-server%e6%b5%b7%e9%87%8f%e6%95%b8%e6%93%9a%e5%b0%8e%e5%85%a5%e7%9a%84%e6%9c%80%e7%b0%a1%e6%bd%94%e7%9a%84%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95-gDvxQkICcE\">SQL Server\u6d77\u91cf\u6578\u64da\u5c0e\u5165\u7684\u6700\u7c21\u6f54\u7684\u64cd\u4f5c\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u4f01\u696d\u9762\u81e8\u8457\u8655\u7406\u548c\u5206\u6790\u6d77\u91cf\u6578\u64da\u7684\u6311\u6230\u3002SQL Server\u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5c0e\u5165\u5927\u91cf\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u4e9b\u6700\u7c21\u6f54\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e6b\u52a9\u7528\u6236\u9ad8\u6548\u5730\u5c07\u6d77\u91cf\u6578\u64da\u5c0e\u5165SQL Server\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8sql-server-management-studio-ssms-gDvxQkICcE\">1. \u4f7f\u7528SQL Server Management Studio (SSMS)<\/h2>\n<p>SQL Server Management Studio (SSMS) \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u7528\u6236\u53cb\u597d\u7684\u754c\u9762\u4f86\u7ba1\u7406SQL Server\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528SSMS\u5c0e\u5165\u6578\u64da\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u6253\u958bSSMS\u4e26\u9023\u63a5\u5230\u76ee\u6a19\u6578\u64da\u5eab\u3002<\/li>\n<li>\u53f3\u9375\u55ae\u64ca\u6578\u64da\u5eab\uff0c\u9078\u64c7\u201c\u4efb\u52d9\u201d &gt; \u201c\u5c0e\u5165\u6578\u64da\u201d\u3002<\/li>\n<li>\u5728\u201cSQL Server\u5c0e\u5165\u548c\u5c0e\u51fa\u5411\u5c0e\u201d\u4e2d\uff0c\u9078\u64c7\u6578\u64da\u6e90\uff08\u4f8b\u5982Excel\u3001CSV\u7b49\uff09\u3002<\/li>\n<li>\u914d\u7f6e\u6578\u64da\u6e90\u7684\u9023\u63a5\u4fe1\u606f\uff0c\u7136\u5f8c\u9ede\u64ca\u201c\u4e0b\u4e00\u6b65\u201d\u3002<\/li>\n<li>\u9078\u64c7\u76ee\u6a19\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u9078\u64c7\u8981\u5c0e\u5165\u7684\u8868\u6216\u8996\u5716\u3002<\/li>\n<li>\u6839\u64da\u9700\u8981\u9032\u884c\u6620\u5c04\u548c\u8f49\u63db\uff0c\u7136\u5f8c\u5b8c\u6210\u5c0e\u5165\u904e\u7a0b\u3002<\/li>\n<\/ol>\n<p>\u9019\u7a2e\u65b9\u6cd5\u9069\u5408\u5c0f\u578b\u5230\u4e2d\u578b\u6578\u64da\u96c6\u7684\u5c0e\u5165\uff0c\u56e0\u70ba\u5b83\u63d0\u4f9b\u4e86\u76f4\u89c0\u7684\u754c\u9762\u548c\u6b65\u9a5f\u6307\u5c0e\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8bulk-insert%e8%aa%9e%e5%8f%a5-gDvxQkICcE\">2. \u4f7f\u7528BULK INSERT\u8a9e\u53e5<\/h2>\n<p>\u5c0d\u65bc\u9700\u8981\u5c0e\u5165\u5927\u91cf\u6578\u64da\u7684\u60c5\u6cc1\uff0c\u4f7f\u7528T-SQL\u7684BULK INSERT\u8a9e\u53e5\u662f\u4e00\u500b\u9ad8\u6548\u7684\u9078\u64c7\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BULK INSERT\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>BULK INSERT [\u76ee\u6a19\u8868\u540d]\nFROM 'C:pathtoyourdatafile.csv'\nWITH\n(\n    FIELDTERMINATOR = ',',  \n    ROWTERMINATOR = 'n',   \n    FIRSTROW = 2           \n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u60a8\u9700\u8981\u5c07\u201c\u76ee\u6a19\u8868\u540d\u201d\u66ff\u63db\u70ba\u60a8\u8981\u5c0e\u5165\u6578\u64da\u7684\u8868\u540d\uff0c\u4e26\u78ba\u4fdd\u6587\u4ef6\u8def\u5f91\u6b63\u78ba\u3002\u9019\u7a2e\u65b9\u6cd5\u7279\u5225\u9069\u5408\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u5feb\u901f\u5c07\u6578\u64da\u5f9e\u6587\u4ef6\u5c0e\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8sql-server-integration-services-ssis-gDvxQkICcE\">3. \u4f7f\u7528SQL Server Integration Services (SSIS)<\/h2>\n<p>SQL Server Integration Services (SSIS) \u662f\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u6574\u5408\u5de5\u5177\uff0c\u9069\u5408\u65bc\u8907\u96dc\u7684\u6578\u64da\u5c0e\u5165\u548c\u8f49\u63db\u4efb\u52d9\u3002\u4f7f\u7528SSIS\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5728SQL Server Data Tools\u4e2d\u5275\u5efa\u4e00\u500b\u65b0\u7684SSIS\u5305\u3002<\/li>\n<li>\u4f7f\u7528\u201c\u6578\u64da\u6d41\u201d\u4efb\u52d9\u4f86\u8a2d\u7f6e\u6578\u64da\u6e90\u548c\u6578\u64da\u76ee\u6a19\u3002<\/li>\n<li>\u914d\u7f6e\u6578\u64da\u8f49\u63db\uff0c\u6839\u64da\u9700\u8981\u9032\u884c\u6578\u64da\u6e05\u6d17\u548c\u8f49\u63db\u3002<\/li>\n<li>\u904b\u884cSSIS\u5305\u4ee5\u5c0e\u5165\u6578\u64da\u3002<\/li>\n<\/ol>\n<p>SSIS\u7279\u5225\u9069\u5408\u9700\u8981\u5b9a\u671f\u5c0e\u5165\u6578\u64da\u7684\u60c5\u6cc1\uff0c\u56e0\u70ba\u5b83\u652f\u6301\u81ea\u52d5\u5316\u548c\u8abf\u5ea6\u529f\u80fd\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8powershell%e8%85%b3%e6%9c%ac-gDvxQkICcE\">4. \u4f7f\u7528PowerShell\u8173\u672c<\/h2>\n<p>PowerShell\u662f\u4e00\u500b\u5f37\u5927\u7684\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u57f7\u884cSQL Server\u7684\u6578\u64da\u5c0e\u5165\u4efb\u52d9\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684PowerShell\u8173\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>$sqlServer = \"YourServerName\"\n$database = \"YourDatabaseName\"\n$table = \"YourTableName\"\n$csvFile = \"C:pathtoyourdatafile.csv\"\n\n$connectionString = \"Server=$sqlServer;Database=$database;Integrated Security=True;\"\n$bulkCopy = New-Object Data.SqlClient.SqlBulkCopy($connectionString)\n$bulkCopy.DestinationTableName = $table\n\n$dt = New-Object System.Data.DataTable\n$dt.Load([System.IO.File]::OpenRead($csvFile))\n\n$bulkCopy.WriteToServer($dt)\n<\/code><\/pre>\n<p>\u9019\u7a2e\u65b9\u6cd5\u9069\u5408\u9700\u8981\u81ea\u52d5\u5316\u6578\u64da\u5c0e\u5165\u904e\u7a0b\u7684\u7528\u6236\uff0c\u4e26\u4e14\u53ef\u4ee5\u8f15\u9b06\u96c6\u6210\u5230\u5176\u4ed6\u81ea\u52d5\u5316\u5de5\u4f5c\u6d41\u4e2d\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-gDvxQkICcE\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u8655\u7406\u6d77\u91cf\u6578\u64da\u5c0e\u5165\u6642\uff0c\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u4f7f\u7528SSMS\u7684\u76f4\u89c0\u754c\u9762\u3001BULK INSERT\u7684\u9ad8\u6548\u6027\u3001SSIS\u7684\u5f37\u5927\u529f\u80fd\uff0c\u9084\u662fPowerShell\u7684\u81ea\u52d5\u5316\u80fd\u529b\uff0c\u9019\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e6b\u52a9\u7528\u6236\u9ad8\u6548\u5730\u5c07\u6578\u64da\u5c0e\u5165SQL Server\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u7684\u4f01\u696d\uff0c\u9078\u64c7\u5408\u9069\u7684<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u660e\u667a\u4e4b\u8209\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL Server\u6d77\u91cf\u6578\u64da\u5c0e\u5165\u7684\u7c21\u6f54\u64cd\u4f5c\u65b9\u6cd5\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u5de5\u4f5c\u6d41\u7a0b\uff0c\u52a9\u529b\u696d\u52d9\u767c\u5c55\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-138815","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=138815"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138815\/revisions"}],"predecessor-version":[{"id":138816,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138815\/revisions\/138816"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=138815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=138815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=138815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}