{"id":52,"date":"2010-07-02T08:47:10","date_gmt":"2010-07-02T06:47:10","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=52"},"modified":"2016-02-17T15:06:26","modified_gmt":"2016-02-17T14:06:26","slug":"massenimport-von-daten-uber-ms-sql-server-management-studio","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=52","title":{"rendered":"Massenimport von Daten \u00fcber MS SQL Server Management Studio"},"content":{"rendered":"<h2>Problem<\/h2>\n<p>Eine gro\u00dfe CSV Datei soll in eine Tabelle importiert werden. Hierbei sollen nicht alle Felder importiert werden.<\/p>\n<h2>Ansatz<\/h2>\n<p>Die Verwendung des Kommandos BULK INSERT wurde von Microsoft f\u00fcr sehr gro\u00dfe Dateien vorgesehen, um den SQL Server ohne die Verwendung von langsamen Cursor-Iterationen zu f\u00fcllen<\/p>\n<h2>L\u00f6sung<\/h2>\n<h3>Vorgehensweise<\/h3>\n<p>Erstellen einer Tabelle mit CREATE TABLE in die importiert wird<br \/>\nZun\u00e4chst erstellt man eine Tabelle in die importiert werden soll, hier wird eine fiktive Tabelle RAWBEIL erstellt.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE &#x5B;dbo].&#x5B;rawbeil](\r\n\t&#x5B;gruppe] &#x5B;varchar](255) NULL,\r\n\t&#x5B;bezeichnung] &#x5B;varchar](255) NULL,\r\n\t&#x5B;feldname] &#x5B;varchar](255) NULL,\r\n\t&#x5B;sapcolumn] &#x5B;varchar](50) NULL,\r\n\t&#x5B;saptable] &#x5B;varchar](50) NULL\r\n)\r\n<\/pre>\n<h3>Erstellen einer IMPORT-Datei f\u00fcr die Feldzuweisung<\/h3>\n<p>Der RECORD-Tag muss den kompletten Aufbau der CSV-Datei enthalten; der ROW-Teil darf enth\u00e4lt die Tabellenspalten, in die importiert wird. FIELD ID wird auf COLUMN SOURCE gemappt. ROW\/COLUMN SOURCE kann auch L\u00fccken enthalten, wenn man Felder aus der CSV Datei weglassen m\u00f6chte..<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&amp;lt;?xml version=&quot;1.0&quot; ?&gt;\r\n&amp;lt;BCPFORMAT xmlns=&quot;http:\/\/schemas.microsoft.com\/sqlserver\/2004\/bulkload\/format&quot; \r\n                   xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;&gt;\r\n\t&amp;lt;RECORD&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;1&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot;\/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;2&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;3&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;4&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;5&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;6&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;7&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;8&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;9&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;10&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;11&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;12&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;13&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;;&quot; MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t\t&amp;lt;FIELD ID=&quot;14&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&quot;\\r\\n&quot; \r\n                  MAX_LENGTH=&quot;255&quot; \/&gt;\r\n\t&amp;lt;\/RECORD&gt;\r\n\t&amp;lt;ROW&gt;\r\n\t\t&amp;lt;COLUMN SOURCE=&quot;1&quot; NAME=&quot;gruppe&quot; xsi:type=&quot;SQLVARYCHAR&quot; \/&gt;\r\n\t\t&amp;lt;COLUMN SOURCE=&quot;2&quot; NAME=&quot;bezeichnung&quot; xsi:type=&quot;SQLVARYCHAR&quot; \/&gt;\r\n\t\t&amp;lt;COLUMN SOURCE=&quot;3&quot; NAME=&quot;feldname&quot; xsi:type=&quot;SQLVARYCHAR&quot; \/&gt;\r\n\t\t&amp;lt;COLUMN SOURCE=&quot;4&quot; NAME=&quot;sapcolumn&quot; xsi:type=&quot;SQLVARYCHAR&quot; \/&gt;\r\n\t\t&amp;lt;COLUMN SOURCE=&quot;5&quot; NAME=&quot;saptable&quot; xsi:type=&quot;SQLVARYCHAR&quot; \/&gt;\r\n\t&amp;lt;\/ROW&gt;\r\n&amp;lt;\/BCPFORMAT&gt;\r\n<\/pre>\n<h3>SQL Anweisung<\/h3>\n<p>CODEPAGE wird verwendet um das Umlaute-Problem zu beheben, FIRSTROW ist die erste Spalte ab der aus CSV importiert wird (meistens will man den Header ausschlie\u00dfen).<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDECLARE @sql nvarchar(4000)\r\nSET @sql =\r\n&#039;BULK INSERT rawbeil FROM &#039;&#039;D:\\web\\karpbjde\\import\\db.csv&#039;&#039;\r\nWITH (\r\nFORMATFILE = &#039;&#039;D:\\web\\karpbjde\\import\\beilmade.fmt&#039;&#039;,\r\nCODEPAGE=&#039;&#039;raw&#039;&#039;,\r\nFIRSTROW = 2\r\n)&#039;\r\nEXEC(@sql)\r\n<\/pre>\n<h2>BULK INSERT mit Typecast in einen bestimmten Datentyp<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\/* Bulk-Import von einer .CSV-Datei in eine Tabelle incl. der Datentypen\r\n* es werden Zahlen im Vormat\r\n* xxx,yy\r\n* xxx.yy\r\n* -xxx,yy\r\n* -xxx.yy\r\n* xxx,yy-\r\n* xxx.yy\r\n* unterst\u00fctzt. Diese werden als Money-Datentyp convertiert\r\n* Bulk-Import\r\n*\r\n* gewidmet an Bj\u00f6rn K.von Nico Gerbig\r\n*\/ \r\n\r\n-- Zuerst als Varchar lesen, dann richtig\r\ncreate table ordersbulk (col1 varchar(50), col2 varchar(50), col3 varchar(50));\r\ncreate table ordersbulk2 (col1 varchar(50), col2 money, col3 money) \r\n\r\n-- Eintragen in Tabelle mit Varchars\r\nBULK INSERT OrdersBulk\r\n    FROM &#039;c:\\test.csv.txt&#039;\r\n    WITH\r\n    (\r\n        --FIRSTROW = 2,\r\n        FIELDTERMINATOR = &#039;;&#039;,\r\n        ROWTERMINATOR = &#039;\\n&#039;\r\n    ) \r\n\r\n-- und Kopieren in die Tabelle mit den richtigen Datentypen\r\ninsert into ordersbulk2 (col1, col2, col3)\r\n    select col1,\r\n          dbo.ConvertVarcharToMoney(col2),\r\n          dbo.ConvertVarcharToMoney(col3)\r\n    from ordersbulk\r\ngo\r\n-- testen\r\nselect * from ordersbulk2\r\ngo \r\n\r\n-- daf\u00fcr notwendige funktion (c)  -=e!ninio=-\r\ncreate function ConvertVarcharToMoney(@abc varchar(255))\r\n    returns money\r\nas begin\r\n    declare @invert money;\r\n    declare @index int;\r\n    declare @newabc money;\r\n    set @invert = 1;\r\n    set @abc = Replace(@abc,&#039;,&#039;,&#039;.&#039;)\r\n    set @abc = Replace(@abc,&#039; &#039;,&#039;&#039;)\r\n    set @index = CHARINDEX(&#039;-&#039;,@abc)\r\n    if @index &amp;gt; 0\r\n    begin\r\n        set @invert = -1;\r\n        set @abc = Replace(@abc, &#039;-&#039;, &#039;&#039;)\r\n    end\r\n    set @newabc = CAST(@abc as money) * @invert\r\n    return @newabc\r\nend\r\ngo \r\n\r\n-- Test f\u00fcr die Funktion selbst\r\nselect dbo.ConvertVarcharToMoney(&#039;13,41&#039;)\r\nselect dbo.ConvertVarcharToMoney(&#039;13.41&#039;)\r\nselect dbo.ConvertVarcharToMoney(&#039;-13,41&#039;)\r\nselect dbo.ConvertVarcharToMoney(&#039;13,41-&#039;)\r\nselect dbo.ConvertVarcharToMoney(&#039;- 13,41&#039;)\r\nselect dbo.ConvertVarcharToMoney(&#039;13,41 -&#039;)\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D52&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem Eine gro\u00dfe CSV Datei soll in eine Tabelle importiert werden. Hierbei sollen nicht alle Felder importiert werden. Ansatz Die Verwendung des Kommandos BULK INSERT wurde von Microsoft f\u00fcr sehr gro\u00dfe Dateien vorgesehen, um den SQL Server ohne die Verwendung von langsamen Cursor-Iterationen zu f\u00fcllen L\u00f6sung Vorgehensweise Erstellen einer Tabelle mit CREATE TABLE in die &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=52\" class=\"more-link\"><span class=\"screen-reader-text\">Massenimport von Daten \u00fcber MS SQL Server Management Studio<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-ms-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-Q","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":15,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":54,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions\/54"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}