{"id":236,"date":"2012-12-28T15:24:15","date_gmt":"2012-12-28T14:24:15","guid":{"rendered":"http:\/\/csopro.de\/biblog\/2012\/12\/ssis-tabellen-variablen-als-datenquelle\/"},"modified":"2012-12-28T15:24:15","modified_gmt":"2012-12-28T14:24:15","slug":"ssis-tabellen-variablen-als-datenquelle","status":"publish","type":"post","link":"https:\/\/www.csopro.de\/biblog\/2012\/12\/ssis-tabellen-variablen-als-datenquelle\/","title":{"rendered":"SSIS: Tabellen-Variablen als Datenquelle"},"content":{"rendered":"<p>In SSIS kann man als Datenquelle ein SQL-Statement eintragen:<\/p>\n<p><a href=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"SQL als Datenquelle\" border=\"0\" alt=\"Eingabefeld f\u00fcr SQL als Datenquelle\" src=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image-thumb.png\" width=\"244\" height=\"212\" \/><\/a><\/p>\n<p>Manchmal m\u00f6chte man in dieser Maske kompliziertere Berechnungen verwenden. Daf\u00fcr kann man T-SQL (wie bei der Programmierung von Stored Procedures) verwenden. Allerdings funktioniert es nicht ganz wie gedacht:<\/p>\n<p>Als erstes kann man keine tempor\u00e4ren Tabellen (beginnend mit #) verwenden. Als Beispiel betrachten wir das SQL-Statement:<\/p>\n<blockquote>\n<p>select 1 as zahl ,2 as doppelte into #t     <br \/>select * from #t<\/p>\n<\/blockquote>\n<p>Dieses einfache Statement liefert im SQL Server Management Studio \u2013 wie erwartet:<\/p>\n<blockquote>\n<p>(1 Zeile(n) betroffen)     <br \/>zahl&#160;&#160;&#160;&#160;&#160;&#160;&#160; doppelte      <br \/>&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211;      <br \/>1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2<\/p>\n<p>(1 Zeile(n) betroffen)<\/p>\n<\/blockquote>\n<p>F\u00fcgt man dieses Statement aber in der SSIS-Datenquelle hinzu, erh\u00e4lt man nach OK folgenden Fehler:<\/p>\n<p><a href=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image1.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Fehler beim Vorbereiten\" border=\"0\" alt=\"Fehler beim Vorbereiten: #t unbekannt\" src=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image-thumb1.png\" width=\"244\" height=\"92\" \/><\/a><\/p>\n<p>bzw. als Text:<\/p>\n<blockquote>\n<p>Fehler bei Datenflusstask [T-SQL Programm [1]]: SSIS-Fehlercode &#8218;DTS_E_OLEDBERROR&#8216;. OLE DB-Fehler. Fehlercode: 0x80040E14.     <br \/>Ein OLE DB-Datensatz ist verf\u00fcgbar. Quelle: &#8218;Microsoft SQL Server Native Client 10.0&#8216; HRESULT: 0x80040E14 Beschreibung: &#8218;Anweisung(en) konnte(n) nicht vorbereitet werden.&#8216;.      <br \/>Ein OLE DB-Datensatz ist verf\u00fcgbar. Quelle: &#8218;Microsoft SQL Server Native Client 10.0&#8216; HRESULT: 0x80040E14 Beschreibung: &#8218;Ung\u00fcltiger Objektname &#8218;#t&#8216;.&#8216;.<\/p>\n<\/blockquote>\n<p>Auch eine versp\u00e4tete \u00dcberpr\u00fcfung des SQLs bringt keine Besserung.<\/p>\n<p>Allerdings funktioniert es, Tabellen-Variablen zu verwenden. Dort hei\u00dft obiger Code entsprechend:<\/p>\n<blockquote>\n<p>Declare @t as TABLE (zahl integer, doppelte integer)     <br \/>insert into @t select 1 ,2       <br \/>select * from @t<\/p>\n<\/blockquote>\n<p>Dieser Code l\u00e4sst sich als Datenquelle eintragen und SSIS erkennt korrekt die beiden Spalten:<\/p>\n<p><a href=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Spalten korrekt erkannt\" border=\"0\" alt=\"Spalten korrekt erkannt\" src=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image-thumb2.png\" width=\"244\" height=\"211\" \/><\/a><\/p>\n<p>Startet man das SSIS-Paket (damit man etwas sieht, habe ich ein UNION ALL dahinter geh\u00e4ngt), scheint es zu funktionieren:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Datenquelle zu UNION ALL\" border=\"0\" alt=\"Datenquelle zu UNION ALL\" src=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image3.png\" width=\"264\" height=\"176\" \/><\/p>\n<p>Alles ist gr\u00fcn \u2013 wunderbar.<\/p>\n<p>Aber nein! Es wurde keine Zeile \u00fcbertragen.<\/p>\n<p>Das ist ein bekanntes Problem, dass in dem INSERT-Statement bereits ein Dataset erzeugt wird \u2013 dem entspricht in obiger Ausf\u00fchrung der erste Text \u201c(1 Zeile(n) betroffen)\u201d. Die Daten des zweiten Befehls werden dann gar nicht mehr im SSIS weiterverwendet!<\/p>\n<p>Also muss man das SQL so abwandeln:<\/p>\n<blockquote>\n<p>set nocount on     <br \/>Declare @t as TABLE (zahl integer, doppelte integer)      <br \/>insert into @t select 1 ,2       <br \/>select * from @t<\/p>\n<\/blockquote>\n<p>Durch das \u201cset nocount on\u201d wird das erste Dataset unterdr\u00fcckt, so dass es jetzt funktioniert:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.csopro.de\/biblog\/wp-content\/uploads\/2012\/12\/image4.png\" width=\"501\" height=\"260\" \/><\/p>\n<p>Wir sehen, es wurde tats\u00e4chlich eine Zeile \u00fcbertragen \u2013 und die Daten stimmen auch, wie der Data Viewer zeigt.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SSIS kann man als Datenquelle ein SQL-Statement eintragen: Manchmal m\u00f6chte man in dieser Maske kompliziertere Berechnungen verwenden. Daf\u00fcr kann man T-SQL (wie bei der Programmierung von Stored Procedures) verwenden. Allerdings funktioniert es nicht ganz wie gedacht: Als erstes kann man keine tempor\u00e4ren Tabellen (beginnend mit #) verwenden. Als Beispiel betrachten wir das SQL-Statement: select &hellip; <a href=\"https:\/\/www.csopro.de\/biblog\/2012\/12\/ssis-tabellen-variablen-als-datenquelle\/\" class=\"more-link\"><span class=\"screen-reader-text\">SSIS: Tabellen-Variablen als Datenquelle<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-integrationservices"],"_links":{"self":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":0,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csopro.de\/biblog\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}