tag:blogger.com,1999:blog-79012514017130780282024-02-19T18:02:26.535+09:00SQLServerminminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-7901251401713078028.post-67241738503381553612013-12-05T14:18:00.001+09:002013-12-05T14:18:13.960+09:00◆エージェントジョブを纏めてバックアップ<p>PowerShellでやろうかと思っていたらGUIからも簡単にできるのを発見したのでメモしておく。</p> <p><a href="http://sqlazure.jp/r/sql-server/249/">蒼の王座・裏口 » SQL Server Agentのジョブをすべて一括でスクリプト化する方法</a></p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com2tag:blogger.com,1999:blog-7901251401713078028.post-48505302166727862013-05-02T16:35:00.001+09:002013-05-02T16:56:12.155+09:00◆グラフ要素のカスタマイズ2<ul> <li><a href="#id001">グラフの背景色を設定して見栄えを良くする</a> <li><a href="#id002">グラフの罫線(枠)を設定して見栄えを良くする</a> <li><a href="#id003">グラフ領域の色を設定して見栄えを良くする</a> <li><a href="#id004">グラフに影を設定して見栄えを良くする</a> <li><a href="#id005">グラフにグラデーションを設定して見栄えを良くする</a> <li><a href="#id006">凡例の背景色/罫線/位置を設定して見栄えを良くする</a> <li><a href="#id007">データ ラベルを表示してデータ値を分かりやすくする</a> <li><a href="#id008">縦軸ラベルの書式を変更して見栄えを良くする</a> <li><a href="#id009">縦軸ラベルの間隔を変更して見栄えを良くする</a> <li><a href="#id010">縦軸へインターレース色を設定して見栄えを良くする</a> <li><a href="#id011">補助グリッド線を表示して見栄えを良くする</a> </li></ul> <h5 id="id001">グラフの背景色を設定して見栄えを良くする</h5> <h6>グラフの背景色を設定する</h6> <p>グラフは、背景色(グラデーションやパターン)を設定して見栄えを良くすることができます。<br><a href="http://lh5.ggpht.com/-535nFKKPVm8/UYIWxoxMulI/AAAAAAAAPwQ/gyzgRuphbFA/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-L2xUDRv4LKY/UYIWx07J5EI/AAAAAAAAPwY/SZY9UyWkBLg/image_thumb%25255B1%25255D.png?imgmax=800" width="321" height="133"></a></p> <p>背景色を設定するには、グラフの背景を右クリックし、「グラフのプロパティ」をクリックします。<br><a href="http://lh4.ggpht.com/-AVi4AWjdbxA/UYIWyWfYv-I/AAAAAAAAPwg/LqDtiKtAfYQ/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-YSKradmg5bg/UYIWy_smMbI/AAAAAAAAPwo/ZjiXsIXYI9w/image_thumb%25255B4%25255D.png?imgmax=800" width="321" height="521"></a></p> <p>[塗りつぶし]ページを開くと、「純色」(一色)、「グラデーション」、「パターン」から背景色を設定することができます。設定方法は、グラフのタイトルの「塗りつぶし」と同様です。</p> <h5 id="id002">グラフの罫線(枠)を設定して見栄えを良くする</h5> <h6>グラフの罫線を設定する</h6> <p>グラフは、罫線(枠)を設定して、見栄えを良くすることができます。</p> <p><a href="http://lh3.ggpht.com/-Y0w5SFZXkVs/UYIWzYevKlI/AAAAAAAAPww/qwYiRVMK_hQ/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-X98r5meZ_6g/UYIW0ZaGmjI/AAAAAAAAPw4/DMMahK1XRjE/image_thumb%25255B6%25255D.png?imgmax=800" width="429" height="179"></a></p> <p>罫線を設定するには、背景色の設定と同様、グラフの背景を右クリックして、「グラフのプロパティ」をクリックします。<br><a href="http://lh6.ggpht.com/-fwKNlBQwblY/UYIW1D-L8eI/AAAAAAAAPxA/mDKEY9zDpx4/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-kV4P7BXBp1Q/UYIW1lFe0LI/AAAAAAAAPxI/CUAuCip8u1I/image_thumb%25255B10%25255D.png?imgmax=800" width="335" height="545"></a></p> <p>罫線の種類には、「線」、「フレーム」、「エンボス」、「浮き彫り」、「浮き出し」の 5種類があり、設定すると次のようになります。</p> <h6>罫線の種類</h6> <p>フレーム以外はほぼ同じ感じ。</p> <p><a href="http://lh5.ggpht.com/-lQJCMIUlVRk/UYIW2JA-tHI/AAAAAAAAPxQ/p-6E507kL9A/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-H3ghEinqmeU/UYIW2ifTj3I/AAAAAAAAPxY/FQdT3TeyJQ0/image_thumb%25255B13%25255D.png?imgmax=800" width="474" height="433"></a></p> <h5 id="id003">グラフ領域の色を設定して見栄えを良くする</h5> <h6>グラフ領域の色を設定する</h6> <p>グラフは、グラフ領域の色(塗りつぶし)を設定して、見栄えを良くすることができます。<a href="http://lh5.ggpht.com/-1UKKcqEazFk/UYIW3EU6_CI/AAAAAAAAPxg/R68heAlVTCY/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-C15JZdSABJc/UYIW3kaL1EI/AAAAAAAAPxo/l2JwD_aMRvk/image_thumb%25255B15%25255D.png?imgmax=800" width="469" height="242"></a></p> <p>グラフ領域の色を設定するには、グラフ領域を右クリックして、「グラフ領域のプロパティ」をクリックします。<br><a href="http://lh4.ggpht.com/-cyhxUi5YmAI/UYIW4AM9LoI/AAAAAAAAPxw/F-w6wPigjkg/s1600-h/image%25255B33%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ERukul7qdJQ/UYIW4umUbgI/AAAAAAAAPx4/TThR-IUkGvU/image_thumb%25255B19%25255D.png?imgmax=800" width="471" height="692"></a></p> <p>[塗りつぶし]ページを開いて、「純色」(一色)、「グラデーション」、「パターン」から背景色を設定することができます。<br>(なぜかグラデーション等は有効にならないようだ)</p> <p>3Dを有効にしていると無効になるようだ・・・。</p> <h5 id="id004">グラフに影を設定して見栄えを良くする</h5> <h6>グラフに影を設定する</h6> <p>グラフには、影を設定して、見栄えを良くすることができます。<br>(3Dを有効にしていると無効になるようだ・・・。)<br><a href="http://lh6.ggpht.com/-ES142cHIUtY/UYIW5FrLT8I/AAAAAAAAPyA/qsVEdZ2C1nc/s1600-h/image%25255B37%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-aHSXtYTD-mA/UYIW5Wd2ubI/AAAAAAAAPyI/q6bX6hll6vM/image_thumb%25255B21%25255D.png?imgmax=800" width="461" height="250"></a></p> <p>影を設定するには、グラフを右クリックして、「系列のプロパティ」をクリックします。<br><a href="http://lh6.ggpht.com/-ccvXdONDO_k/UYIW5xhgofI/AAAAAAAAPyQ/BQYM-bMTjAY/s1600-h/image%25255B42%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-pJzy6LgDI1w/UYIW6cC7O_I/AAAAAAAAPyY/OEcDuc_4tNU/image_thumb%25255B24%25255D.png?imgmax=800" width="462" height="643"></a></p> <p>[影]ページを開いて、「影のオフセット」と「影の色」を変更して、影の大きさや色を設定することができます。</p> <h5 id="id005">グラフにグラデーションを設定して見栄えを良くする</h5> <h6>グラフにグラデーションを設定する</h6> <p>グラフにグラデーションを設定する<br><a href="http://lh5.ggpht.com/-hKzexFMHWMY/UYIW660nOyI/AAAAAAAAPyg/agqhIPHvlSc/s1600-h/image%25255B46%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfMBzx9mNtUgweNZY3DrQIPgHBt0o2P6A3M9j-ebq4CDUsqKuqh465P97d-zJghW0Rzle4NZwkHAHAKypBPt8YaE4lIWnKb5KYjFWIuhY61M1-ka7ZwIR9p5T05O3Ia5skcbh3Rkh58RBI/?imgmax=800" width="402" height="246"></a></p> <p>グラデーションを設定するには、影の設定と同様、グラフを右クリックして、「系列のプロパティ」をクリックします。<br><a href="http://lh6.ggpht.com/-icVNwMBA99M/UYIW74iQB7I/AAAAAAAAPyw/KqYJNtUmFus/s1600-h/image%25255B51%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-HR4Gn1B25MA/UYIW8djIoBI/AAAAAAAAPy4/29LuvhMUwwI/image_thumb%25255B29%25255D.png?imgmax=800" width="403" height="595"></a></p> <p>[塗りつぶし]ページを開いて、他の要素の塗りつぶし(背景色)と同様、グラデーションやパターンを設定することができます。</p> <h5 id="id006">凡例の背景色/罫線/位置を設定して見栄えを良くする</h5> <h6>凡例の背景色/罫線/位置を設定する</h6> <p>グラフは、凡例の背景色や罫線、位置を変更して、見栄えを良くすることができます。<br><a href="http://lh6.ggpht.com/-3GIy3QtQQxc/UYIW8r79GHI/AAAAAAAAPzA/WLRKJw1bfs8/s1600-h/image%25255B55%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-4OI59TEx8yc/UYIW9NG4D4I/AAAAAAAAPzI/bOZPA2PV6W0/image_thumb%25255B31%25255D.png?imgmax=800" width="403" height="220"></a></p> <p>凡例を変更するには、凡例を右クリックして、「凡例のプロパティ」をクリックします。<br><a href="http://lh5.ggpht.com/-bHe81blldVA/UYIW9r2cySI/AAAAAAAAPzQ/G57ySJHqOTQ/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-q4m90NIoK-A/UYIW-TQ_ubI/AAAAAAAAPzY/3ggJ-PaRtU4/image_thumb%25255B35%25255D.png?imgmax=800" width="634" height="409"></a></p> <p>ほかのプロパティと同様、[塗りつぶし]ページで背景色、[罫線]ページで罫線(枠)、[影]ページで影を設定することができます。</p> <h6>凡例の位置の変更</h6> <p>凡例の位置は、[全般]ページから変更することができます。<br><a href="http://lh4.ggpht.com/-MVvO_GFsOOY/UYIW-3qeDhI/AAAAAAAAPzg/6o2zPPtDn4s/s1600-h/image%25255B66%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-tyo5uDSib0s/UYIW_gPguTI/AAAAAAAAPzo/SExb2SkI6oE/image_thumb%25255B38%25255D.png?imgmax=800" width="630" height="799"></a></p> <h5 id="id007">データ ラベルを表示してデータ値を分かりやすくする</h5> <h6>データ ラベルを表示する</h6> <p>グラフでは、データ ラベルを表示することで、データ値を分かりやすくすることができます。<br><a href="http://lh3.ggpht.com/-VAWXQhfiLVI/UYIXAJv0kEI/AAAAAAAAPzw/60iRco-dZr8/s1600-h/image%25255B70%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-TuaDDfQKiKk/UYIXAiKPh7I/AAAAAAAAPz4/jg8cnrk6yys/image_thumb%25255B40%25255D.png?imgmax=800" width="498" height="263"></a></p> <p>データ ラベルを表示するには、グラフを右クリックして、「データ ラベルの表示」をクリックします。<br><a href="http://lh5.ggpht.com/-aUm_sqGr1x8/UYIXA3ZfobI/AAAAAAAAP0A/kJ7b6MrQRCs/s1600-h/image%25255B74%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-AZDSSeWh0z8/UYIXBdkZE1I/AAAAAAAAP0I/_zZLn8GdSMM/image_thumb%25255B42%25255D.png?imgmax=800" width="498" height="344"></a></p> <h6>データ ラベルの書式を変更する</h6> <p>データ ラベルの書式を変更するには、データ ラベルを選択して「リボン」のボタンから行います。<br><a href="http://lh6.ggpht.com/-AUkz8Lu27Mc/UYIXCPffmiI/AAAAAAAAP0Q/JmtD5eBgwj8/s1600-h/image%25255B78%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-p6DVPS-V-BY/UYIXCdLiINI/AAAAAAAAP0Y/vaicJ0R0Eis/image_thumb%25255B44%25255D.png?imgmax=800" width="382" height="254"></a></p> <p>通貨形式(¥マーク付きで 3桁ごとにカンマ区切り)へ変更するには、「通貨」を選択するか、「$」ボタンをクリックします。<br>なお、書式は、[プロパティ]ペインの「Fomart」プロパティから変更することもできます(Format プロパティでの書式の記述方法は後述しています)。<br><a href="http://lh6.ggpht.com/-IzQccW_kTa0/UYIXCwkIbUI/AAAAAAAAP0g/aUxFh-qzunQ/s1600-h/image%25255B83%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-TCbsajSv5fo/UYIXDu5pbBI/AAAAAAAAP0o/jyRVgijNFL8/image_thumb%25255B47%25255D.png?imgmax=800" width="628" height="278"></a></p> <p>データ ラベルは、他の要素と同様、フォントの種類やサイズ、色などを変更することもできます。</p> <p><font color="#9b00d3">Note: Smart Label(スマート ラベル)機能</font><br>データ ラベルは、既定では「Smart Label」機能が有効に設定されているので、重なり合うラベルを自動調整してくれています。<br><a href="http://lh4.ggpht.com/-2h6Ol_dZhXk/UYIXEPeR0OI/AAAAAAAAP0w/0xAd7yWTuY4/s1600-h/image%25255B87%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-lN1kgkN0qSA/UYIXEgCQxGI/AAAAAAAAP04/ubrckAKnzIs/image_thumb%25255B49%25255D.png?imgmax=800" width="627" height="240"></a></p> <p>「Smart Label」を無効に設定したい場合は、[グラフ データ]ペインで[Σ 値]の「受注金額」を選択してから、[プロパティ]ペインを開き、「SmartLabels」を展開して、「Disabled」(無効化)プロパティを「True」へ変更します(既定は False)。</p> <h5 id="id008">縦軸ラベルの書式を変更して見栄えを良くする</h5> <h6>縦軸ラベルの書式を変更する</h6> <p>グラフの縦軸のラベルは、他の要素と同様、「書式」や「フォントの種類/サイズ/色」などを変更することができます。<br><a href="http://lh5.ggpht.com/-2EFogfSYOc4/UYIXFKKmkYI/AAAAAAAAP1A/2uCcJa1X89Q/s1600-h/image%25255B92%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-oZwvVccReQY/UYIXFi1_X9I/AAAAAAAAP1I/DdLdQ_qOSqI/image_thumb%25255B52%25255D.png?imgmax=800" width="562" height="309"></a></p> <p>縦軸ラベルの書式を変更するには、縦軸のラベルを右クリックして、「縦軸のプロパティ」をクリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-IBfqKE4qjBtPQzYL1b8oaB5nCVUNOhc3GD6NmEeXISzLPiUiz5ER18vrwJBH3bR4MtV43ePAk0hTU5wCBrJBv53t4kkwycQRvoJOqlGUfOJ9Mphbz1p8zqCz4U-ZBCJlaCGv8XuSTpf3/s1600-h/image%25255B97%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-_o5Hmni9ATc/UYIXG7wxXEI/AAAAAAAAP1Y/JRr64BMCBnk/image_thumb%25255B55%25255D.png?imgmax=800" width="650" height="321"></a></p> <p>[数値]ページで「通貨」を選択すれば、通貨形式へ変更できるようになります。なお、他の要素と同様、リボンや Format プロパティから書式を変更することもできます。<br>このダイアログでは、[ラベルのフォント]ページでフォントの種類/サイズ/色を変更することも可能です。</p> <h5 id="id009">縦軸ラベルの間隔を変更して見栄えを良くする</h5> <h6>縦軸ラベルの間隔を変更する</h6> <p>グラフの縦軸の「ラベルの間隔」は、任意の値へ変更することができます。<br><a href="http://lh5.ggpht.com/-6trsig32-9A/UYIXHbZVEzI/AAAAAAAAP1g/TZv79k8pMsE/s1600-h/image%25255B102%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XNmJUKozXAM/UYIXH13kPlI/AAAAAAAAP1o/QMTbBZLnGNQ/image_thumb%25255B58%25255D.png?imgmax=800" width="633" height="342"></a></p> <p>ラベルの間隔を変更するには、ラベルの書式設定と同様、縦軸のラベルを右クリックして、「縦軸のプロパティ」をクリックします。<br><a href="http://lh6.ggpht.com/-gXKHK-OUD9g/UYIXIY6qJgI/AAAAAAAAP1w/yF_3B82_Sbw/s1600-h/image%25255B106%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-VpLzN1MmYsc/UYIXI1QoC8I/AAAAAAAAP14/ktcVd0v7zu0/image_thumb%25255B60%25255D.png?imgmax=800" width="636" height="328"></a></p> <p>[軸のオプション]ページで[間隔]を「1000000」へ変更すれば、100万円単位でラベルを表示できるようになります。なお、このように間隔を大きい値へ変更した場合は、次のようにデザイン画面でグラフが表示されなくなってしまうので、この設定はグラフ作成の最終手順として行うことをお勧めします。<br><a href="http://lh4.ggpht.com/-yYY-jmwdm6o/UYIXJZ2wD9I/AAAAAAAAP2A/QoUOymWhqmo/s1600-h/image%25255B111%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-VvZeBN2MLGA/UYIXJ8WbOhI/AAAAAAAAP2I/oQNy8QlV9fE/image_thumb%25255B63%25255D.png?imgmax=800" width="496" height="268"></a></p> <h5 id="id010">縦軸へインターレース色を設定して見栄えを良くする</h5> <h6>縦軸へインターレース色を設定する</h6> <p>グラフは、「インターレース色」を設定することで、グラフ領域を縞模様(ストライプ)で表示できるようになるので、見栄えを良くすることができます。<br><a href="http://lh5.ggpht.com/-UsvbdAFiFGA/UYIXKvpgRDI/AAAAAAAAP2Q/P4umwTdwzHY/s1600-h/image%25255B115%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-vVThn5fHu_c/UYIXLKAz9ZI/AAAAAAAAP2Y/_XCj3BJiq0g/image_thumb%25255B65%25255D.png?imgmax=800" width="503" height="264"></a></p> <p>インターレース色を設定するには、ラベルの間隔や書式設定と同様、縦軸のラベルを右クリックして、「縦軸のプロパティ」をクリックします。<br><a href="http://lh6.ggpht.com/-IPko711_Fv8/UYIXLh55xJI/AAAAAAAAP2g/1Z7c6FA6kRE/s1600-h/image%25255B119%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-dUiXJmMGpXU/UYIXMJyUYsI/AAAAAAAAP2o/lyPPUAWMZUg/image_thumb%25255B67%25255D.png?imgmax=800" width="508" height="279"></a></p> <p>[軸のオプション]ページで「インターレース色を使用する」をチェックして、[色]で任意の色を選択(ここでは、「その他の色」から FloralWhite を選択)すれば、縞模様(ストライプ)で表示できるようになります。</p> <h5 id="id011">補助グリッド線を表示して見栄えを良くする</h5> <h6>補助グリッド線を表示する</h6> <p>グラフは、「補助グリッド線」を表示することで、グラフ領域へ補助目盛りのラインを追加できるので、見栄えを良くすることができます。<br><a href="http://lh3.ggpht.com/-lkDpHk80kQs/UYIcEuuaW_I/AAAAAAAAP24/tyNMH9s5bYE/s1600-h/image%25255B124%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-3XiThGfT0nY/UYIcFC2a8RI/AAAAAAAAP3A/x5tbOGOWemk/image_thumb%25255B70%25255D.png?imgmax=800" width="621" height="333"></a></p> <p>補助グリッド線を表示するには、軸ラベルを右クリックして、「補助グリッド線の表示」をクリックします。<br><a href="http://lh6.ggpht.com/-qi2AlN5fVyc/UYIcF7bw3TI/AAAAAAAAP3I/HdbaPpWJiqA/s1600-h/image%25255B129%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-c-9QtGWjI7s/UYIcGQvoKKI/AAAAAAAAP3Q/-QFGvAMZTv4/image_thumb%25255B73%25255D.png?imgmax=800" width="442" height="359"></a></p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-53581956198046057352013-05-02T14:46:00.001+09:002013-05-02T16:56:59.528+09:00◆グラフ要素のカスタマイズ1<ul> <li><a href="#id001">ウィザードを利用しないグラフの作成</a> <li><a href="#id002">3D を有効化して見栄えを良くする</a> <li><a href="#id003">グラフのタイトルの見栄えを良くする</a> <li><a href="#id004">グラデーションやパターンを設定して見栄えを良くする</a> <li><a href="#id005">軸のタイトルを変更して見栄えを良くする</a> </li></ul> <h5 id="id001">ウィザードを利用しないグラフの作成</h5> <p>ここでは、Step3 で作成した、次のような「区分ごと/年ごとの受注金額の合計」のグラフをウィザードを利用せずに作成する手順を説明します。<br><a href="http://lh5.ggpht.com/-aGcSrhgAwnU/UYH9DSkKmzI/AAAAAAAAPpw/2uf0rClgdvM/s1600-h/image%25255B2%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-VYzsuKhGwXE/UYH9DxxxijI/AAAAAAAAPp4/e1j_kAonvr0/image_thumb.png?imgmax=800" width="244" height="151"></a></p> <h6>Let's Try</h6> <ol> <li>まずは、レポート ビルダーを起動します。 <li>[作業の開始]ページが表示されたら、[新しいレポート]の[空のレポート]をクリックします。 <li>レポートのデザイン画面が表示されたら、デザイン上の任意の場所を右クリックして、[挿入]から[グラフ]をクリックします。<br><a href="http://lh6.ggpht.com/-6xnmcGJeKeo/UYH9EEbkIwI/AAAAAAAAPqA/M-fJR8TvxuE/s1600-h/image%25255B6%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-zTskwSHDS-Y/UYH9EztczvI/AAAAAAAAPqI/nLGp_5V8XUM/image_thumb%25255B2%25255D.png?imgmax=800" width="362" height="337"></a> <li>[グラフの種類を選択]ダイアログが表示されたら、[縦棒]を選択して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-L_Jig5txBSo/UYH9FUfqcTI/AAAAAAAAPqQ/yBJe9tzJpaQ/s1600-h/image%25255B9%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-cJYoIXx2zRA/UYH9F_LjeQI/AAAAAAAAPqY/vcFzSj0I_0Q/image_thumb%25255B3%25255D.png?imgmax=800" width="228" height="107"></a> <li>次に、[データセットのプロパティ]ダイアログが表示されるので、Step2 で作成した共有データセット(ds_受注クエリ)を選択して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-NdMPcdSyGTA/UYH9GMKCY6I/AAAAAAAAPqg/AyigZZXpims/s1600-h/image%25255B13%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-KdWBSaa-JAE/UYH9Gr9F2KI/AAAAAAAAPqo/2sg0Isr4xuU/image_thumb%25255B5%25255D.png?imgmax=800" width="358" height="225"></a><br>なお、共有データセットを作成していない場合は、[レポートに埋め込まれたデータセットを使用します]を選択して、別途データセットを作成してください(データセットに指定するクエリは、サンプル スクリプト内の「Step2-2_Query.txt」ファイルに記載)。</li></ol> <h6>フィールドの配置</h6> <p>次に、受注金額や区分名、年フィールドをグラフへ配置します。</p> <ol> <li>フィールドをグラフへ配置するには、次のようにグラフをクリックして、[グラフ データ]ペインを表示します。<br><a href="http://lh5.ggpht.com/-9c3u3KrYyZg/UYH9HM9b1cI/AAAAAAAAPqw/1zypaBqNL6w/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-mV58BpT5JvA/UYH9HlzndCI/AAAAAAAAPq4/22ZJvi-ksHo/image_thumb%25255B7%25255D.png?imgmax=800" width="404" height="231"></a> <li>次に、[グラフ データ]ペインの[Σ 値]の[+]ボタンをクリックして、「受注金額」フィールドを選択します。<br><a href="http://lh4.ggpht.com/-pLqvV4TGsQA/UYH9IDMm8-I/AAAAAAAAPrA/bkMsMWfpTC0/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-NwPCr5m9XTw/UYH9IpzqqwI/AAAAAAAAPrI/nTmuQrUAv1U/image_thumb%25255B9%25255D.png?imgmax=800" width="404" height="176"></a> <li>次に、[カテゴリ グループ]の[詳細]の[▼]ボタンをクリックして、「区分名」フィールドを選択します。<br><a href="http://lh3.ggpht.com/-hRgzw1BLJ40/UYH9JFJBIQI/AAAAAAAAPrQ/JlCKYPQdyVE/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ZlxZMZHOsEE/UYH9JVH-BAI/AAAAAAAAPrY/j4Kw8IMChTg/image_thumb%25255B12%25255D.png?imgmax=800" width="292" height="234"></a><br>これで、区分ごとの受注金額の合計をグラフで表示できるようになります。 <li>次に、[系列グループ]の[+]ボタンをクリックして、「年」フィールドを選択します。<br><a href="http://lh5.ggpht.com/-GU3AQ4CDUsU/UYH9J7hkUrI/AAAAAAAAPrg/6tn0BXW-M28/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ayPZK9wiVHc/UYH9KZVI-sI/AAAAAAAAPro/JNtdpqduJF4/image_thumb%25255B14%25255D.png?imgmax=800" width="290" height="98"></a> <li>グラフの大きさを任意に調整して、[実行]ボタンをクリックすると、次のように結果を確認できます。<br><a href="http://lh3.ggpht.com/-JGlXD_enWag/UYH9KiVzwBI/AAAAAAAAPrw/bQPYNARNvVs/s1600-h/image%25255B35%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-_ttuJPq3VAE/UYH9LC5DbRI/AAAAAAAAPr4/VA5J9YqTPYA/image_thumb%25255B17%25255D.png?imgmax=800" width="372" height="202"></a><br>このように、ウィザードを利用しなくても、簡単にグラフを作成することができます。以降では、グラフの各要素の書式やスタイルを変更する方法を詳しく説明していきます。</li></ol> <h5 id="id002">3D を有効化して見栄えを良くする</h5> <h6>3D の有効化</h6> <p>Step 3 では、[グラフの種類を選択]ダイアログから「3D 棒グラフ」や「3D 面グラフ」を選択することでグラフを 3D 化する方法を説明しましたが、種類を選択しなくてもグラフを 3D 化することができます。また、[グラフの種類を選択]ダイアログでは、3D のグラフが用意されていないものもあります(ドーナッツや範囲グラフなど)。こういったグラフでも、次のようにグラフを右クリックして「3D 効果」をクリックすれば、3D 化することができます。</p> <p><a href="http://lh5.ggpht.com/-Y5F2mwq-mco/UYH9LgSBeWI/AAAAAAAAPsA/AS_2T32vPns/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-eES9xv3AGBw/UYH9MOC0fkI/AAAAAAAAPsI/iJTm-pUjnSU/image_thumb%25255B20%25255D.png?imgmax=800" width="533" height="231"></a></p> <p>[グラフ領域のプロパティ]ダイアログが表示されたら、「3D の有効化」をチェックすることで、次のように 3D 化することができます。</p> <p><a href="http://lh5.ggpht.com/-u6XTMtP0pL0/UYH9MuDppgI/AAAAAAAAPsQ/438kT1zJX-A/s1600-h/image%25255B44%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-0k8EpVvRTz0/UYH9NDof7YI/AAAAAAAAPsY/ejbE4_lzLOA/image_thumb%25255B22%25255D.png?imgmax=800" width="537" height="173"></a></p> <h6>3D 設定の変更</h6> <p>[グラフ領域のプロパティ]ダイアログでは、「系列のクラスタリングを有効にする」や「投影モード」、「回転」、「傾斜」、「壁面の厚さ」などの各種の 3D 設定を変更することができます(次のページに結果例を掲載してあります)。</p> <p><font color="#00ff00">標準</font><br><a href="http://lh5.ggpht.com/-AGkrG3GZnnM/UYH9NtLdfnI/AAAAAAAAPsg/i7d3z_OK8_4/s1600-h/image%25255B49%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-GUqzis5vBVM/UYH9OJgMkzI/AAAAAAAAPso/xu0NJhCV53Q/image_thumb%25255B25%25255D.png?imgmax=800" width="401" height="196"></a></p> <p><グラフ領域のプロパティ設定></p> <p><a href="http://lh6.ggpht.com/-0V8lbCnt6qA/UYH9Oh-igII/AAAAAAAAPsw/cVQ9hLJWQGs/s1600-h/image%25255B52%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-m2h9_BIDfo4/UYH9O_oL6jI/AAAAAAAAPs4/rsFVQDdwL2s/image_thumb%25255B26%25255D.png?imgmax=800" width="244" height="128"></a></p> <p><a href="http://lh6.ggpht.com/-Q4WkNbiwg48/UYH9Pp3SoYI/AAAAAAAAPtA/Adyu3T0_Ca0/s1600-h/image%25255B60%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-TJCs9zE8L0g/UYH9QNUz7mI/AAAAAAAAPtI/coUFRRkgTQE/image_thumb%25255B32%25255D.png?imgmax=800" width="617" height="1071"></a></p> <h5 id="id003">グラフのタイトルの見栄えを良くする</h5> <h6>グラフのタイトルの見栄えを良くする</h6> <p>グラフのタイトルは、「テキスト」や「フォントの種類/サイズ/色」、「塗りつぶし(背景色)」、「罫線(枠線の色や幅)」、「影」、「配置場所」などを変更して、見栄えを良くすることができます(下記のグラフは積み上げ縦棒)。</p> <p><a href="http://lh3.ggpht.com/-h6-lm4tOaZo/UYH9QmaH_qI/AAAAAAAAPtQ/xirCvuCtAX4/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-wRFjqTOjjDc/UYH9Q-KHMZI/AAAAAAAAPtY/I1-QugsrTjs/image_thumb%25255B35%25255D.png?imgmax=800" width="439" height="185"></a></p> <p>タイトルを変更するには、タイトルを右クリックして、「タイトルのプロパティ」をクリックします。</p> <p><a href="http://lh3.ggpht.com/-XCDruIdFpJI/UYH9Rnp3-NI/AAAAAAAAPtg/wgJpOS0GaBo/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Xq69NqYdRkI/UYH9SI6E5XI/AAAAAAAAPto/gIOoUJBd-aQ/image_thumb%25255B37%25255D.png?imgmax=800" width="438" height="362"></a></p> <p><a href="http://lh3.ggpht.com/-wQi0_WF0EzU/UYH9SSXEgYI/AAAAAAAAPtw/C5e9p-yDzQs/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-sUXgSISwDH8/UYH9S86dVrI/AAAAAAAAPt4/c1cxknVGSd0/image_thumb%25255B39%25255D.png?imgmax=800" width="438" height="212"></a></p> <h6>リボンからフォントや色を変更</h6> <p>「フォントの種類/サイズ/色」や「塗りつぶし」、「罫線」は、リボンの書式設定ボタンから行うこともできます。</p> <p><a href="http://lh4.ggpht.com/-H7CvJWX5aVU/UYH9ebHN4gI/AAAAAAAAPuI/Vt-t423FMx0/s1600-h/image77.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-oqokQEO0Ku8/UYH9e851H6I/AAAAAAAAPuQ/4UisTqIjBw4/image_thumb41.png?imgmax=800" width="440" height="195"></a></p> <h5 id="id004">グラデーションやパターンを設定して見栄えを良くする</h5> <h6>グラデーションを設定する</h6> <p>タイトルの「塗りつぶし」(背景色)は、次のようにグラデーションを設定して見栄えを良くすることができます。</p> <p><a href="http://lh6.ggpht.com/-TxZ8aAozfb0/UYH9fdiPVoI/AAAAAAAAPuY/SOv0ZvMowek/s1600-h/image82.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-UB-1Cu8gdvc/UYH9glDEEYI/AAAAAAAAPug/xhf_2ebmLsc/image_thumb44.png?imgmax=800" width="605" height="199"></a></p> <h6>パターンを設定する</h6> <p>タイトルの「塗りつぶし」(背景色)は、パターンを設定して見栄えを良くすることもできます。</p> <p><a href="http://lh3.ggpht.com/-X8MzSSRJPS4/UYH9hFOFo_I/AAAAAAAAPuo/Gfw6nvxjNzQ/s1600-h/image86.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-yZyxlF-3ir8/UYH9hs_rvjI/AAAAAAAAPuw/gYZg2N_6tm0/image_thumb46.png?imgmax=800" width="599" height="197"></a></p> <h5 id="id005">軸のタイトルを変更して見栄えを良くする</h5> <p>軸のタイトル(グラフの横軸と縦軸のタイトル)は、「テキスト」や「フォントの種類/サイズ/色」、「配置場所」などを変更して、見栄えを良くすることができます。</p> <p><a href="http://lh3.ggpht.com/-nLZaNTmAAMU/UYH9iOnxfiI/AAAAAAAAPu4/lpAxwH2IIJs/s1600-h/image90.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-jPUbRLBQQW8/UYH9iiLEahI/AAAAAAAAPvA/-RypFQAgqrQ/image_thumb48.png?imgmax=800" width="413" height="180"></a></p> <p>軸のタイトルを変更するには、軸のタイトルを右クリックして、「軸のタイトルのプロパティ」をクリックします。</p> <p><a href="http://lh6.ggpht.com/-yhJLCQ6ye1A/UYH9jFc4YtI/AAAAAAAAPvI/KuByvQvQTaA/s1600-h/image95.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-sOPV_urcjqk/UYH9mIXKEdI/AAAAAAAAPvQ/lfE2BUQiZR8/image_thumb51.png?imgmax=800" width="504" height="450"></a></p> <p>フォントの種類やサイズ、色の変更は、リボンの書式設定ボタンから行うことも可能です。なお、軸のタイトルでは、グラフのタイトルで設定できた「塗りつぶし(背景色)」や「罫線(枠線の色や幅)」、「影」などは設定することができません。軸のタイトルが不要な場合は、[軸のタイトルの表示]のチェックをオフにすれば軸のタイトルを削除することもできます。 </p> <h6>縦軸のタイトルを縦書きにする</h6> <p>縦軸のタイトルは、[プロパティ]ペインで[TextOrientation]プロパティを「Stacked」へ変更することで、縦書きにすることもできます(既定値は Auto)。</p> <p><a href="http://lh5.ggpht.com/-k1yhbY_CSbY/UYH9mu2xP0I/AAAAAAAAPvY/urwNY0olZKc/s1600-h/image100.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-BICOK_hMSI8/UYH9nOW9prI/AAAAAAAAPvg/hQRBMui2n-8/image_thumb54.png?imgmax=800" width="588" height="199"></a></p> <p><strong><font color="#00ff00">※</font></strong>[プロパティ]ペインが表示されていない場合は、リボンの[表示]タブで[プロパティ]をチェックしてください。</p> <p><a href="http://lh5.ggpht.com/-5yYoa9LStAY/UYH9nQQWtaI/AAAAAAAAPvo/TRIOAjPg9fo/s1600-h/image104.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-5-A746YcQlU/UYH9n49DihI/AAAAAAAAPvw/-PAY411h94A/image_thumb56.png?imgmax=800" width="585" height="171"></a></p> <p><font color="#9b00d3">Note: 厳密な縦書きではない<br></font>Reporting Services では、縦書きフォントがサポートされていないので、「Stacked」へ設定した軸のタイトルは厳密な縦書きではありません。内部的には、横書きの文字を 1文字ずつ縦に並べただけになるので、カッコやセミコロンなどは、横書きのままになり、ひらがなと漢字の位置は微妙にずれたりします(漢字のみを利用するなどの工夫が必要)。<br><a href="http://lh6.ggpht.com/-suigOcQrOlE/UYH9ocrvYLI/AAAAAAAAPv4/YGTQXQgFn6k/s1600-h/image108.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Icy3M6SYMCo/UYH9pffQOxI/AAAAAAAAPwA/pv7SH_6aZo0/image_thumb58.png?imgmax=800" width="250" height="209"></a></p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-22530012398151182932013-04-07T00:25:00.001+09:002013-04-07T00:25:27.376+09:00◆実践編:3つ以上の テーブルの結合<ul> <li><a href="#id001">この STEP で使用するデータベース</a> <li><a href="#id002">3 つ以上のテーブルの結合</a> <li><a href="#id003">売上集計</a> <li><a href="#id004">WITH ROLLUP と WITH CUBE</a> <li><a href="#id005">クロス集計</a> <li><a href="#id006">PIVOT によるクロス集計</a> <li><a href="#id007">ビュー: CREATE VIEW</a> </li></ul> <h5 id="id001">この STEP で使用するデータベース</h5> <p>この STEP では、<a href="http://go.microsoft.com/?linkid=9816465">サンプル スクリプト</a>に含まれる「NorthwindJ 」データベース<br>(NorthwindJ.mdf と NorthwindJ.ldf)を利用します。この STEP を始める前に、このデータベ<br>ースを SQL Server 2012 上へ作成しておく必要があります。作成の手順は、次のとおりです。</p> <ol> <li>Management Studio で、次のようにツールバーの[ファイルを開く]ボタンをクリックしま<br>す。<br><a href="http://lh3.ggpht.com/-ZkH8J2Jx3as/UWA2CPYqg4I/AAAAAAAAPY0/mLvjTkQTx8w/s1600-h/image3.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-vWUrDSPLU3c/UWA2JSRiVrI/AAAAAAAAPY8/41rbncBm7v4/image_thumb1.png?imgmax=800" width="586" height="193"></a><br>これにより、[ファイルを開く]ダイアログが表示されるので、サンプル スクリプトを解凍し<br>たフォルダーを展開して、「NorthwindJ.sql」ファイルを選択し、[開く]ボタンをクリック<br>します。 <li>次のようにデータベースを作成するためのスクリプトが表示されるので、ツールバーの[実行]ボタンをクリックして、スクリプトを実行します。<br><a href="http://lh3.ggpht.com/-8S58tRGgK50/UWA2OSDl82I/AAAAAAAAPZE/C613ty9MDbQ/s1600-h/image7.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Df0tCzmKcEI/UWA2Wb9pCUI/AAAAAAAAPZM/L3jAWZIlXaA/image_thumb3.png?imgmax=800" width="585" height="190"></a> <li>数秒後に実行が完了して、次のように画面下に「クエリが正常に実行されました」と表示され<br>ることを確認します。<br><a href="http://lh4.ggpht.com/-IGeao7Xbw_U/UWA2bZ9uxmI/AAAAAAAAPZU/go0S8ZvPPcA/s1600-h/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Suzo_FMw9cs/UWA2ipPHcPI/AAAAAAAAPZc/ZzRBpRqFOV4/image_thumb5.png?imgmax=800" width="347" height="130"></a><br>以上でデータベースの作成が完了です。<br>なお、この NorthwindJ データベースは、Microsoft Access 2003 に付属のサンプル データベース「Norhwind」を SQL Server 上へアップサイズしたものを利用していますが、この自習書の手順を試すために、一部のデータを加工しています。</li></ol> <h6>NorthwindJ データベースの構成</h6> <p>NorthwindJ データベースは、Microsoft Access 2003 に付属のサンプル データベース<br>「Norhwind」を SQL Server 上へアップサイズし、この自習書の手順を試すために、一部のデ<br>ータを加工したものです。具体的なスキーマ構成は次のとおりです。<br><a href="http://lh4.ggpht.com/-Jp2GYNUU_RM/UWA2n1hoQTI/AAAAAAAAPZk/cJNaJwe2cTA/s1600-h/image17.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-HD1l9YdYAqo/UWA2vmQrsdI/AAAAAAAAPZs/sqz5xGiWw2g/image_thumb9.png?imgmax=800" width="618" height="549"></a></p> <p>このデータベースは、商品の販売管理を題材とし、次のように「受注」テーブルと「受注明細」テ<br>ーブルの中へ受注データが格納されています。</p> <p><a href="http://lh5.ggpht.com/-TCFnRxgQfaQ/UWA20ui-3sI/AAAAAAAAPZ0/DIXm3DaESWw/s1600-h/image22.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-0KCnujrODm0/UWA28GcjcdI/AAAAAAAAPZ8/RUv1f7HctcI/image_thumb12.png?imgmax=800" width="615" height="371"></a></p> <p>「受注」テーブルには、いつ受注したのかや、どの顧客(得意先)からの受注なのかなどの情報が<br>格納され、「受注明細」テーブルには、各注文の明細(どの商品がいくらで、何個受注したのかな<br>ど)が格納されています。<br>また、「受注明細」テーブルの「商品コード」列からは、受注対象となった商品の名前や区分名(商<br>品分類名)などを「商品」テーブルと「商品区分」テーブルから取得できるようにリレーションシップを設定してあります。</p> <p><a href="http://lh4.ggpht.com/-_iIpGU3ohEM/UWA3BOZDUQI/AAAAAAAAPaE/8u5ErQpixHM/s1600-h/image27.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-WZBqGFqxA_Q/UWA3I8-mevI/AAAAAAAAPaM/06nApRVDM4o/image_thumb15.png?imgmax=800" width="610" height="391"></a></p> <p>「商品」テーブルには、商品データ(ビリビリビールやオタル白ラベルなど)が格納され、「商品<br>区分」テーブルには、商品分類(飲料や調味料、菓子類など)が格納されています。</p> <h5 id="id002">3 つ以上のテーブルの結合</h5> <p>3 つ以上のテーブルを結合するには、次のように記述します。</p> <p><a href="http://lh4.ggpht.com/-V4TbuMFS6S0/UWA3Niox7dI/AAAAAAAAPaU/S6jlAG6TqFU/s1600-h/image31.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hi3XvSGXW18/UWA3Ux8xoHI/AAAAAAAAPac/iHRY5RSUdrA/image_thumb17.png?imgmax=800" width="408" height="162"></a></p> <p>INNER JOIN と ON を結合したいテーブルの分だけ記述すれば、複数のテーブルを結合できる<br>ようになります。</p> <h6>Let's Try</h6> <ol> <li>まずは、2 つのテーブルから結合してみましょう。「商品」テーブルと「商品区分」テーブル<br>を結合して、次のようなデータになっていることを確認します。<br><a href="http://lh6.ggpht.com/-6MwV_9rz0dM/UWA3Z8m-UPI/AAAAAAAAPak/CJEJiDGeh3A/s1600-h/image36.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-VX-Go3lmxoo/UWA3hIVMSAI/AAAAAAAAPas/jxKM5hIUG9c/image_thumb20.png?imgmax=800" width="581" height="192"></a><br>「商品区分」テーブルには、区分コード「1」の “飲料” や、区分コード「2」の “調味料” などが格納され、「商品」テーブルには、どの区分に属した商品なのかを識別するために「区分コード」列へ値が格納されています。したがって、この 2 つのテーブルを結合するには、次<br>のように結合キー列へ「区分コード」を指定します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 区分名, 商品.*<br>FROM 商品<br>INNER JOIN 商品区分<br>ON 商品.区分コード = 商品区分.区分コード</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-0S-q1uoErSg/UWA3l2k3wxI/AAAAAAAAPa0/SLE0vJlWVK4/s1600-h/image40.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-hOvhBGQ0MeU/UWA3tLsn_8I/AAAAAAAAPa8/6a1ZWwBGBSo/image_thumb22.png?imgmax=800" width="588" height="213"></a> <li>次に、商品の仕入先が格納されている「仕入先」テーブルとも結合してみましょう。このテー<br>ブルのデータは、次のようになっています。<br><a href="http://lh4.ggpht.com/-JN17YRifSYA/UWA3x0EolxI/AAAAAAAAPbE/f12hOUXKAzE/s1600-h/image44.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUCwFRJrTU_e6GRaicOM-v4PwHCUs1VuYnBKNNgOzhpNl97cBPb-b5-nHEYQQP3Jnc3CsWc8_FDf-0YCCVu-phAaf7io167qLJohMbfeA20BiW2xpow1nLL34mCeD8Hj41ebFXwRQp7Cuh/?imgmax=800" width="595" height="149"></a><br>「仕入先」テーブルとは、「仕入先コード」で結合することができるので、次のように結合キ<br>ー列を指定します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 仕入先名, 区分名, 商品.*<br>FROM 商品<br>INNER JOIN 商品区分<br>ON 商品.区分コード = 商品区分.区分コード<br>INNER JOIN 仕入先<br>ON 商品.仕入先コード = 仕入先.仕入先コード</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-GTo1I_xz-YU/UWA3-ZDRezI/AAAAAAAAPbU/emSXRx2M7-I/s1600-h/image48.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-9S5FKWtXieo/UWA4F8eTPNI/AAAAAAAAPbc/vQnIjE8s6l8/image_thumb26.png?imgmax=800" width="597" height="254"></a><br>「区分名」列の隣へ「仕入先名」列を取得できたことを確認できます。<br>このように3 つ以上のテーブルを結合する場合には、INNER JOIN と ON 句をテーブルの分<br>だけ追加するようにします。</li></ol> <h6>JOIN でテーブルを記述する順序</h6> <p>複数のテーブルを内部結合する場合、JOIN を記述する順番は関係ありません。</p> <p><font color="#9b00d3">Note: JOIN へ記述したテーブル順に処理させる ~FORCE ORDER~<br></font>複数のテーブルを結合する場合の内部的な処理(結合される順番)は、SQL Server が最速だと判断(予測)した順番で行われます。しかし、内部処理を加味するような高度なチューニングでは、その判断を覆したい場合もあります(SQL Server の判断は、あくまでも最速だと予測したものなので、本当に最速だとは限らないのです)。こういった場合、JOIN へ記述したテーブルの順に、SQL Server に処理させることができます。これは SELECT ステートメントの最後に “OPTION (FORCE ORDER)” と指定することで実現できます。Force は「強制」という意味です。<br>以前に筆者が行った案件では、FORCE ORDER を指定し、テーブルの記述順を変更することで、8 秒かかっていた処理を 3 秒に短縮させたことがあります。このときは、インデックスの作成と結合ヒント(MERGE や LOOP など、結合の内部的な処理方法)の変更も行いましたが、ここまで実行時間を短縮できたのは FORCE ORDER のおかげでした。</p> <p><font color="#9b00d3">Note: クエリ デザイナーで GUI 操作で複数テーブルの結合<br></font>前述の Note で説明したように、Management Studio の「クエリ デザイナー」を利用すれば、GUI 操作で複数のテーブルを結合することもできます。「クエリ デザイナー」を起動するには、クエリ エディター上で任意の場所を右クリックして、[エディターでクエリをデザイン]をクリックします。</p> <p><a href="http://lh3.ggpht.com/-Ge3sx-EzoEk/UWA4LugcnFI/AAAAAAAAPbk/xSZjtiXWvBY/s1600-h/image54.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-pb34vhTZvPk/UWA4T1ClmdI/AAAAAAAAPbs/m9nF-jLWT5U/image_thumb30.png?imgmax=800" width="636" height="454"></a></p> <p>なお、今回の NorthwindJ データベースのように FOREIGN KEY 制約が設定されている場合には、上記の操作で問題ありませんが、もし FOREIGN KEY 制約を設定していない場合は、結合キー列がクエリ デザイナーには分からないので、外部キー列を主キー列へドラッグ&ドロップして、明示的に結合キー(リレーションシップ)を指示する必要があります。</p> <p><a href="http://lh3.ggpht.com/-e2yRoQ2xXik/UWA4ZUqF-mI/AAAAAAAAPb0/nVnnd7NNln0/s1600-h/image60.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-R1nPEcuZawI/UWA4g8-KNdI/AAAAAAAAPb8/Jr_Qu7G1AN0/image_thumb34.png?imgmax=800" width="468" height="437"></a></p> <p>このようにドラッグ&ドロップで指示したリレーションシップは、実際に FOREIGN KEY 制約が設定されるわけではありません。FOREIGN KEY 制約は、結合には必須ではないので、ドラッグ&ドロップで行っているのは、あくまでも結合キー列(ON 句)の指示のみです。</p> <h6>受注情報の取得</h6> <ol> <li>次に、受注に関する情報が格納されている「受注」テーブルと「受注明細」テーブルを結合し<br>てみましょう。それぞれのテーブルのデータは次のようになっています。<br><a href="http://lh3.ggpht.com/-zkXS6wgTy0o/UWA4mXDFBrI/AAAAAAAAPcE/yKTtcsvRAJY/s1600-h/image65.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-qykaTo6TXEY/UWA4t_dlXdI/AAAAAAAAPcM/4gallg5rUuQ/image_thumb37.png?imgmax=800" width="531" height="332"></a><br>「受注」テーブルと「受注明細」テーブルを結合するには、次のように「受注コード」列を結<br>合キーへ指定します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 受注明細.*, 受注.*<br>FROM 受注<br>INNER JOIN 受注明細<br>ON 受注.受注コード = 受注明細.受注コード</p></td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcGiqCemPqke7QwmU1TARfotS7qtHMonqo0GbylTfyE25QWYHP9Dd9lgTJPn3ZF7lsfyAtzdaw2zVlgtWrBULwWQQcS2_L9U4cyZAY9TMjyyz99cUwvrtqyj0eMk8AcUa4Ro16ahJmbbj3/s1600-h/image69.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-39BCglAuuqA/UWA46IbZ-HI/AAAAAAAAPcc/08C2Uz_UWDM/image_thumb39.png?imgmax=800" width="545" height="160"></a> <li>続いて、商品名が記述されている「商品」テーブルとも結合してみましょう。このテーブルと<br>結合するには、「商品コード」列を結合キーへ指定します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 受注明細.*, 商品名, 受注.*<br>FROM 受注<br>INNER JOIN 受注明細<br>ON 受注.受注コード = 受注明細.受注コード<br>INNER JOIN 商品<br>ON 受注明細.商品コード = 商品.商品コード</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-UJXi26dXKfA/UWA4_IWZMRI/AAAAAAAAPck/rPoIw982zk0/s1600-h/image73.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-KScI21gWOoo/UWA5GeUG9jI/AAAAAAAAPcs/A6aoPZpkEcI/image_thumb41.png?imgmax=800" width="554" height="174"></a><br>商品テーブルを結合することで、受注明細データを商品名で区別できるようになったことを確認できます。</li></ol> <h5 id="id003">売上集計</h5> <p>次に、集計関数(SUM や AVG、MAX など)とグループ化を行う GROUP BY 句を使って、<br>NorthwindJ データベースの売上集計を行ってみましょう。売上データは、前の手順で試したよう<br>に「受注明細」テーブルへ格納されているので、「単価」(受注時単価)と「数量」を掛け算すれば、<br>受注金額を計算できるようになります(正確には「割引」列も計算する必要がありますが、ここで<br>は省略します)。</p> <h6>Let's Try</h6> <ol> <li>まずは、"すべての受注金額の合計" を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT SUM(単価 * 数量) FROM 受注明細</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-sTC1LYsGKmI/UWA5LSEPONI/AAAAAAAAPc0/0STqFF9tcBg/s1600-h/image77.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-1MLPkbFCH0U/UWA5SYDmzII/AAAAAAAAPc8/ySXGj6dTcGs/image_thumb43.png?imgmax=800" width="306" height="134"></a><br>単価と数量を乗算したものに対して SUM 関数で合計を計算し、受注金額の合計を求めています。結果は、2,982 万1,530 円でした。 <li>次に、GROUP BY 句を利用して、"商品ごとの受注金額の合計" を計算してみましょう。次<br>のように記述します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 商品コード, SUM(単価 * 数量) FROM 受注明細<br>GROUP BY 商品コード</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-SsxTqEQgF-8/UWA5XcxVnMI/AAAAAAAAPdE/1XgAM8ULtCA/s1600-h/image81.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-d7FidQSOG9Q/UWA5eRQgHEI/AAAAAAAAPdM/x8Iyj5GJOHU/image_thumb45.png?imgmax=800" width="314" height="142"></a><br>商品コード「23」は 8 万400 円、「46」は 12 万5,000 円であったことを確認できます。 <li>商品コードごとの合計では、どの商品なのかが分かりづらいので、次は、"商品名ごとの受注金額" を取得してみましょう。これを取得するには、次のように「商品」テーブルと結合する<br>必要があります。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 商品名, SUM(単価 * 数量) FROM 受注明細<br>INNER JOIN 商品 ON 受注明細.商品コード = 商品.商品コード<br>GROUP BY 商品名</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-EOmOEng1JW8/UWA5jJcHVPI/AAAAAAAAPdU/ZxTBahS4ohY/s1600-h/image85.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-7uLlRNka-5g/UWA5qt7HQPI/AAAAAAAAPdc/5L0vk6DD_wc/image_thumb47.png?imgmax=800" width="487" height="161"></a><br>しかし、結果はエラーになります。「単価」列は、「受注明細」テーブルにも、「商品」テーブ<br>ルにも存在するからです。しかし、この2 つの列は、同じ名前の「単価」でも意味が異なりま<br>す。「受注明細」テーブルは “受注時” の単価、「商品」テーブルは “標準” の単価です。したがって、受注金額を計算するには、「受注明細」テーブルの「単価」列を利用する必要があります。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 商品名, SUM(受注明細.単価 * 数量) FROM 受注明細<br>INNER JOIN 商品 ON 受注明細.商品コード = 商品.商品コード<br>GROUP BY 商品名</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-rqQHTxWR8xg/UWA5v9lWsII/AAAAAAAAPdk/52wjVfWGcbk/s1600-h/image89.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg27NSibkYFknfud6UdnmyV3txaeTW1QG3K5lbXLmhWFv3YY1IyX4Xtk_JfgYBh8FBP5UdFdIuwvnPC8gWAMDtrG4Q4zt_DAc9tTbgKt8r-Oi8o79G7ceXhI1kOrIhT8WHkVeb34TRP-h76/?imgmax=800" width="481" height="229"></a> <li>次に、飲料や調味料、菓子類など、"商品区分名ごとの受注金額" を取得してみましょう。商<br>品区分名を取得するには、次のように「商品区分」テーブルを結合します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 区分名, SUM(受注明細.単価 * 数量) FROM 受注明細<br>INNER JOIN 商品 ON 受注明細.商品コード = 商品.商品コード<br>INNER JOIN 商品区分 ON 商品.区分コード = 商品区分.区分コード<br>GROUP BY 区分名</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-qKWxDXzj2ks/UWA58XI6F2I/AAAAAAAAPd0/oEWsdo6ofew/s1600-h/image93.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-_Z0A4KW0ums/UWA6Dh9_3dI/AAAAAAAAPd8/rz74u-2oTyk/image_thumb51.png?imgmax=800" width="486" height="251"></a> <li>次に、"受注年ごとの受注金額" を計算してみましょう。受注日(年月日)は、「受注」テーブ<br>ルへ格納されていますが、「YEAR」という関数を利用することで、年月日のうちの「年」の<br>みを取得できるようになります。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT YEAR(受注日), SUM(受注明細.単価 * 数量) FROM 受注明細<br>INNER JOIN 受注 ON 受注明細.受注コード = 受注.受注コード<br>GROUP BY YEAR(受注日)</p></td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDgSrF0EJyLh5oQTHDtuRI16DsysGcjNurjBCBSjosc0ktn81-AsCfRUJlXthkSQJQTRCNqISsNjaTLpQoIqagKW3BQEKG0ewCNEbrSnHAF1hyphenhyphenSZ1YJRK-FV9sBJr8gFyd3hyW03yH2q-y/s1600-h/image97.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-KtPc1sRUrbY/UWA6PzWkg9I/AAAAAAAAPeM/lvHAzoTr0vI/image_thumb53.png?imgmax=800" width="483" height="210"></a><br>2007 年は 868 万2,930 円、2005 年は 1,159 万9,700 円と、年ごとの受注金額の合計を<br>取得できたことを確認できます。 <li>次に、GROUP BY 句へ複数の列を指定して、"受注年ごと、かつ区分名ごとの受注金額" を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT YEAR(受注日), 区分名, SUM(受注明細.単価 * 数量) FROM 受注明細<br>INNER JOIN 受注 ON 受注明細.受注コード = 受注.受注コード<br>INNER JOIN 商品 ON 受注明細.商品コード = 商品.商品コード<br>INNER JOIN 商品区分 ON 商品.区分コード = 商品区分.区分コード<br>GROUP BY YEAR(受注日), 区分名<br>ORDER BY YEAR(受注日), 区分名</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-7xXJwZiaXLE/UWA6VYvw7rI/AAAAAAAAPeU/tU-jT_k8-hY/s1600-h/image103.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-TpoLtTg5FBI/UWA6dbJwUjI/AAAAAAAAPec/5W9MkOjj8Xg/image_thumb57.png?imgmax=800" width="597" height="681"></a><br>GROUP BY へ「区分名」列を指定するために、「商品」テーブルと「商品区分」テーブルを<br>結合しています。最後の ORDER BY 句は、結果を分かりやすくするために付けています。<br>なお、この結果には、全体の合計とそれぞれの列の合計(区分ごとの合計と年ごとの合計)は取得できていません。これを可能にするのが、次に説明する WITH ROLLUP と WITH CUBEです。</li></ol> <h5 id="id004">WITH ROLLUP と WITH CUBE</h5> <p>前述したように、GROUP BY 句で複数の列を指定した場合には、それらの列を組み合わせたグループ化を行うことはできますが、“全体の合計” と “それぞれの集計結果” を取得することはできません。<br>これらを取得できるようにするのが「WITH ROLLUP」と「WITH CUBE」です。この2つは、<br>GROUP BY 句の後へ記述します。</p> <h6>Let's Try: WITH ROLLUP</h6> <ol> <li>まずは、「WITH ROLLUP」を利用して、前の Step で取得した "受注年ごと、かつ区分名<br>ごとの受注金額" に加えて、"全体の受注金額の合計" と "年ごとの合計" を取得してみまし<br>ょう。<br><a href="http://lh5.ggpht.com/-S7kBgjKNlXU/UWA6iZX_sJI/AAAAAAAAPek/Xfunxr2FbXI/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-kIpj8IUWp8Q/UWA6p1OtB0I/AAAAAAAAPes/ReoadXzxbW4/image_thumb%25255B1%25255D.png?imgmax=800" width="580" height="186"></a><br><a href="http://lh3.ggpht.com/-kH3fq75YXwQ/UWA6u7iPLCI/AAAAAAAAPe0/bIWFN0HsIXw/s1600-h/image%25255B9%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-IwdQ3Y1m_PM/UWA62qt_fVI/AAAAAAAAPe8/nnGFzh9mSrI/image_thumb%25255B4%25255D.png?imgmax=800" width="451" height="454"></a><br>両方の列へ “NULL” と表示される結果が、全体の受注金額合計です。また、「区分名」列が<br>“NULL” と表示される結果が、年ごとの受注金額の合計です。このように、WITH ROLLUP<br>を利用すると、全体の集計と GROUP BY 句の “1 つ目の列” でグループ化した集計結果を取得できるようになります。</li></ol> <h6>WITH CUBE</h6> <p>WITH ROLLUP は、あくまでも 1 つ目の列の集計結果だけで、2 つ目の列「区分名」でグループ<br>化した集計結果を取得することができません。これを取得できるようにしたのが「WITH CUBE」<br>です。では、これを試してみましょう。</p> <ol> <li>前の手順で記述した SQL の「ROLLUP」の部分を「CUBE」へ置き換えて、実行してみまし<br>ょう。<br><a href="http://lh6.ggpht.com/-Zf46BGZkxX0/UWA67jnvQ8I/AAAAAAAAPfE/u2oQIogCzNQ/s1600-h/image%25255B13%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-pZHEEh5onWQ/UWA7C4CNGUI/AAAAAAAAPfM/7MZ-BlRGrzM/image_thumb%25255B6%25255D.png?imgmax=800" width="499" height="161"></a><br><a href="http://lh5.ggpht.com/-XYMUJDY_WKY/UWA7H3OhrFI/AAAAAAAAPfU/19rrm1y6QTM/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-KDoLlZDVEuw/UWA7PWaciYI/AAAAAAAAPfc/P1Imo3gk5i8/image_thumb%25255B8%25255D.png?imgmax=800" width="505" height="436"></a> <p>1 つ目の列に “NULL” と表示され、「区分名」列に “飲料” や “加工食品” と表示されるものが、区分ごとの受注金額の合計です。このように WITH CUBE を利用すると、複数列での集計結果を取得できるようになります。</p> <p><font color="#9b00d3">Note: CUBE(キューブ)= 立方体<br></font>Cube は「立方体」という意味で、サイコロやルービック キューブの形を思い浮かべると分かりやすいと思います。GROUP BY 句で 2 つの列を指定した集計は、2 次元での集計(クロス集計)、次の図のように 3 つの列を指定(年度と商品区分に加えて、支社ごとの集計も追加)した場合は、キューブの形で表現できるので、WITH CUBE<br>というわけです。<br><a href="http://lh5.ggpht.com/-i6bUxVPTy_0/UWA7US_L3XI/AAAAAAAAPfk/4NfZikZlbCE/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-FDzMGGrrscs/UWA7bovMujI/AAAAAAAAPfs/FB_yZiDTNTQ/image_thumb%25255B10%25255D.png?imgmax=800" width="513" height="184"></a><br>さて、実際の売上分析では、さらに得意先ごとの集計や担当社員ごとの集計を計算したり、3 次元よりも多くの次元(4 次元や5 次元以上)で分析をします。このような分析は、多次元(Multi Dimensional)分析と呼ばれ、専用の製品(OLAP:Online Analytical Processing というカテゴリに分類される製品)が販売されています。OLAP製品を利用すると、独自の多次元データベースを構築して高速にデータ集計を行えるようになります。なお、SQLServer の場合は、無償で同梱される Analysis Services がその機能(OLAP 機能)を持ち、多次元データベースのことを「キューブ」と呼んで管理しています。Analysis Services を利用すると、次のような集計を簡単、かつパフォーマンス良く行えるようになります。<br><a href="http://lh3.ggpht.com/-A3J8m2VJ8rs/UWA7hdY9nII/AAAAAAAAPf0/Mrhgbb0F3hc/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XSfhPMBD0y8/UWA7paEkG8I/AAAAAAAAPf8/vO6HBdTd89U/image_thumb%25255B13%25255D.png?imgmax=800" width="515" height="468"></a><br>GROUP BY 句による集計演算は、次元数が多くなればなるほど、指数関数的にパフォーマンスが劣化するので、これを回避するものとして、Analysis Services などの専用の製品が存在しています。本格的な分析を行いたい場合には、Analysis Services の利用を検討することをお勧めします(Analysis Services は、SQL Server に付属の標準機能です)。Analysis Services については、本自習書シリーズの「Analysis Services 入門」編で、基礎からステップ バイ ステップ形式で簡単に試せるように説明していますので、こちらもぜひご覧いただければと思います。</p> <p><font color="#9b00d3">Note: Oracle での ROLLUP と CUBE<br></font>SWITH ROLLUP と WITH CUBE は、SQL Server 独自の記述方法で、SQL-1999 標準規格(1999 年に規格化された SQL 標準)では次のように記述します(Oracle などでは以下のように記述)。<br>GROUP BY ROLLUP( YEAR(受注日), 区分名 )GROUP BY の隣に ROLLUP または CUBE を記述して、グループ化対象の列をカッコで囲みます。</p> <p><font color="#9b00d3">Note: GROUPING SETS<br></font>SQL Server 2008 からは、WITH CUBE と同じように多次元での集計結果を取得できる関数として GROUPINGSETS が追加されています。これは、SQL-2003 標準規格(2003 年に規格化された SQL 標準)で定義されているもので、Oracle 10g で搭載されている GROUPING SETS とも同じように利用することができます。<br>この GROUPING SETS は、GROUP BY 句とともに利用できる集計関数で、複数の集計値を連結(UNION ALL)する場合や、WITH ROLLUP または CUBE を利用して複数項目の集計値を取得する場合の置き換えとして利用することができます。<br>たとえば、前述の WITH CUBE で取得した、年ごと・商品区分ごとの合計を取得したものは、次のように記述することができます。<br><a href="http://lh5.ggpht.com/-vU4YlwA16lY/UWA7uGX4GzI/AAAAAAAAPgE/iBCbsp2yaHo/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-SsBY4x74Wko/UWA71r86NsI/AAAAAAAAPgM/YCOriYRVb4E/image_thumb%25255B16%25255D.png?imgmax=800" width="419" height="569"></a><br>GROUPGING SETS については、本自習書シリーズの「SQL Server 2008 からの新機能ダイジェスト」編で詳しく説明しています。<br></p></li></ol> <h5 id="id005">クロス集計</h5> <p>WITH ROLLUP と WITH CUBE では、結果が縦に表示されてしまうので、これを次のように 2<br>次元(クロス集計)の形式でわかりやすく取得することも可能です。<br><a href="http://lh5.ggpht.com/-ES8zPdFtn7E/UWA76rzKqXI/AAAAAAAAPgU/YHH_I-JCOl4/s1600-h/image%25255B35%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-c0tRMAmGQCo/UWA8B0nMwbI/AAAAAAAAPgc/2IN5UhT4Guc/image_thumb%25255B18%25255D.png?imgmax=800" width="472" height="226"></a></p> <h6>Let's Try</h6> <ol> <li>年度を列として表示するには、次のように “CASE 式” を SUM 関数の中で利用します。<br><a href="http://lh3.ggpht.com/-9CJuomHqSSg/UWA8G31vIhI/AAAAAAAAPgk/T0DtNP_vioQ/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-GnRaZdj3ZLQ/UWA8OevXe5I/AAAAAAAAPgs/GTFdJj_0x2g/image_thumb%25255B21%25255D.png?imgmax=800" width="594" height="385"></a><br><a href="http://lh3.ggpht.com/-MJnRPWNiBNg/UWA8TcB9PcI/AAAAAAAAPg0/NLu6gByBcjo/s1600-h/image%25255B45%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-5p_1vYVwb00/UWA8awn3fyI/AAAAAAAAPg8/1WtCvRTEk1Q/image_thumb%25255B24%25255D.png?imgmax=800" width="595" height="583"></a><br>YEAR (受注日) で取得した値を条件に、該当年のみを計算するようにすれば、クロス集計形式で結果を取得できるようになります。</li></ol> <h5 id="id006">PIVOT によるクロス集計</h5> <p>SQL Server 2005 からは、PIVOT 演算子が追加されて、前の Step のように CASE 式を利用<br>しなくてもクロス集計結果を取得できるようになりました。これは次のように利用します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM<br>(SELECT 区分名, YEAR(受注日) AS 年<br>,受注明細.単価 * 数量 AS 受注金額<br>FROM 受注明細<br>INNER JOIN 受注 ON 受注明細.受注コード = 受注.受注コード<br>INNER JOIN 商品 ON 受注明細.商品コード = 商品.商品コード<br>INNER JOIN 商品区分 ON 商品.区分コード = 商品区分.区分コード<br>) p<br>PIVOT ( SUM(受注金額) FOR 年 IN ([2005],[2006],[2007]) ) AS pvt<br>ORDER BY 区分名</p></td></tr></tbody></table></p> <p><a href="http://lh3.ggpht.com/-Bwl7OY1GKC8/UWA8gZyJVzI/AAAAAAAAPhE/KIxpNuJ3Bbk/s1600-h/image%25255B51%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-3LVxVmUmjOc/UWA8n2LzFYI/AAAAAAAAPhM/v3N1-7MDYHU/image_thumb%25255B28%25255D.png?imgmax=800" width="496" height="413"></a><br></p> <p>サブクエリとして集計対象となるデータを取得し、それに対して PIVOT 演算子で SUM 関数で<br>処理しています。「FOR ~ IN」の部分で列として表示したい値を列挙します。</p> <h5 id="id007">ビュー: CREATE VIEW</h5> <p>SELECT ステートメントで結合するテーブルが増えてくると、記述が複雑になってきます。このよ<br>うな場合に、SELECT ステートメントを 1 つのパッケージとして保存できる機能があります。そ<br>れが「ビュー」(View)機能です。ビューを作成するには、CREATE VIEW ステートメントを利<br>用します。構文は、次のとおりです。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiz42NZnN8Oq7eIxkPGgIiIIKA3cS3ssRii7tAcyXtfmJ1ECh_lIQ_zs2UAubVNp9csRQvv1Jl9ZPyXwHe8PTjXKRGkifl1hEBAiCLaYxjnVZC6RDS1CRsgb8M1Bk1YwSGC6zi3JG1KqgD/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-DF3u8HRAYyY/UWA8z9UQelI/AAAAAAAAPhc/qfybZ9J37Eg/image_thumb%25255B29%25255D.png?imgmax=800" width="199" height="83"></a></p> <h6>Let's Try</h6> <ol> <li>まずは、「受注」と「受注明細」、「商品」、「商品区分」テーブルを結合する SELECT ステー<br>トメントに対して、「受注商品一覧」という名前のビューを作成してみましょう。<br><a href="http://lh6.ggpht.com/-cKK-EGGbcSU/UWA840CWuSI/AAAAAAAAPhk/bCALq8SlFS4/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Pupb_HBg_7A/UWA9AZyT9UI/AAAAAAAAPhs/Bmgfw6z1QNs/image_thumb%25255B32%25255D.png?imgmax=800" width="530" height="377"></a><br>結果はエラーになります。ビューを作成する場合は、SELECT で取得する列に同じ名前の列が存在しているとエラーになります(上の例では「*」ですべての列を取得しています)。したがって、次のように特定の列のみを取得するように変更しなければなりません。<br><a href="http://lh6.ggpht.com/-jE7mD5VYyjc/UWA9FRssrfI/AAAAAAAAPh0/y9XHT_EApO4/s1600-h/image%25255B63%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-as-7Ap_gByg/UWA9Mz-acSI/AAAAAAAAPh8/Z-3DUqgWLwM/image_thumb%25255B34%25255D.png?imgmax=800" width="542" height="381"></a> <li>作成したビューは、次のようにオブジェクト エクスプローラーの[ビュー]フォルダーを展<br>開すると確認することができます。<br><a href="http://lh4.ggpht.com/-n0mFc_Dvb80/UWA9R-BoQWI/AAAAAAAAPiE/Qj8qdS4MTCw/s1600-h/image%25255B67%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-5SBd7wYHoY0/UWA9ZLD2BRI/AAAAAAAAPiM/cWVlEm1TQ08/image_thumb%25255B36%25255D.png?imgmax=800" width="403" height="471"></a> <p><font color="#9b00d3">Note: バッチの先頭ではない場合のエラー<br></font>CREATE VIEW ステートメントは、バッチの先頭で実行しないと、次のエラーが発生します。<br><a href="http://lh4.ggpht.com/-9eLmwhGSsIs/UWA9eDQnaQI/AAAAAAAAPiU/klym_3ZTp5c/s1600-h/image%25255B71%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPqQ6ou_YaGcHGTAmIFReVP6rOd05IJq2yyLbqhQt2YDX_YkW291-qmRP46ZA7DdBweBvBmH8xEppiQBQQcZ9E70e6iQMNfQdb8Fuq4FPBWS2xrbW7gGk3u1nSc4CLmC-R0meT3h8Dcaoi/?imgmax=800" width="414" height="188"></a><br>バッチの区切りは、「go」コマンドで行えるので、CREATE VIEW ステートメントを実行する上へ「go」を追加すれば、このエラーを回避することができます。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTbNssC0DhMLe3esZTcMN6e7GhGIBz70gxb-tCC5dlwh4aIYB9pXNOKv2-SqT1U3ELb2hlhlpluj4l_C_QCeTx4jlRuo5RupU9OY0ZiHS9K1SEnilAqYuqqSWiwq4qY6hzIJ_ztfVqt7uG/s1600-h/image%25255B75%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-axwVUmarkk4/UWA9xZIolvI/AAAAAAAAPis/Gi4_aURvfp8/image_thumb%25255B40%25255D.png?imgmax=800" width="420" height="134"></a></p> <p> </p> <p> </p></li></ol> <h6>ビューの参照</h6> <ol> <li>作成したビューを参照するには、テーブルを利用する場合と同じように SELECT ステートメ<br>ントの FROM 句で指定します。<a href="http://lh6.ggpht.com/-fuAAdozPU8M/UWA92f-katI/AAAAAAAAPi0/mPmRLmMcqWI/s1600-h/image%25255B79%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-VeoDI7sbewc/UWA99loCzvI/AAAAAAAAPi8/HupYWTroHpo/image_thumb%25255B42%25255D.png?imgmax=800" width="497" height="335"></a><br>これにより、ビューの中で定義した SELECT ステートメントが実行されて、4 つのテーブル<br>を JOIN した結果を取得できたことを確認できます。 <li>次に、GROUP BY 句を利用して、"区分ごとの受注金額の合計" を取得してみましょう。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbdykUkYCrHjcoNBGujSy-rsMb5SvACTkAXx2O2ba44MQ7sIc88hePiaEx3eLX_fTBaL5WFZ0NOAavEpaCVrM0qf5nB_Q7abcuF_kUDmyh017alQhQ3T4WBz-zu5oAS06b_jhuosxa5wMi/s1600-h/image%25255B83%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-jqIVjuozWsU/UWA-J44J7rI/AAAAAAAAPjM/B-Tp03qcVww/image_thumb%25255B44%25255D.png?imgmax=800" width="458" height="409"></a><br>わずか 2 行のステートメントだけで、区分ごとに受注金額の合計を取得できたことを確認で<br>きます。このように、ビューを利用すれば、複雑な JOIN を記述することなく、複数のテー<br>ブルをまたがったデータ取得を簡単に行えるようになります。なお、ビューは、テーブルと同<br>じように利用できるので “仮想表” とも呼ばれています。 <p><font color="#9b00d3">Note: ACCESS のクエリ機能に相当<br></font>ビューは、Access の「クエリ」機能に相当します。ただし、Access のクエリとまったく同じというわけではありません。Access のクエリには、“パラメーター化” を行う機能がありますが、ビューにはないからです。SQL Serverでパラメーター化クエリを作成したい場合は、「ストアド プロシージャ」機能を利用する必要があります。ストアド プロシージャについては、本自習書シリーズの「開発者のための Transact-SQL 応用」編で詳しく説明していますので、こちらもぜひご覧いただければと思います。</p> <p><font color="#9b00d3">Note: Management Studio でのビューの作成: ビュー デザイナー<br></font>Management Studio の「ビュー デザイナー」機能を利用すると、Access でクエリを作成するのと同じような感覚で、GUI 操作でビューを作成することができます。これは、次のように操作します。<br><a href="http://lh3.ggpht.com/-wAntytc9xpo/UWA-PEsiJxI/AAAAAAAAPjU/hxkL4T0_UuQ/s1600-h/image%25255B88%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-_4nteSsoSoY/UWA-XA3YpnI/AAAAAAAAPjc/f3VqF7Gs4_A/image_thumb%25255B47%25255D.png?imgmax=800" width="591" height="450"></a></p> <p><font color="#9b00d3">Note: ビュー経由の更新<br></font>ビューに対して、UPDATE や INSERT、DELETE など、更新系のステートメントを実行することも可能です。ただし、ビュー内で GROUP BY や集計関数、DISTINCT 処理を行っていたり、演算結果を行っている列に対しては、更新系のステートメントを実行することはできません。なお、このような場合に「INSTEAD OF トリガー」と呼ばれるトリガーを作成すると、ビューに対して更新系のステートメントが実行されたときに、別の処理を記述できるようになります(ビューのもとになっているテーブルを直接更新するようなステートメントを記述できるよ<br>うになります)。</p></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-66496310600109795272013-04-05T14:58:00.001+09:002013-04-24T15:54:35.184+09:00◆アプリケーションからの利用<ul> <li><a href="#id001">この Step で作成するアプリケーション</a> <li><a href="#id002">ReportViewer コントロールからのアクセス</a> <li><a href="#id003">パラメーターの指定</a> <li><a href="#id004">ASP.NET Web フォームからのアクセス</a> </li></ul> <h5 id="id001">この Step で作成するアプリケーション</h5> <p>Windows アプリケーション<br><a href="http://lh6.ggpht.com/-5rj_HXSTVqg/UV5nuZn-24I/AAAAAAAAPTM/wsykl76mMkg/s1600-h/image3.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-b0L4JI7F1Lo/UV5nuwrn7DI/AAAAAAAAPTU/VDfZwZ8PHSI/image_thumb1%25255B1%25255D.png?imgmax=800" width="470" height="196"></a></p> <p>ASP.NET アプリケーション<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ3yD7VOsxTbi4Q96rsCLCNqihLZwcuQlchGtrMxRgqiGMzQ1-8MAFGmHbqN_x7UgB9aRl9O5Kq39v1PHTBjsJX9obbZXug2fnR01nW9BJPRhweyIpoCYcMk__4WcqBOhEsUuz4uxA-CyP/s1600-h/image7.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-8Y-4weLLqHM/UV5nv0h_6lI/AAAAAAAAPTk/FlINF579MFM/image_thumb3.png?imgmax=800" width="464" height="403"></a></p> <h5 id="id002">ReportViewer コントロールからのアクセス</h5> <h6>ReportViewer コントロールからのアクセス</h6> <p>Windows フォームや Web フォームなどのアプリケーションから、レポートへアクセスするには、ReportViewer コントロールを利用します。<br><a href="http://lh4.ggpht.com/-W232FFK0Nu8/UV5nwXaYuyI/AAAAAAAAPTs/a06naFltSV4/s1600-h/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-TM5mFDHPbaU/UV5nw-xt-1I/AAAAAAAAPT0/ZCeDX22hjso/image_thumb5.png?imgmax=800" width="464" height="171"></a></p> <p>それでは、実際に SalesRepo レポートへアクセスする Windows アプリケーションを作成してみましょう。ここでは、Visual Studio 2010 を例に説明します。</p> <ol> <li>まずは、Visual Stduio 2010 を起動して、[ファイル]メニューから[新規作成]→[プロジェクト]をクリックします。 <li>[新しいプロジェクト]ダイアログが表示されたら、[インストールされたテンプレート]から今回は「Visual Basic」を選択して、「Windows フォーム アプリケーション」を選択します。<br><a href="http://lh3.ggpht.com/-yTWRKZePFHk/UV5nxEL0P3I/AAAAAAAAPT8/1IkwzmUiTVw/s1600-h/image15.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/--tcREuedaWw/UV5nxrLjcTI/AAAAAAAAPUE/tvq561DUeNE/image_thumb7.png?imgmax=800" width="348" height="104"></a><br>[名前]と[場所]へ任意のプロジェクト名と保存場所を入力して、[OK]ボタンをクリックします。 <li>次に、ツールボックスの[レポート]カテゴリから、[ReportViewer]コントロールを Windows フォーム上へドラッグ&ドロップします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6r2Fsi3nUhHrL0wgRdXYuZppIzoJabLZ8JFpL390cUwAOMPE0J9jGJJ8q-tJizyuIHdNV9PkbiSKbKmVvI444SViF6V4Z0r7xOccpHfBEHQ9jHXZ2CPUg64EwKYDQwL2AuOrmNmdKiHe_/s1600-h/image19.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Sl0lSeo2Oy0/UV5nyc9EnbI/AAAAAAAAPUU/J3LsA5f4Cx8/image_thumb9.png?imgmax=800" width="345" height="200"></a> <li>配置後、ReportViewer コントロールの右上に出ている「」をクリックします。<br><a href="http://lh3.ggpht.com/-P6dzqP79Ufk/UV5nzOOgcmI/AAAAAAAAPUc/SwtnC1nEqDQ/s1600-h/image24.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-XDLvEqu8nRI/UV5nzT6xsHI/AAAAAAAAPUk/Wntkm1fMt-k/image_thumb12.png?imgmax=800" width="587" height="157"></a><br>これにより、[ReportViewer タスク]スマート タグが表示されるので、[レポートの選択]で「サーバー レポート」を選択します。[レポート サーバーの URL]は、「http://localhost/reportserver」と設定されていることを確認し(必要に応じてサーバー名を変更し)、[レポート パス]へレポートのパス(今回は /SalesRepo)を入力します。 <li>次に、ReportViewer コントロールの大きさを調整して、レポートを表示しやすくしてから、ツールバーの[デバッグ開始]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-8GnHLNm8xrA/UV5nz3cPqMI/AAAAAAAAPUs/JMnjRv-FpEA/s1600-h/image27.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-PzC1S9P-5Ow/UV5n0azF6rI/AAAAAAAAPU0/A62Y8zu6OPM/image_thumb13.png?imgmax=800" width="95" height="65"></a> <li>これにより、Windows アプリケーションが起動して、レポートが表示されることを確認できます。<br><a href="http://lh5.ggpht.com/-fXGME_oct1s/UV5n04kflxI/AAAAAAAAPU8/WhYDtMNjXkc/s1600-h/image31.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-C6Pod7qJF4A/UV5n1cy0WoI/AAAAAAAAPVE/JRqVQxF3qrM/image_thumb15.png?imgmax=800" width="477" height="195"></a><br>任意の商品区分を選択して、[レポートの表示]ボタンをクリックすると、選択した商品区分のみが表示されることを確認できます。<br>このように、ReportViewer コントロールを利用すると、アプリケーションから簡単にレポートへアクセスできるようになります。</li></ol> <h6>ファイルへのエクスポート</h6> <p>次に、起動した Windows アプリケーションから、ファイルのエクスポートをしてみましょう。</p> <ol> <li>次のように[エクスポート]ボタンをクリックして、今回は「Excel」をクリックします。<br><a href="http://lh6.ggpht.com/-Vy4VECSBm8g/UV5n1l8xHMI/AAAAAAAAPVM/HZgMiLLxMAk/s1600-h/image35.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-pz4MJzUoYDo/UV5n2D7oStI/AAAAAAAAPVU/BTHXZMfPIAE/image_thumb17.png?imgmax=800" width="476" height="232"></a> <li>[名前を付けて保存]ダイアログが表示されたら、[ファイル名]へ任意の名前を入力して、[保存]ボタンをクリックします。 <li>保存した Excel ファイルを開きます。<br><a href="http://lh6.ggpht.com/-OyO4BV71fXI/UV5n2eEK0zI/AAAAAAAAPVc/TYfqW-YrXSg/s1600-h/image38.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-An8wOptvSqw/UV5n22ITNfI/AAAAAAAAPVk/yW2szqFwXlE/image_thumb18.png?imgmax=800" width="244" height="180"></a><br>このように、Windows アプリケーションからでも、ファイルへのエクスポートを簡単に行うことができます。</li></ol> <h6>エクスポート ボタンの非表示</h6> <p>ReportViewer コントロールでは、プロパティを設定して、レポート ツールバーをカスタマイズすることもできます。それではこれを試してみましょう。</p> <ol> <li>配置した ReportViewer コントロールをクリックして、ReportViewer コントロールの[プロパティ]を表示します。<br><a href="http://lh4.ggpht.com/-NuRTM9ldBWY/UV5n3aLQKTI/AAAAAAAAPVs/aOLadDNf5-k/s1600-h/image42.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-S8n7kbO3-8w/UV5n37W7gnI/AAAAAAAAPV0/pAaZUPSxXDw/image_thumb20.png?imgmax=800" width="397" height="269"></a><br>プロパティの一覧から、[ツール バー]セクションにある[ShowExportButton]を「False」へ設定します。これで、エクスポート ボタンを表示しないようにすることができます。 <li>[デバッグ開始]ボタンをクリックして、結果を確認してみましょう。<br><a href="http://lh3.ggpht.com/-CKtZ-bV8rKE/UV5n4NMc15I/AAAAAAAAPV8/6puqWEY24Ao/s1600-h/image46.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCnoirW-2P9gywBQ5mXeObHE_99yGiHGubuaVwX_4i8IuIbAKadJTzLq15rIk-umiXHMqn2vaZYfJBiaoGHmL8b0peefnVCVfqSPEn4jBmD1ClqZy8S53MOumlUJTw6th-pzbQMSU7qrHc/?imgmax=800" width="395" height="172"></a><br>このように、ReportViewer コントロールの[ツールバー]セクションにあるプロパティを利用すれば、レポート ツールバーにあるボタンの表示/非表示を設定することができます。</li></ol> <h5 id="id003">パラメーターの指定</h5> <h6>パラメーターの指定</h6> <p>ReportViewer コントロールで、パラメーターを指定するには、Microsoft.Reporting.Win Forms 名前空間の ReportParameter クラスを利用します。</p> <ol> <li>Windows フォームをダブル クリックして、Form_load イベント ハンドラーを生成し、次のようにコードを記述します。<br><a href="http://lh5.ggpht.com/-nrDeNRLZv30/UV5n5FPiIDI/AAAAAAAAPWM/D0c0iQUchgA/s1600-h/image50.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-xIjAGnXz86A/UV5n5igIbEI/AAAAAAAAPWU/ZmcV3uxoXGo/image_thumb24.png?imgmax=800" width="592" height="349"></a> <li>[デバッグ開始]ボタンをクリックして、結果を確認します。<br><a href="http://lh5.ggpht.com/-mMLbDPL4UxQ/UV5n59-brRI/AAAAAAAAPWY/lHCHAhQTh_8/s1600-h/image54.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-gZHkV7IjCJE/UV5n6amFvJI/AAAAAAAAPWg/r286lY3CYns/image_thumb26.png?imgmax=800" width="458" height="223"></a><br>p1 パラメーター(商品区分コード)が 3の菓子類のデータが表示されることを確認できます。<br>このように、ReportParameter オブジェクトを利用すると、プログラム内からレポートのパラメーターを指定することができます。 <p><font color="#9b00d3">Note: 複数選択可能なパラメーターがある場合</font><br>「年」パラメーター(p2)のように、複数選択可能なパラメーターがある場合は、次のように配列として、値を代入する必要があります。<br><a href="http://lh3.ggpht.com/-1j5BEe9mf0Y/UV5n61XhScI/AAAAAAAAPWo/RjQh_ZUqoXI/s1600-h/image58.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-o2Rr2bR6UV4/UV5n7Y6up8I/AAAAAAAAPW0/s89H-kan77M/image_thumb28.png?imgmax=800" width="445" height="541"></a><br>(C#)<br><a href="http://lh5.ggpht.com/-ay0xTJMQdj4/UV5n75m3O7I/AAAAAAAAPW8/btU7Oz1uuoI/s1600-h/image4%25255B1%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-vJKVJyhA6z4/UV5n8Qajt3I/AAAAAAAAPXE/eDSBZVZb8yU/image_thumb1.png?imgmax=800" width="446" height="138"></a><br></p></li></ol> <h5 id="id004">ASP.NET Web フォームからのアクセス</h5> <p>ASP.NET Web フォームからレポートへアクセスする場合にも、Windows フォームの場合と同じように ReportViewer コントロールを利用します(利用方法についても、Windows フォームの場合とまったく同じです)。</p> <p><font color="#ff0000">※</font>試してみたが、私の環境ではスクリプトエラーが発生し動作しなかった。(VS2012)<br> JavaScriptのバージョンを選んだりするのかしらん?<br><br>その後、もう少し試してみました。<br>エラーが発生するのは、自習書99ページのカスケードパラメータを使った場合。<br>商品区分を選ぶと、その商品区分の商品だけがコンボボックスに表示されるパターン。<br>この時、商品コンボボックスを表示するとエラーとなる。<br>情けないことに、これ、IEのバグの様です。(IE9以降で発生)<br>他のブラウザ(FireFox,Chrome,Opera)では発生しません。<br><a href="http://lh6.ggpht.com/-Z6W6Z3-SaVk/UXeBpv8w_bI/AAAAAAAAPoY/CUTpofdkWAc/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-UQz_Ihq8ZKA/UXeBqc5DI3I/AAAAAAAAPog/kBT2PhyqcnM/image_thumb%25255B1%25255D.png?imgmax=800" width="476" height="242"></a></p> <p>とりあえず以下のようなスクリプトを追加してエラー発生時に「getComputedStyle」関数を置き換えることで回避できるようです。</p> <table cellspacing="0"> <tbody> <tr> <td> <table bordercolor="#29e000" cellspacing="0" cellpadding="7" bgcolor="black" border="1"> <tbody> <tr> <td> <table cellspacing="0" cellpadding="7" width="630" bgcolor="white" border="1"> <tbody> <tr> <td valign="top" width="610"> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff"><font style="font-size: 9.5pt"><</font></font></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#800000">script</font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"> </font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#ff0000">type</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#0000ff">="text/javascript"></font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9.5pt"> </font></font></span></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">if</font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"> (window.getComputedStyle != </font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">null</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000">) {</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9.5pt"> </font></font></span></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">var</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000"> orginalGetComputedStyle = window.getComputedStyle;</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">window.getComputedStyle = </font></font></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">function</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000"> (element, parm) {</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9.5pt"> </font></font></span></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">try</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000"> {</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9.5pt"> </font></font></span></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">return</font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"> orginalGetComputedStyle(element, </font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">null</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000">);</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font face="MS ゴシック"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">}</font></font></font></span></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt"><span style="mso-spacerun: yes"> </span></font></font></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">catch</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000"> (err) {</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9.5pt"> </font></font></span></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">return</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000"> orginalGetComputedStyle(</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">document.getElementsByTagName(</font></font></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#a31515">"body"</font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#000000">)[0], </font></span><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff">null</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#000000">);</font></span></font></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font face="MS ゴシック"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">}</font></font></font></span></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font face="MS ゴシック"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">}</font></font></font></span></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; line-height: normal; text-autospace: ; mso-layout-grid-align: none" align="left"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font face="MS ゴシック"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9.5pt"> </font></span><font style="font-size: 9.5pt">}</font></font></font></span></p> <p class="MsoNormal" style="margin: 0mm 0mm 0pt; text-justify: inter-ideograph; line-height: normal" align="justify"><font face="MS ゴシック"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#0000ff"><font style="font-size: 9.5pt"></</font></font></span><font style="font-size: 9.5pt"><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font color="#800000">script</font></span></font><span lang="EN-US" style="font-family: ; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; color: ; mso-bidi-font-family: 'MS Gothic'; mso-highlight: white; mso-font-kerning: 0pt"><font style="font-size: 9.5pt" color="#0000ff">></font></span></font></p></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table> <p>この様な不具合がIE9からそのままという事は、この機能もほとんど使われていないのでしょうね・・・。</p> <ol> <li>Visual Stduio 2010 を起動し、[ファイル]メニューから[新規作成]→[プロジェクト]をクリックします。<br><a href="http://lh4.ggpht.com/-PIjVH6RsFkE/UV5n807W4rI/AAAAAAAAPXM/A4jzppDe_iY/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-noJLFW3LD8Q/UV5n9Ave6kI/AAAAAAAAPXU/Apk7IcLL5l0/image_thumb%25255B1%25255D.png?imgmax=800" width="577" height="138"></a> <li>[新しいプロジェクト]ダイアログが表示されたら、[インストールされたテンプレート]から「Visual Basic」を選択して、「ASP.NET Web アプリケーション」を選択します。<br><a href="http://lh5.ggpht.com/-sQI2DZ3piMk/UV5n9-DzHAI/AAAAAAAAPXc/BgxflyfspyQ/s1600-h/image%25255B9%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-g1YWE2SEi8M/UV5n-Z66a3I/AAAAAAAAPXk/NdwvWpMAoLU/image_thumb%25255B4%25255D.png?imgmax=800" width="576" height="397"></a> <li>次に、[デザイン]タブをクリックしてデザイン画面を表示して、ツールボックスの[AJAX Extensions]カテゴリから、[ScriptManager]を Web フォーム上へドラッグ&ドロップします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEja1GpuWrWZjw0rQM6ZNyO4Su1oaaJJqk6D91GV5X37Ww2aAQIFqmFmOLMRsLQwoUS2RozNB_xa78gw-EW_qv9D4zl0vFCjGpKLBwt1RtLYfoez489t95Jw6KI9Cv9I808d8R-tBnlig2dR/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-rPh_ESf3NYM/UV5n_BwgSdI/AAAAAAAAPX0/3K1J8jeO9rY/image_thumb%25255B7%25255D.png?imgmax=800" width="568" height="432"></a> <p><font color="#9b00d3">Note: Visual Studio 2010 では、ReportViewer が AJAX 対応のコントロール</font><br>Visual Studio 2010 からは、ReportViewer コントロールが AJAX 対応のコントロールに変更されました。このため、ScriptManager コントロールの配置が必要になります。</p> <li>次に、ツールボックスの[レポート]カテゴリから[ReportViewer]コントロールを Web フォーム上へドラッグ&ドロップします。<br><a href="http://lh4.ggpht.com/-rgP3bGf8h5A/UV5n_pRLacI/AAAAAAAAPX8/LT33BJCZxss/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-j_zsMRGGfso/UV5oADVC2-I/AAAAAAAAPYE/tlk_75ARgfg/image_thumb%25255B9%25255D.png?imgmax=800" width="562" height="302"></a> <li>[ReportViewer タスク]スマート タグが表示されたら、[レポートの選択]で「サーバー レポート」を選択し、[レポート サーバーの URL]へは、「http://localhost/reportserver」と設定されていることを確認します。[レポート パス]へは、レポートのパス(今回は /SalesRepo)を入力します。<br><a href="http://lh3.ggpht.com/-QBDyV57S7lg/UV5oAoCKt_I/AAAAAAAAPYM/8ily1eSoB38/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-36xP9_gxO8A/UV5oBPocADI/AAAAAAAAPYU/3li7bSanvF0/image_thumb%25255B11%25255D.png?imgmax=800" width="559" height="226"></a> <li>ツール バーの[デバッグ開始]ボタンをクリックして、実行します。<br><a href="http://lh3.ggpht.com/-GI5qlYrHUyE/UV5oB_sBNJI/AAAAAAAAPYc/7A8FfBiMrlE/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-DIV9gucBDH8/UV5oCDqFCqI/AAAAAAAAPYk/YEQkRekTwzE/image_thumb%25255B14%25255D.png?imgmax=800" width="555" height="463"></a><br>レポートが表示されたら、任意の商品区分名を選択して、[レポートの表示]ボタンをクリックし、選択した商品区分のデータが、正しく表示されることを確認します。<br>このように、ASP.NET Web アプリケーションでも、Windows アプリケーションと同様に ReportViewer コントロールを利用して、簡単にレポートへアクセスすることができます。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com1tag:blogger.com,1999:blog-7901251401713078028.post-91964419018926306862013-04-03T22:46:00.001+09:002013-04-03T22:46:28.103+09:00◆その他の SQL-92 規格<ul> <li><a href="#id001">サブクエリ(副問い合わせ)</a> <li><a href="#id002">UNION ALL</a> <li><a href="#id003">CASE 式</a> </li></ul> <h5 id="id001">サブクエリ(副問い合わせ)</h5> <p>SQL では、SELECT ステートメントの中に SELECT ステートメントを記述して、入れ子にして<br>利用することができます。このようなクエリは、サブクエリ(副問い合わせ)と呼ばれています。</p> <h6>Let's Try</h6> <ol> <li>まずは、”給与が最も高い社員” を検索してみましょう。これは、次のようにサブクエリを利<br>用して、WHERE 句の条件式へ給与の最大値(MAX(給与))を与えることで、結果を取得す<br>ることができます。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM 社員<br>WHERE 給与 = (SELECT MAX(給与) FROM 社員)</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-8AsfJNDgGRg/UVwvsVMuobI/AAAAAAAAPPE/tGRAOjdn4EE/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XNvz447AFRk/UVwvzlWLs3I/AAAAAAAAPPM/xomxaSJiKY4/image_thumb%25255B1%25255D.png?imgmax=800" width="347" height="124"></a><br>給与が最も高い社員(松田さん)を取得できたことを確認できます。<br>このように SELECT ステートメントをカッコで囲むことで、SELECT ステートメントを入れ<br>子にすることができます。 <li>次に、”平均給与よりも高い給与をもらっている社員” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM 社員<br>WHERE 給与 > (SELECT AVG(給与) FROM 社員)</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-ImNXs8FWtso/UVwv4YxsXdI/AAAAAAAAPPU/U01Fli4339w/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-81IhEzrmyls/UVwv_vN4VgI/AAAAAAAAPPc/6fq3zPghAcw/image_thumb%25255B4%25255D.png?imgmax=800" width="331" height="166"></a><br><br>サブクエリとして、給与の平均値(AVG(給与))を WHERE 句の条件式へ与えることで、"平均給与よりも高い給与の社員" を取得することができます。<br><br><font color="#9b00d3">Note: サブクエリの SELECT ステートメントの結果が複数件ある場合</font><br>サブクエリでは、カッコ内の SELECT ステートメントの結果が複数件ある場合は、「=」や「>」などの比較演算子を利用するとエラーになります。たとえば、次のように GROUP BY 句を使って、"部門ごとの給与の最大値(MAX)" を取得し、その社員を取得するようにすると、結果はエラーになります。<br><a href="http://lh3.ggpht.com/-l2lGS-cfRSU/UVwwEt9M1yI/AAAAAAAAPPk/-tdxQt6nfWA/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-pOGVxy7JSqU/UVwwMDIqPMI/AAAAAAAAPPs/paiI6wILVrg/image_thumb%25255B6%25255D.png?imgmax=800" width="450" height="175"></a><br>このエラーを回避するには、次のように、IN 演算子を利用することができますが、取得した結果は正しくありません(余分なデータが取得されています)。<br><a href="http://lh4.ggpht.com/-t4ptRIdOj3I/UVwwRDKr-EI/AAAAAAAAPP0/nxm5cde69NQ/s1600-h/image%25255B16%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-1-fobbSKRyk/UVwwYVfJt9I/AAAAAAAAPP8/6x-lDlfbjJY/image_thumb%25255B8%25255D.png?imgmax=800" width="451" height="181"></a><br>“鈴木 太郎” さんの給与は30 万円で部門番号 ”10” の中での最大給与ではありません。部門番号 ”10” の中での最大給与は、“松田 裕太”さんの 70 万円です。一方、“小笠原 翔”さんは、部門番号 ”20“ の社員で、部門番号 “20”の中では最大給与の 30 万円です。このように、正しくない結果が返るのは、カッコ内のステートメントが返す値が「70 万円と 30 万円」で、次のように解釈されてしまっているためです。<br><a href="http://lh6.ggpht.com/-LrZmGUWvRrw/UVwwdcvc9jI/AAAAAAAAPQE/5DU0cL2EpBM/s1600-h/image%25255B19%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTCmJTs5k_Gea8QNn-SUuWnbF2lZ5TMkDBPy5Bx4OlLKLD1jRgDF3jE5LvTXkJmeGkfw5Yp7-ShZrQIy37DMtRzt87e_-lua9Tcyce0QMAEQwd471aFuvWzUSW-qXPUgbfy5siEnbUbGCH/?imgmax=800" width="223" height="48"></a><br>したがって、これを解決するには、次のように SELECT ステートメントの結果を “テーブル” のように扱って、内部結合するようにします。<br><a href="http://lh3.ggpht.com/-BKkseMclVAc/UVwwp4PPYJI/AAAAAAAAPQU/E7lGqx89laM/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-4mmMTt_Icko/UVwww2wfDhI/AAAAAAAAPQc/R_979zChIjs/image_thumb%25255B11%25255D.png?imgmax=800" width="447" height="174"></a><br>部門番号ごとの最大給与の結果に対して、「b」という名前を付けてテーブルのように扱っているところがポイントです。これを図解すると、次のようになります。<br><a href="http://lh6.ggpht.com/-pSN8ZMokWK8/UVww2KSdX-I/AAAAAAAAPQk/YwvkKAKUuA8/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-AbDXxcKvP7M/UVww9TEsJUI/AAAAAAAAPQs/FS3Xt2aX4oA/image_thumb%25255B13%25255D.png?imgmax=800" width="453" height="167"></a><br>部門番号ごとの最大給与の結果「b」と「社員」テーブルを結合することで、部門ごとの最大給与を取得している社員を抽出できるようになります。このように SELECT ステートメントの結果をテーブルのように利用する方法は、「インライン ビュー」とも呼ばれています。</li></ol> <h5 id="id002">UNION ALL</h5> <p>SQL では、UNION ALL を利用すると、複数の SELECT ステートメントの検索結果を連結でき<br>るようになります。構文は、次のとおりです。</p> <p><a href="http://lh3.ggpht.com/-41wRUGWQhnQ/UVwxCQA17dI/AAAAAAAAPQ0/EnVk_5JBWr8/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ZUMEZ-89hss/UVwxJbUI_jI/AAAAAAAAPQ8/f8DA0SZOh8M/image_thumb%25255B14%25255D.png?imgmax=800" width="159" height="132"></a></p> <h6>Let's Try</h6> <ol> <li>社員番号 1~3 の社員を、それぞれ別々の SELECT ステートメントで取得し、結果を<br>UNION ALL で連結してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM 社員 WHERE 社員番号 = 1<br> UNION ALL<br>SELECT * FROM 社員 WHERE 社員番号 = 2<br> UNION ALL<br>SELECT * FROM 社員 WHERE 社員番号 = 3</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-oGytzr-nO9M/UVwxORA0GOI/AAAAAAAAPRE/L7UmvtloAac/s1600-h/image%25255B34%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-rYP95dPpDEo/UVwxVQS5ocI/AAAAAAAAPRM/M_jYqBn7FpA/image_thumb%25255B16%25255D.png?imgmax=800" width="374" height="206"></a><br>この結果は、次の SELECT ステートメントを実行した場合と同じになります。<br><a href="http://lh3.ggpht.com/-YwYkhQdP9n0/UVwxaBuaOhI/AAAAAAAAPRU/ObkGoHBklNM/s1600-h/image%25255B42%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-4PXvw44-M_8/UVwxhQd49QI/AAAAAAAAPRc/S2pw3YCY07c/image_thumb%25255B20%25255D.png?imgmax=800" width="373" height="149"></a><br>このように、UNION ALL 句を使用すると、2 つ以上の SELECT ステートメントの結果を連<br>結して 1 つの結果にすることができます。 <p><font color="#9b00d3">Note: UNION ALL と UNION<br></font>UNION ALL 句は、単純に SELECT ステートメントの結果を連結するだけなので、結果に重複行があった場合にもそのまま連結します。これに対して、結果に重複があった場合に、重複行を排除して連結することができる「UNION 句」(ALL が付かないもの)があります。<br>たとえば、UNION ALL 句を利用した場合の結果は、次のとおりです。<br><a href="http://lh3.ggpht.com/-mmQrNl_6bTA/UVwxmWjhXhI/AAAAAAAAPRk/eV2pm2EKesk/s1600-h/image%25255B46%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-XhgPxLn-XF8/UVwxtQFVFxI/AAAAAAAAPRs/tivib1YMsk8/image_thumb%25255B22%25255D.png?imgmax=800" width="370" height="158"></a><br>これに対して、UNION 句を利用した場合の結果は、次のとおりです。<br><a href="http://lh3.ggpht.com/-e3TT81i1a-Q/UVwxydxhO1I/AAAAAAAAPR0/TK8El9bxAPA/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-G2n-fpkFsT4/UVwx5ZxmC5I/AAAAAAAAPR8/6GPnoivyWx4/image_thumb%25255B24%25255D.png?imgmax=800" width="370" height="189"></a><br>このように、UNION 句では、重複行を削除した結果を取得することができます(その分、UNION ALL よりも、UNION の方がパフォーマンス上のオーバーヘッドがあります)。</p> <p><font color="#9b00d3">Note: UNION ALL はデータ型と列数が同じであれば連結可能</font><br>UNION ALL と UNION では、データ型と列数が同じであれば、列名が違っても、全く関係ないテーブル同士でも、結果を連結することが可能です。したがって、次の SQL はエラーにはなりません。<br><a href="http://lh4.ggpht.com/-Syy6aHdWK7I/UVwx-dL6csI/AAAAAAAAPSE/8BUnPhU_vtk/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-AulCCdIbzBM/UVwyFrT37cI/AAAAAAAAPSM/XpgAOOgqnOo/image_thumb%25255B26%25255D.png?imgmax=800" width="232" height="331"></a><br></p> <p>社員番号と部門場号は、全く関係のないデータですが、UNION ALL や UNION は、単純な結果の ”連結” を行っているだけなので、1 つの結果として取得することができます。</p></li></ol> <h5 id="id003">CASE 式</h5> <p>SQL では、CASE 式を利用すると、Visual Basic での「Select Case」や C 言語系と Java の<br>「Switch」と同じような複数の条件分岐を行えるようになります。構文は、次のとおりです。</p> <p><a href="http://lh4.ggpht.com/-mZW9IZwIvnY/UVwyKYmu1cI/AAAAAAAAPSU/H_cR5128qcY/s1600-h/image%25255B57%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-N69EMEOfjpM/UVwyRiF0MGI/AAAAAAAAPSc/P8m9Gr9d5nU/image_thumb%25255B27%25255D.png?imgmax=800" width="244" height="150"></a></p> <h6>Let's Try</h6> <ol> <li>次のように入力して、部門番号の値をもとに、部門名を表示するようにしてみましょう。部門<br>番号が "10" の場合は "総務部"、部門番号が "20" の場合は "営業部”、これ以外の場合は "その他”と表示してみます。<br><a href="http://lh5.ggpht.com/-6FCymdQ6jhU/UVwyWsui2RI/AAAAAAAAPSk/W5TQ5IWg6g0/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgv64PUfQ4x4TAkwglxT8KQ0zjld3KE8IqgZRm6SBClkZbW9Pl9CKuYMZ-Y4tSTPPkuNQFrFiDK6XzWAj407l-LFYKsv9E42C5EYIGaOoktV4AJTkhaxfneICmlm5GYfuAOwNohd1m0Vm-v/?imgmax=800" width="298" height="373"></a><br>このように、CASE 式を使用すると、列の値をもとに、別の値を表示できるようになります。<br> <li>次に、入社日の値をもとに、"2000 年以降に入社した社員" と "1990 年以降に入社した社<br>員"、"1980年以降に入社した社員"、"それ以外の社員" と表示されるようにしてみましょう。<br><a href="http://lh5.ggpht.com/-iMc5YqOeqOE/UVwyjJ-YWSI/AAAAAAAAPS0/w7Q9hESjCYk/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-3IGVnT4EPtM/UVwyqX32VMI/AAAAAAAAPS8/wHHqXwR57NE/image_thumb%25255B31%25255D.png?imgmax=800" width="302" height="319"></a> <p><font color="#9b00d3">Note: CASE 式は選択リスト以外でも利用可能</font><br>CASE 式は、単独では使用することができませんが、ステートメント内であれば、ORDER BY 句や GROUP BY 句、<br>WHERE 句、UPDATE の SET 句などで利用することも可能です。</p></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-56005581660448931492013-04-02T23:25:00.001+09:002013-04-02T23:25:28.942+09:00◆集計関数とテーブルの結合<ul> <li><a href="#id001">集計関数: SUM、AVG、MAX、MIN、COUNT</a> <li><a href="#id002">グループ化: GROUP BY 句</a> <li><a href="#id003">複数テーブルの結合</a> <li><a href="#id004">内部結合: INNER JOIN</a> <li><a href="#id005">GROUP BY 句と結合</a> <li><a href="#id006">外部結合: OUTER JOIN</a> </li></ul> <h5 id="id001">集計関数: SUM、AVG、MAX、MIN、COUNT</h5> <h6>集計関数</h6> <p>SQL では、集計関数を利用して、合計や平均の計算など、集計値を計算することができます。集<br>計関数には、主に次の 5 つの関数があります。</p> <p><a href="http://lh3.ggpht.com/-7r9ajFclx-M/UVrjuE2klAI/AAAAAAAAPGM/VbFSiXJLOjg/s1600-h/image%25255B2%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-qb0Z2pmP2K0/UVrj1RgEpHI/AAAAAAAAPGU/-VRLQrtxHqg/image_thumb.png?imgmax=800" width="244" height="132"></a></p> <ol> <li>まずは、SUM 関数を利用して、「社員」テーブルから、“社員全員の給与の合計金額” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT SUM(給与) FROM 社員</td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggONGvWvxTpuzChyphenhyphen73sx9ovxfQjIut6f5K2GI2V0TEjHZplzu92olcag6Q-7QnLfy6yKnf3ty8upnOVTAo4o68DfGDuqZXxXbJEEVDKhfRP1wllv13lZW1a0iOGuSlABQmKP30eMXdPDST/s1600-h/image%25255B5%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ahlDCqq2owo/UVrkBfHD4JI/AAAAAAAAPGk/osEsacZawWE/image_thumb%25255B1%25255D.png?imgmax=800" width="244" height="96"></a><br>このように、SUM 関数のカッコ内へ列名を記述すると、その列の合計値を計算できるように<br>なります。 <li>次に、AVG 関数を利用して、“給与の平均金額” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT AVG(給与) FROM 社員</td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-Pp6HsKI76YA/UVrkGUCidsI/AAAAAAAAPGs/_OyFb3Deb_U/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-mTUMTUdQ3iw/UVrkNZtVt6I/AAAAAAAAPG0/_AYHbZtF4eU/image_thumb%25255B2%25255D.png?imgmax=800" width="244" height="97"></a><br>このように、AVG 関数を利用すると、平均値を取得することができます。 <p><font color="#9b00d3">Note: 集計関数では NULL 値が無視される<br></font>SUM と AVG、MAX、MIN 関数では、NULL 値が無視されます。AVG 関数の実行結果を、もう一度確認してみます。<br><a href="http://lh6.ggpht.com/-N5lIiOPumHY/UVrkSWBSLuI/AAAAAAAAPG8/FZJ4EvreThI/s1600-h/image%25255B11%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-BMGkyhYSW3Q/UVrkZQhIVKI/AAAAAAAAPHE/eRrn-vbWfgU/image_thumb%25255B3%25255D.png?imgmax=800" width="244" height="92"></a><br>「社員」テーブルの内容は、次のようになっています。<br><a href="http://lh6.ggpht.com/-1tbZ2jFR5kE/UVrkedcWh_I/AAAAAAAAPHM/XeJEAXVX6PA/s1600-h/image%25255B15%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-CQGPWoKdzf0/UVrklhAWWUI/AAAAAAAAPHU/5uKJ_coz5Uw/image_thumb%25255B5%25255D.png?imgmax=800" width="404" height="127"></a><br>このように、AVG 関数の結果は、NULL 値が無視されて計算されていることが分かります。もし、社員数で給与の平均を計算したい場合には、NULL 値のデータの社員も含める必要があるので、数値が入る列には、NULL 値の取り扱いに注意して、NULL 値を許可するかどうかをよく考えて、運用前にきちんとルールを作っておく必要があります。<br>なお、NULL 値があった場合に、それを別の値(0 など)へ変換して集計関数で処理させる方法もあります。これは「ISNULL」という関数を使って、次のように記述することができます。<br><a href="http://lh3.ggpht.com/-22-schNcx-8/UVrkqSejkvI/AAAAAAAAPHc/jqkW0lz6eXg/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-V5Z_ZP_skFo/UVrkxqJgauI/AAAAAAAAPHk/RgqS_VlMcAE/image_thumb%25255B6%25255D.png?imgmax=800" width="244" height="109"></a><br>ISNULL 関数の第 2 引数には、NULL 値だった場合に変換したい値を指定します。ここでは、0 を指定している<br>ので、給与の平均を 41.6666 万円として計算することができています。なお、ISNULL 関数は、Oracle では、<br>「NVL」という関数に相当します。</p> <li>次に、MAX 関数と MIN 関数を利用して、“給与の最大値と最小値” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT MAX(給与), MIN(給与) FROM 社員</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-vRh8MZahtFA/UVrk2lV6wtI/AAAAAAAAPHs/F2amqmk3b58/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-b1GYIYkSVYU/UVrk9_e4GwI/AAAAAAAAPH0/G3gaCDE6Qr4/image_thumb%25255B7%25255D.png?imgmax=800" width="244" height="143"></a> <li>次に、COUNT 関数を利用して、「社員」テーブルの行数をカウントしてみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT COUNT(*) FROM 社員</td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-wDRDb4rIKDw/UVrlCmvcwhI/AAAAAAAAPH8/qF5pp7TDKN8/s1600-h/image%25255B24%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-9JbPW4wgEfc/UVrlJ8pZr_I/AAAAAAAAPIE/MDfLRXNX840/image_thumb%25255B8%25255D.png?imgmax=800" width="218" height="123"></a><br>結果は、「6」が返り、社員テーブルの行数(6 件)を取得できたことを確認できます。 <li>COUNT 関数のカッコ内へ、列名を指定した場合は、NULL 値を除いたデータ件数をカウン<br>トすることもできます。これを「給与」列で試してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT COUNT(給与) FROM 社員</td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-hdPTjitInhA/UVrlOnyLSSI/AAAAAAAAPIM/mu-B0Dvs7ks/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-vkz8L4EcmIM/UVrlV4qD8pI/AAAAAAAAPIU/VdW0qrSFsO8/image_thumb%25255B9%25255D.png?imgmax=800" width="244" height="133"></a><br>「給与」列には NULL 値が 1 件(浅田さん)あるので、この値が無視されて、5 件とカウン<br>トされています。このように、COUNT 関数で、列名を指定した場合には、前述の Note と<br>同様、NULL 値を無視してカウントすることができます。</li></ol> <h5 id="id002">グループ化: GROUP BY 句</h5> <h6>GROUP BY 句でグループ化</h6> <p>集計関数は、GROUP BY 句と一緒に利用することで、グループごとの集計値を計算できるように<br>なります。これは、次のように利用します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 列名1, 列名2, …<br>FROM テーブル名<br>WHERE 検索する行の条件<br>GROUP BY 列名1, 列名2, ・・・[ HAVING 絞込み条件 ]</p></td></tr></tbody></table> <p>GROUP BY 句でグループ化を行う列を指定し、HAVING 句で絞り込みを行う条件を指定すること<br>ができます。</p> <h6>Let's Try</h6> <ol> <li>次のように GROUP BY 句を記述して、“部門ごとの給与の平均値” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 部門番号, AVG(給与) FROM 社員<br>GROUP BY 部門番号</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-VqqLSQM5vwc/UVrlahHYpyI/AAAAAAAAPIc/x3tX5gS8aYE/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-z8lwappg1rk/UVrlhyxlobI/AAAAAAAAPIk/KSUmkSynrPw/image_thumb%25255B10%25255D.png?imgmax=800" width="244" height="131"></a><br>部門番号 ”20” は、給与が NULL 値の「浅田 あさみ」さんしかいないので、結果が NULL 値になっています。 <li>そこで、次のように、部門番号 ”20” の社員を 2 人追加してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>INSERT INTO 社員 VALUES (7, '大久保 光', 250000, '2006/04/01', 20)<br>INSERT INTO 社員 VALUES (8, '小笠原 翔', 300000, '2006/04/01', 20)</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-maD-rHbJEDQ/UVrlmnDGcoI/AAAAAAAAPIs/uI60gKgxr3w/s1600-h/image%25255B33%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-mS5RLtS3WCo/UVrlt3ptLFI/AAAAAAAAPI0/UhXx9reyWX0/image_thumb%25255B11%25255D.png?imgmax=800" width="244" height="124"></a> <li>社員データを追加後に、もう一度 “部門ごとの給与の平均値” を取得してみましょう。<br><a href="http://lh4.ggpht.com/-nOFWoV5tvh8/UVrlyxpmnKI/AAAAAAAAPI8/VZEC9lRt9PQ/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-h8L8Y-hp61Y/UVrl6OTGVvI/AAAAAAAAPJE/GlRkCfW_f2M/image_thumb%25255B12%25255D.png?imgmax=800" width="244" height="134"></a><br>今度は、部門番号 ”20” は、大久保さん(25 万円)と小笠原さん(30 万円)の平均なので、<br>27.5 万円と計算されたことを確認できます(NULL 値の浅田さんのデータは無視されていま<br>す)。 <li>次に、COUNT 関数を利用して、“部門ごとの社員数” を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 部門番号, COUNT(*) FROM 社員<br>GROUP BY 部門番号</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-0FAi5QnUkeY/UVrl-2zmsdI/AAAAAAAAPJM/KzliQBUn4fY/s1600-h/image%25255B39%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-H25SlkuP7YU/UVrmF1wrGmI/AAAAAAAAPJU/SqnwhQKMm0A/image_thumb%25255B13%25255D.png?imgmax=800" width="244" height="135"></a><br>部門番号 ”10” は 5 人、”20” は 3 人という結果を取得できます。このように、GROUP BY<br>句は、COUNT 関数と一緒に利用することもできます。 <li>次のように HAVING 句を利用して、“社員数が 5 人以上の部門番号” を検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 部門番号, COUNT(*) FROM 社員<br>GROUP BY 部門番号 HAVING COUNT(*) >= 5</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-rP1Mva0vcb4/UVrmKtfjNbI/AAAAAAAAPJc/DOeJMrfkQmQ/s1600-h/image%25255B42%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-zNKvsIdPquw/UVrmRx12UoI/AAAAAAAAPJk/14n2KGFcUEA/image_thumb%25255B14%25255D.png?imgmax=800" width="244" height="136"></a><br>このように、HAVING 句を利用すると、集計結果に対して絞り込みが行えるので便利です。 <p><font color="#9b00d3">Note: WHERE 句と HAVING 句の絞り込みの違い<br></font>WHERE 句と HAVING 句は、取得するデータの絞り込みができるという点では同じですが、WHERE 句が、集計<br>演算をする前に絞り込みを行うのに対して、HAVING 句では、集計を行った結果に対して絞り込みを行うので、<br>取得結果の意味が大きく異なります。集計の前か後か、目的に応じて使い分けるようにしましょう。</p></li></ol> <h5 id="id003">複数テーブルの結合</h5> <h6>複数テーブルの結合</h6> <p>SQL では、1 つのテーブルからだけではなく、複数のテーブルを結合して、結果を取得すること<br>ができます。<br>たとえば、今まで使用してきた「社員」テーブルでは、社員の「部門番号」を確認することはでき<br>ても、「部門名」を確認することはできませんでした。しかし、「部門」テーブルと結合することで、<br>社員が所属する「部門名」を取得できるようになります。</p> <h6>リレーションシップ</h6> <p>「社員」テーブルと「部門」テーブルには、次のような関係があります。<br></p> <p><a href="http://lh6.ggpht.com/-_IB2ZCMnTwE/UVrmW4HIxmI/AAAAAAAAPJs/7V9h4s_mFY4/s1600-h/image%25255B46%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-C8Uxq1l_yGw/UVrmerdBsWI/AAAAAAAAPJ0/nCVlTeUfH48/image_thumb%25255B16%25255D.png?imgmax=800" width="641" height="264"></a></p> <p>「社員」テーブルの「部門番号」から、「部門」テーブルの「部門番号」を参照することで、部門<br>に関する情報(部門名)を取得できるようになります。このようなテーブル同士の関係は、リレー<br>ションシップと呼ばれます。<br>また、「部門番号」のように、リレーションシップのある列のことを結合キー列といいます。</p> <h6>結合の種類</h6> <p>テーブルの結合には、内部結合(Inner Join)と外部結合(Outer Join)、自己結合(Self Join)<br>の 3 つの種類があります。以降では、これらについて説明します。</p> <h5 id="id004">内部結合: INNER JOIN</h5> <p>内部結合は、リレーションシップをたどって関連情報を取得できる結合です。構文は、次のとおり<br>です。</p> <p><a href="http://lh6.ggpht.com/-LcrkfSBHJM0/UVrmjo65JaI/AAAAAAAAPJ8/VPS3MBroqho/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-A8rnRb2MdnM/UVrmqgM93FI/AAAAAAAAPKE/gRKGW9tGolo/image_thumb%25255B18%25255D.png?imgmax=800" width="450" height="89"></a></p> <p>FROM 句のテーブル名の後に、INNERT JOIN を記述して、結合したいテーブルを指定し、ON 句<br>の後に結合キー列を「=」で指定します。INNER JOIN の INNER は、省略することもできます。</p> <h6>Let's Try</h6> <ol> <li>「社員」テーブルと「部門」テーブルを内部結合してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT *<br>FROM 社員 INNER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-6_X471lJRuQ/UVrmvpEzzlI/AAAAAAAAPKM/S2kSMkt5pmQ/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-tCAlYNY5VCs/UVrm2znZBTI/AAAAAAAAPKU/bAZkbzK7DiM/image_thumb%25255B20%25255D.png?imgmax=800" width="401" height="178"></a><br>INNER JOIN で「社員」と「部門」テーブルを指定し、ON 句で「部門番号」列を指定する<br>ことで、社員と部門を結合できています。<br>内部結合では、ON 句で「=」演算子を利用しているように、「社員」テーブルと「部門」テ<br>ーブルで「部門番号」が等しいものを取得しています。したがって、この結合は「等結合」や<br>「等価結合」とも呼ばれています。 <li>次に、社員テーブルと部門テーブルを結合して、「氏名」と「部門名」列のみを取得してみま<br>しょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 氏名, 部門名<br>FROM 社員 INNER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-M9UIRbn3dQc/UVrm79qWJQI/AAAAAAAAPKc/dF1Wi3fOQ_U/s1600-h/image%25255B57%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-KR546xY8Bag/UVrnDK1pR0I/AAAAAAAAPKk/b9K2zmeXuk0/image_thumb%25255B21%25255D.png?imgmax=800" width="145" height="88"></a> <li>次に、上の結果に加えて、"部門番号" を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 氏名, 部門番号, 部門名<br>FROM 社員 INNER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVlyQszL1i_liMwdJyjSzUoOkeB7x7EQym3A0c5c8s1Pj1H0F9a_x1JRGdCVQ3m84a_gSccitB2452-fHiyAn66AtpM2Db2Ucp2M80B1hPtz_vCsLiroR74f-vD5prsMOr-7h9aMFHkfJr/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Ti7i1QjHEjA/UVrnPBfT8TI/AAAAAAAAPK0/i98mK5AcnXo/image_thumb%25255B23%25255D.png?imgmax=800" width="305" height="152"></a><br>この結果は、「列名 ‘部門番号’ があいまいです」とエラーになります。SQL Server にとって<br>は、「部門番号」列が、「社員」テーブルの列なのか「部門」テーブルの列なのかを区別することができないので、このエラーが発生しています。</li></ol> <h6>テーブル名.列名</h6> <p>結合するテーブルに同じ列名がある場合には、「部門.部門番号」のようにテーブル名の後に「.」(ドット)を付けて列名を指定するようにします。したがって、部門番号を取得するには、次<br>のように記述します。</p> <p><a href="http://lh5.ggpht.com/-SX9PvX2Dx3Y/UVrnUNrKg9I/AAAAAAAAPK8/nkkV-hxHbmg/s1600-h/image%25255B64%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-n319hwn4U4w/UVrnbZJhxXI/AAAAAAAAPLE/v5BoC0_7eYM/image_thumb%25255B24%25255D.png?imgmax=800" width="238" height="244"></a></p> <p>今度は、正しく結果を取得することができました。このように、同じ列名がある場合には、「テ<br>ーブル名.列名」と指定しなければなりません。なお、「テーブル名.*」のように、列名のと<br>ころへ「*」を指定した場合は、そのテーブルのすべての列を取得できるようになります。</p> <p><font color="#9b00d3">Note: テーブル名に対する別名<br></font>テーブル名が長い場合には、テーブル名を何度も記述するのが面倒な場合があります。このような場合に、テーブル名に対して別名をつけることができます。これは、列名に対して別名を付けたときと同じように「AS」を利用して、次のように記述することができます。<br><a href="http://lh3.ggpht.com/-trbfH6ym3EA/UVrngTAj8tI/AAAAAAAAPLM/mOq3oFITBYI/s1600-h/image%25255B67%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-9r0cjWO6cLs/UVrnnnspjfI/AAAAAAAAPLU/-KrdkTG7njw/image_thumb%25255B25%25255D.png?imgmax=800" width="244" height="53"></a></p> <p>これは、「社員」テーブルに対して「s」、部門に対して「b」という別名をつけています。また、「AS」は省略してもよいので、次のように記述することもできます。<br><a href="http://lh3.ggpht.com/-wNWxSNKob9s/UVrnsY3Xx7I/AAAAAAAAPLc/cJIEhwWFwAI/s1600-h/image%25255B70%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbPgkZ_cGW42vYNu9-WONMGdyFnDkkHbKqOoOthxYNi9cCfo2YHwPFEy74evGNiad_ktrv-cQleTqIhNpqs8UtZu0JDMGdXuZ75GtoesAnZsWvJ-CXfMp_E4lpm62oKtlMRaiGsyriDzEW/?imgmax=800" width="244" height="59"></a></p> <p>なお、Oracle では、AS を利用したテーブル名に対する別名がサポートされていないので、上記のように AS を省略して別名を記述しなければなりません。</p> <p>■ 別名利用時の注意点<br>テーブル名に別名を利用している場合には、ON 句の中や列名を列挙するときにも、別名を利用しなければならないことに注意する必要があります。</p> <p><font color="#9b00d3">Note: クエリ デザイナーで結合演算を GUI 生成</font><br>Management Studio では、「クエリ デザイナー」という機能を利用すると、GUI 操作だけで結合演算を行うSELECT ステートメントを生成させることもできます。「クエリ デザイナー」を起動するには、次のように「クエリ エディター」上の任意の場所を右クリックして、[エディターでクエリをデザイン]をクリックします。<br><a href="http://lh6.ggpht.com/-kkm2aTV6yt4/UVrn5M4fnPI/AAAAAAAAPLs/iVJNrdwkFX8/s1600-h/image%25255B75%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-E5Xzzvf2K2s/UVroBNelfQI/AAAAAAAAPL0/nctAlf-J7f0/image_thumb%25255B29%25255D.png?imgmax=800" width="636" height="385"></a></p> <p>これにより、[テーブルの追加]ダイアログが表示されるので、[Ctrl]キーを押しながら、「社員」と「部門」を選択し、[追加]ボタンをクリックします。追加後、[閉じる]ボタンをクリックすると「クエリ デザイナー」画面が表示されます。ここで、取得したい列をチェックすれば、それらが選択リストに追加された SELECT ステートメントが自動生成されるようになります。</p> <h5 id="id005">GROUP BY 句と結合</h5> <p>続いて、GROUP BY 句と結合を組み合わせて、より実践的な集計演算を行ってみましょう。</p> <h6>Let's Try</h6> <ol> <li>まずは、「部門名」ごとの所属人数を取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 部門名, COUNT(*)<br>FROM 社員 INNER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号<br>GROUP BY 部門名</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-YdiTUJOOGf8/UVroGPExjQI/AAAAAAAAPL8/Mxvpmdo7LQ8/s1600-h/image%25255B78%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Hfn3N9G-IF8/UVroNQj3F_I/AAAAAAAAPME/xtprnfQnuSU/image_thumb%25255B30%25255D.png?imgmax=800" width="244" height="170"></a><br>社員テーブルだけでは、部門名を取得することができませんでしたが、部門テーブルと結合することで、部門名を取得し、部門名ごとの集計ができるようになっています。 <li>次に、上の結果に加えて、部門番号も取得してみましょう。<br><a href="http://lh4.ggpht.com/-yPaP8hb582w/UVroSOGZl7I/AAAAAAAAPMM/5kE5IUXwMcw/s1600-h/image%25255B82%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-SvUsv6XxGe0/UVroZSODTrI/AAAAAAAAPMU/mduCV_cnVB0/image_thumb%25255B32%25255D.png?imgmax=800" width="486" height="213"></a><br>結果はエラーになります。GROUP BY 句では、選択リストへ指定できる列が、GROUP BY へ指定した列のみという制約があります。したがって、部門番号を取得したい場合には、次のように GROUP BY 句にも、部門番号を記述しなければなりません。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 部門.部門番号, 部門名, COUNT(*)<br>FROM 社員 INNER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号<br>GROUP BY 部門.部門番号, 部門名</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-Vj3Z5gJhcrU/UVroeZxlX3I/AAAAAAAAPMc/406Vs1eGiMw/s1600-h/image%25255B86%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-MlA8IG4bTlQ/UVrolrhjjWI/AAAAAAAAPMk/LP_5QNx60Dk/image_thumb%25255B34%25255D.png?imgmax=800" width="336" height="218"></a><br>今度は、部門番号を正しく取得することができました。このように GROUP BY 句で複数の<br>列を取得したい場合には、その列も GROUP BY 句へ含めるようにしなければなりません。<br><br><font color="#9b00d3">Note: GROUP BY 句で複数列を指定するときの冗長回避<br></font>GROUP BY 句で複数列を指定する場合は、記述が冗長になるので、これを回避する方法として、MAX 関数または MIN 関数を利用する方法があります。たとえば、上記のクエリを MAX 関数を利用すると、次のように記述することができます。<br><a href="http://lh5.ggpht.com/-svJ7YHmCuvA/UVroqmDgkcI/AAAAAAAAPMs/-xN40MuF02g/s1600-h/image%25255B90%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-3hBvh456oVM/UVroxpyz_vI/AAAAAAAAPM0/HzZgjAMAkqQ/image_thumb%25255B36%25255D.png?imgmax=800" width="336" height="196"></a><br>GROUP BY 句では、「部門番号」列のみを取得し、MAX 関数で「部門名」を指定すれば、GROUP BY 句へ「部門名」を記述しなくて済みます。「部門名」のように「部門番号」列に従属した列(部門テーブル内の列)であれば、MAX 関数や MIN 関数を使っても、値は1つしかなく、最大値も最小値もその値しかないので、このような利用方法で結果を取得することができます。</li></ol> <h5 id="id006">外部結合: OUTER JOIN</h5> <p>内部結合では、結合するテーブル同士で、対応するデータ(等しいデータ)のみを取得するのに対<br>して、外部結合では、対応するデータがなくても(結合するテーブルの片方にしか存在しないデー<br>タの場合にも)、値を取得できるようになります。構文は、次のとおりです。</p> <p><a href="http://lh5.ggpht.com/-jLPLl-4hsqk/UVro2vXU4JI/AAAAAAAAPM8/wUU0SnnHzD0/s1600-h/image%25255B94%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Lkbz1_WmXP4/UVro9py973I/AAAAAAAAPNE/AeZ10kiD_Cc/image_thumb%25255B38%25255D.png?imgmax=800" width="370" height="57"></a></p> <p>内部結合(INNER JOIN)との記述の違いは、LEFT または RIGHT を指定した OUTER JOIN で<br>ある点です。LEFT(左)を指定した OUTER JOIN は、「左外部結合」、RIGHT(右)を指定した<br>OUTER JOIN は、「右外部結合」と呼ばれます。</p> <h6>RIGHT OUTER JOIN(右外部結合)</h6> <ol> <li>まずは、OUTER JOIN の結果が分かりやすくなるように、「部門」テーブルへデータを 1 件<br>追加しておきましょう。次のように入力して、部門番号「30」の「情報システム部」を追加<br>します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">INSERT INTO 部門 VALUES ( 30, '情報システム部' )</td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-SN6orHEzfX4/UVrpCof_vfI/AAAAAAAAPNM/e0-1l7f1zUI/s1600-h/image%25255B98%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-1Pglf7767Ew/UVrpJwuuGNI/AAAAAAAAPNU/O_7iLEExzsI/image_thumb%25255B40%25255D.png?imgmax=800" width="325" height="179"></a> <li>新しく追加した情報システム部には、まだ所属する社員がいません。この状態で、次のように「RIGHT OUTER JOIN」(右外部結合)を利用して、「社員」テーブルと「部門」テーブル<br>を結合してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 社員.*, 部門名<br>FROM 社員 RIGHT OUTER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-HyNdlcpW4EI/UVrpO-8vG0I/AAAAAAAAPNc/ibdSpgo1olY/s1600-h/image%25255B103%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-o7MVcmKZWjs/UVrpWZfbfmI/AAAAAAAAPNk/qPBDwB9JPKs/image_thumb%25255B43%25255D.png?imgmax=800" width="636" height="320"></a>INNER JOIN(内部結合)では、部門番号が等しいデータのみを取得するので、所属社員のいない「情報システム部」を取得することはできません。これに対して、OUTER JOIN(外部<br>結合)では、対応するデータがない場合(結合するテーブルの片方にしか存在しないデータの場合)にも、結果を取得できるようになります。この例では、JOIN の右側(RIGHT)に指定<br>した「部門」テーブルにしか存在しないデータの「情報システム部」を取得することができて<br>います。また、このデータは片方にしか存在しないので、残りのデータは NULL 値で表示さ<br>れています。</li></ol> <h6>LEFT OUTER JOIN(左外部結合)</h6> <ol> <li>次に、「RIGHT OUTER JOIN」を「LEFT OUTER JOIN」へ変更して、社員テーブルと部門<br>テーブルを結合してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 社員.*, 部門名<br>FROM 社員 LEFT OUTER JOIN 部門<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoGRx_3LTP7Yru2KP1pJQEQGNEtMBG0f4IbSHTdYHey8MGuKXIzioyNoeY92bLi2ibGqpelGcML8bDtrs7nE8M_qRRbQjUb8vAnszHeFxEIJeAzFK0n8-58tpt2jTArNIujsGzp_zmCBkF/s1600-h/image%25255B107%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-n5DblHrZBbM/UVrpiW5EnhI/AAAAAAAAPN0/mtke_nSKGe0/image_thumb%25255B45%25255D.png?imgmax=800" width="366" height="242"></a><br>結果は、INNER JOIN(内部結合) の場合と同じになります。LEFT と指定した場合は、JOINの左側(LEFT)に指定した「社員」テーブルにしか存在しないデータを取得できるようになりますが、社員テーブルにしか存在しないデータはないので、INNER JOIN と同じ結果になっています。 <li>次に、結合するテーブルの左と右を入れ替えてみましょう。「社員」テーブルを右へ、「部門」<br>テーブルを左へ指定して結合してみます。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 社員.*, 部門名<br>FROM 部門 LEFT OUTER JOIN 社員<br>ON 社員.部門番号 = 部門.部門番号</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-hqDk0jrcxTA/UVrpncyLMBI/AAAAAAAAPN8/6AB5G4iM4ko/s1600-h/image%25255B112%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-EAFoZ6CYyq4/UVrpup-Xb6I/AAAAAAAAPOE/EYpGF_GoOx0/image_thumb%25255B48%25255D.png?imgmax=800" width="512" height="332"></a><br>今度は、JOIN の左側(LEFT) が「部門」テーブルになるので、「情報システム部」を取得<br>できたことを確認できます。<br>このように RIGHT と LEFT の違いは、JOIN の左と右のどちらにテーブルを記述するかど<br>うかだけです。<br><br><font color="#9b00d3">Note: 自己結合<br></font>結合には、自分自身のテーブルと結合する「自己結合」もあります。たとえば、次のようなemp(社員)テーブルがあったとします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioHhaxPN8b33NwWAJB96LsdOjiJaI4KAZvUua5bHCv6V4h7miiYxR66k40VRKfF3Pif0V6DaTFx8474PH9WCXh_YVdSvXz4UFkvqcJPQpmSHuZzN-N0Uzkz-klietY4d4T7FAKX1yes3J5/s1600-h/image%25255B116%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-_86jFzG5Zp0/UVrp63nAxqI/AAAAAAAAPOU/3TjsBOUM5wU/image_thumb%25255B50%25255D.png?imgmax=800" width="331" height="238"></a><br>このテーブルには、上司の社員番号を格納している「上司社員番号」列があります。このような親子関係のあるデータが同じテーブルに格納されている場合は、自己結合が役立ちます。自己結合を利用すれば、次のように上司の氏名を取り出すことができます。<br><a href="http://lh6.ggpht.com/-4cAIxKFIOZ0/UVrp_wuAhJI/AAAAAAAAPOc/CUAqrvorlzg/s1600-h/image%25255B120%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnbFDxVbkV8mV1FiGSRY7kRLZv9RlFSPy8o1Bw62iEk7M-HhOhWe6YKItbnhIkhAyfO-HtfoN-Cwv8Wxr3yOUnrrryI_W0qgF44me41gALYuPSxSH3XQS1fXkVC45PyVyDlgC3q0w6Jc1A/?imgmax=800" width="325" height="217"></a><br> <p>自己結合では、同じテーブル(emp)を INNER JOIN で結合できます。このとき、1 つには別名を付ける必要があるので、ここでは「manager」と付けています。あとは、結合条件を「emp.上司社員番号=manager.社員番号」とすれば、上司の氏名を取得できるようになります。<br>自己結合では、次のように 1 つのテーブルを 2 つのテーブルのように見立てて、結合を行うことができます。<br><a href="http://lh3.ggpht.com/-IwYrZv8SZW8/UVrqMEt4OTI/AAAAAAAAPOs/YHBYmmtUwy8/s1600-h/image%25255B125%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-dZcFZQ_c7GE/UVrqTWOk2bI/AAAAAAAAPO0/GC5HW6iLTn8/image_thumb%25255B55%25255D.png?imgmax=800" width="608" height="159"></a></p></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-16021539825685479802013-04-01T22:41:00.001+09:002013-04-01T22:41:37.480+09:00◆データの追加 / 更新 / 削除<ul> <li><a href="#id001">テーブルの作成: CREATE TABLE ステートメント</a> <li><a href="#id002">データの追加: INSERT ステートメント</a> <li><a href="#id003">データの更新: UPDATE ステートメント</a> <li><a href="#id004">データの削除: DELETE ステートメント</a> </li></ul> <h5 id="id001">テーブルの作成: CREATE TABLE ステートメント</h5> <h6>テーブルの作成</h6> <p>SQL では、CREATE TABLE というステートメントを利用して、テーブルを作成することができ<br>ます。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE TABLE テーブル名<br>( 列名1 データ型[NOT]NULL,<br>列名2 データ型[NOT]NULL, … )</p></td></tr></tbody></table> <h6>データ型</h6> <p>テーブルに作成する列は、それぞれ格納するデータの種類(データ型)を指定する必要があります。データ型には、いろいろな種類がありますが、最も基本となるのは、次の 3 種類です。</p> <p><a href="http://lh5.ggpht.com/-EtkSVw8PcXY/UVmLhALvMBI/AAAAAAAAPBs/h4SzACzndN8/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-G6GSuFOawjQ/UVmLoa5sj7I/AAAAAAAAPB0/LXTZLpxlgyk/image_thumb%25255B1%25255D.png?imgmax=800" width="400" height="103"></a></p> <p>このほかのデータ型については、本自習書シリーズの「Transact-SQL 入門」で詳しく説明して<br>います。</p> <h6>NULL 値を許可するかどうか</h6> <p>テーブルの作成時には、列ごとに NULL(ヌルやナルと読みます)を許可するかどうかを設定す<br>る必要があります。NULL は「不確定な値」という意味で、データを追加するときには、入力する<br>値がない場合など、データの追加時ではなく、後から(データの更新時に)値を入力したい場合に<br>は、NULL 値を許可しておくようにします。NULL 値を許可しない場合は、データを追加するとき<br>に、必ず値を入力しなければなりません。データ型のあとに、"NULL" と記述すると "NULL 値<br>を許可する" ことになり、"NOT NULL" と記述すると、"NULL 値を許可しない" ことになりま<br>す。</p> <h6>Let's Try</h6> <ol> <li>次のように入力して、「sampleDB」データベースの中へ「社員2」という名前のテーブルを<br>作成してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>USE sampleDB<br>CREATE TABLE 社員2<br>( 社員番号 int NOT NULL,<br>氏名 char(50) NOT NULL,<br>給与 int NULL,<br>入社日 date NULL )</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-yV0bDTDNy7M/UVmLtTSSZaI/AAAAAAAAPB8/wMdfBLTG5nA/s1600-h/image%25255B9%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-htLMOkNl09o/UVmL0stKdxI/AAAAAAAAPCE/HtXaczujZwo/image_thumb%25255B3%25255D.png?imgmax=800" width="223" height="174"></a><br><font color="#9b00d3">Note: char(50) とは?</font><br>char データ型の場合は、格納する文字データの最大バイト数を「char(50)」のように指定する必要があります。<br>50 と指定した場合は、50 バイト(全角文字だと 25 文字分)までの文字データを格納することができます。</li></ol> <h5 id="id002">データの追加: INSERT ステートメント</h5> <h6>データの追加</h6> <p>SQL では、INSERT というステートメントを利用して、テーブルへデータを追加することができ<br>ます。構文は、次のとおりです。</p> <p><a href="http://lh6.ggpht.com/-ALdfcu70DPE/UVmL5rt6TMI/AAAAAAAAPCM/lVF-ipbLVQo/s1600-h/image%25255B13%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-prLmUKi8Skg/UVmMA0TjKII/AAAAAAAAPCU/1gFAYA8GrwE/image_thumb%25255B5%25255D.png?imgmax=800" width="313" height="128"></a></p> <p>INSERT INTO に続けてテーブル名を記述して、カッコ内へ列名を列挙し、VALUES に続くカッ<br>コ内へ、追加したい値をカンマで区切って記述します。列名を省略する場合は、テーブル作成時の<br>列を定義した順番に値を記述する必要があります。なお、INTO は省略することも可能です。</p> <h6>Let's Try</h6> <ol> <li>「社員2」テーブルは、「社員番号、氏名、給与、入社日」の順に定義しているので、この順<br>番に沿って、値をカンマで区切って指定してみましょう。<br><a href="http://lh6.ggpht.com/-rORTFGAmgz4/UVmMF2rB2aI/AAAAAAAAPCc/ICb4agtwOZo/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-GRrLyFeEhTA/UVmMNAf290I/AAAAAAAAPCk/e58jGrrr7Mk/image_thumb%25255B7%25255D.png?imgmax=800" width="405" height="151"></a><br>実行後、結果ウィンドウへ「1 行処理されました」と表示されれば、データの追加が成功し<br>ています。<br><br><font color="#9b00d3">Note: データの追加は行単位</font><br>リレーショナル データベースの世界では、データの追加は「行単位」で行い、Excel のように「セル単位」ではありません。したがって、新しい行を追加するには、すべての列の値を指定する必要があります。 <li>続いて、さらに「社員2」テーブルへデータを追加してみましょう。今度は、INSERT ステー<br>トメント内へ列名を記述して、その順番に沿って値を記述してみます。<br><a href="http://lh4.ggpht.com/-aZ8uyLI8la4/UVmMSNCesQI/AAAAAAAAPCs/Eyv7yQxqRJ8/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-uIFiWUHvrNw/UVmMZTH_ufI/AAAAAAAAPC0/JpPEMGkn1Lg/image_thumb%25255B9%25255D.png?imgmax=800" width="398" height="57"></a> <li>NULL 値を追加したい場合には、次のように NULL というキーワードを引用符で囲まずに記<br>述します。<br><a href="http://lh4.ggpht.com/-WuV-FO7eM_M/UVmMeCKaRKI/AAAAAAAAPC8/XoUai5fxsz4/s1600-h/image%25255B25%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-LT5n6lLI0AQ/UVmMlREeKkI/AAAAAAAAPDE/S89-Xh8VOqw/image_thumb%25255B11%25255D.png?imgmax=800" width="396" height="69"></a></li></ol> <h5 id="id003">データの更新: UPDATE ステートメント</h5> <h6>データの更新</h6> <p>SQL では、UPDATE というステートメントを利用して、テーブル内のデータを更新することがで<br>きます。構文は、次のとおりです。</p> <p><a href="http://lh3.ggpht.com/-_6BTFztbddc/UVmMqTAsd2I/AAAAAAAAPDM/EjMI794RhiQ/s1600-h/image%25255B28%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-pVyzfwyJQK0/UVmMxvtUr1I/AAAAAAAAPDU/D0UJje3pbVs/image_thumb%25255B12%25255D.png?imgmax=800" width="244" height="78"></a></p> <p>UPDATE に続けてテーブル名、SET 句へ更新したい列の名前と更新後の値、WHERE 句へ更新したい行を指定する条件を記述します。WHERE 句では、Step 3 で説明したすべての演算子(比較演算子や BETWEEN、IN、LIKE など)を利用することができます。<br>WHERE 句を省略した場合は、すべての行が更新対象になってしまうことに注意する必要があります。</p> <h6>Let's Try</h6> <ol> <li>次のように入力して、「社員2」テーブルの「社員番号」が「2」番の給与を 40 万円へ更新<br>してみましょう。<br><a href="http://lh4.ggpht.com/-S-CxMrn7Ea4/UVmM2k6cHuI/AAAAAAAAPDc/OOeyLMo7fuw/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn-nJANiEf5mfHEEIxPdtGSFlAiPhtPKYgYya6xoKl_cemRdQBFV_JQLW4mvI73KC1FDjXUh_0lGlsq3QyDnHlvJHuP8-LprVJYqTlGNm5b7vNHV3NxLTl8wK_kSY6S5HlIBS4z5TS-4jP/?imgmax=800" width="170" height="81"></a><br>結果ウィンドウへ「1 行処理されました」と表示されれば、更新が成功しています。 <li>SELECT ステートメントを実行して、データが正しく更新されたことを確認しておきましょ<br>う。<br><a href="http://lh5.ggpht.com/-2uMFom6T4zw/UVmNCmEpeTI/AAAAAAAAPDs/RcU1EYVhnHQ/s1600-h/image%25255B34%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-L6Dnl3V-Dhg/UVmNJ9L4LQI/AAAAAAAAPD0/DPmmEXH-4_E/image_thumb%25255B14%25255D.png?imgmax=800" width="243" height="154"></a><br>社員番号が「2」の「小田 良夫」さんの給与が 40 万円へ更新されていることを確認できま<br>す。 <li>次に、WHERE 句を省略してデータを更新してみましょう。<br><a href="http://lh6.ggpht.com/-WC96sOEnuy4/UVmNOoJWRwI/AAAAAAAAPD8/IqidzbVONRA/s1600-h/image%25255B38%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-loRYPL1juZ0/UVmNYSizPsI/AAAAAAAAPEE/5pswOKees1c/image_thumb%25255B16%25255D.png?imgmax=800" width="197" height="161"></a><br>今度は、「3 行処理されました」と表示され、全データ(3 行)が更新対象となってしまった<br>ことが分かります。 <li>SELECT ステートメントを実行して、結果を確認してみましょう。<br><a href="http://lh6.ggpht.com/-DoeUdc0zJ3w/UVmNdZm3amI/AAAAAAAAPEM/EfI24BK9W30/s1600-h/image%25255B41%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-6dMIMJhIHYk/UVmNkg9Z-yI/AAAAAAAAPEU/5m1v02jgZww/image_thumb%25255B17%25255D.png?imgmax=800" width="244" height="128"></a><br>このように、UPDATE ステートメントでは、WHERE 句を省略すると、すべてのデータが更<br>新対象になってしまうことに注意する必要があります。</li></ol> <h5 id="id004">データの削除: DELETE ステートメント</h5> <h6>データの削除</h6> <p>SQL では、DELETE というステートメントを利用して、テーブル内のデータを削除することがで<br>きます。構文は、次のとおりです。</p> <p><a href="http://lh5.ggpht.com/-Kjrg90DBMG4/UVmNpo6th9I/AAAAAAAAPEc/jgGvueDrobs/s1600-h/image%25255B44%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-iK437SDIrrM/UVmNxNwxItI/AAAAAAAAPEk/QOh-qcSEpIQ/image_thumb%25255B18%25255D.png?imgmax=800" width="208" height="64"></a></p> <p>DELETE FROM に続けてテーブル名、WHERE 句へ削除したい行の条件を記述します。WHERE句では、UPDATE ステートメントのときと同様、Step 3 で説明したすべての演算子を利用することができます。<br>また、WHERE 句を省略した場合には、すべての行が削除対象になってしまうことにも注意する必<br>要があります。</p> <h6>Let's Try</h6> <ol> <li>次のように入力して、「社員2」テーブルから「社員番号」が「2」番のデータを削除してみま<br>しょう。<br><a href="http://lh3.ggpht.com/-WbWjrWstCOQ/UVmN13EW8jI/AAAAAAAAPEs/c8OJ5JUmVNs/s1600-h/image%25255B47%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-1iGCwnuQtl4/UVmN9Cwm_yI/AAAAAAAAPE0/ujZa9woXOow/image_thumb%25255B19%25255D.png?imgmax=800" width="162" height="122"></a><br>結果ウィンドウへ「1 行処理されました」と表示されれば、削除が成功しています。 <li>SELECT ステートメントを実行して、データが削除されていることを確認しておきましょう。<br><a href="http://lh5.ggpht.com/-K8K82NA32sw/UVmOCIrvLcI/AAAAAAAAPE8/Y6qr1bBehJg/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-e1hLFFR8Xis/UVmOJRnK9LI/AAAAAAAAPFE/DK1oMjXyGJU/image_thumb%25255B20%25255D.png?imgmax=800" width="155" height="112"></a> <li>次に、WHERE 句を省略して、データを削除してみましょう。<br><a href="http://lh6.ggpht.com/-EvNJlNbfoHg/UVmOOOn0dCI/AAAAAAAAPFM/rDISlexLwMU/s1600-h/image%25255B53%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-2nA3tc7zPpo/UVmOVSQAVJI/AAAAAAAAPFU/ns_29iOtyxU/image_thumb%25255B21%25255D.png?imgmax=800" width="136" height="102"></a><br>今度は、「2 行処理されました」と表示され、全データ(2 行)が削除されてしまったことが<br>分かります。<br>このように、DELETE ステートメントで WHERE 句を省略した場合には、すべてのデータが<br>削除対象となってしまうので、注意する必要があります。<br><br>Note: 高速な全データの削除が行える「TRUNCATE TABLE」<br>テーブル内のすべてのデータを削除したい場合には、DELETE ステートメントで WHERE 句を省略するよりも、<br>TRUNCATE TABLE というステートメントを利用したほうが高速に実行することができます。これは、次のよう<br>に利用します。<br><a href="http://lh4.ggpht.com/-x6Iqz-x_Z4g/UVmOaS6FkmI/AAAAAAAAPFc/idwkoc4wRuQ/s1600-h/image%25255B56%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-caqGr5XRX7s/UVmOhnLOExI/AAAAAAAAPFk/b8txNFEDQFk/image_thumb%25255B22%25255D.png?imgmax=800" width="181" height="86"></a><br>「コマンドは正常に完了しました」と表示されれば、全データの削除が完了しています。高速な理由は、トランザ<br>クション ログへ記録する変更履歴を少なくしているからになりますが、トランザクション ログについては、本自<br>習書シリーズの「バックアップと復元」で詳しく説明しています。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-18003801133934926552013-04-01T21:17:00.001+09:002013-04-01T21:17:17.779+09:00◆WHERE 句で利用できる演算子<ul> <li><a href="#id001">比較演算子: =、<、>、<=、>=、<></a> <li><a href="#id002">論理演算子: AND、OR、NOT</a> <li><a href="#id003">BETWEEN 演算子による範囲検索</a> <li><a href="#id004">IN 演算子による複数値の検索</a> <li><a href="#id005">IS NULL 演算子による NULL 値の検索</a> <li><a href="#id006">LIKE 演算子による文字列検索</a> </li></ul> <h5 id="id001">比較演算子: =、<、>、<=、>=、<></h5> <p>WHERE 句の条件式では、次の 6 つの比較演算子を利用することができます。</p> <p><a href="http://lh6.ggpht.com/-lZ66Il9f2_I/UVl3QLHMWaI/AAAAAAAAO80/NxY5SFhiFYc/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-AYGd7fNd_KI/UVl3XJNIRZI/AAAAAAAAO88/JVEJhmjAFEQ/image_thumb%25255B1%25255D.png?imgmax=800" width="352" height="146"></a></p> <h6>Let's Try</h6> <ol> <li>まずは、「<」を利用して、“給与が 50 万円よりも少ない社員” を検索してみましょう。<br><a href="http://lh6.ggpht.com/-VOKuipO0h20/UVl3cHv0aPI/AAAAAAAAO9E/XmveX_RLcE8/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-I8T9sMMea9o/UVl3jPuCnyI/AAAAAAAAO9M/bJV6A2aYJ3A/image_thumb%25255B3%25255D.png?imgmax=800" width="425" height="160"></a> <li>次に、「>=」を利用して、“給与が 50 万円以上の社員” を検索してみましょう。<br><a href="http://lh3.ggpht.com/-riXqaD5MOWA/UVl3oJCRo5I/AAAAAAAAO9U/jol6ZzNuDJA/s1600-h/image%25255B10%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-TJGf7CLPds8/UVl3vVLepWI/AAAAAAAAO9c/lFtGCqp2d-M/image_thumb%25255B4%25255D.png?imgmax=800" width="244" height="165"></a></li></ol> <h5 id="id002">論理演算子: AND、OR、NOT</h5> <p>WHERE 句では、複数の条件式を利用する場合には、「AND」や「OR」、「NOT」などの論理演算<br>子を利用する必要があります。</p> <p><a href="http://lh3.ggpht.com/-PQC8DfQ748g/UVl30Ak90eI/AAAAAAAAO9k/2Nm0E1aaL-Y/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-RB0w8zgei9s/UVl37ao8LlI/AAAAAAAAO9s/a0tgcbdBjzc/image_thumb%25255B6%25255D.png?imgmax=800" width="388" height="89"></a></p> <h6>Let's Try</h6> <ol> <li>まずは、次のように「AND」を利用して、“給与が 50 万円以上で" かつ "入社日が<br>1997/04/01 以降” の社員を検索してみましょう。<br><a href="http://lh6.ggpht.com/-OAB34xrgH1Y/UVl4ABf8TQI/AAAAAAAAO90/2q9qWOArP6U/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-qWFY1VTavRM/UVl4HSfp4jI/AAAAAAAAO98/RV76Q3q_3js/image_thumb%25255B8%25255D.png?imgmax=800" width="424" height="160"></a><br>両方の条件を満たしているデータのみを取得できたことを確認できます。 <p>次に、「OR」を利用して、“給与が 50 万円以上” または ”入社日が 1997/04/01 以降” の<br>社員を検索してみましょう。<br><a href="http://lh5.ggpht.com/-aabfRBbX9wk/UVl4MT_-qtI/AAAAAAAAO-E/NsuqH-rxu-E/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-CheDsVzjUio/UVl4TQsEQTI/AAAAAAAAO-M/neY15VoiJr8/image_thumb%25255B10%25255D.png?imgmax=800" width="428" height="235"></a><br>今度はどちらかの条件を満たしているデータのみを取得できたことを確認できます。このように、WHERE 句では、「AND」や「OR」を利用して複数の条件を指定できるようになります。</p></li></ol> <p><font color="#9b00d3">Note: AND と OR の優先順位<br></font>AND と OR が両方使われた場合は、どちらが優先されると思いますか? 次の結果を見てみましょう。<br><a href="http://lh5.ggpht.com/-hvNA3Z0VlDY/UVl4YTm-_dI/AAAAAAAAO-U/-v-gcL2DVbY/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-E04zdZCTvVM/UVl4fm5eZ6I/AAAAAAAAO-c/Ffa8JhzPf-Q/image_thumb%25255B12%25255D.png?imgmax=800" width="358" height="216"></a></p> <p>”入社日が 1997/04/01 以降、かつ社員番号が 1 “ の条件(AND)が先に処理され、その後に “または、給与<br>が50 万以上” の条件(OR)が処理されていることを確認できます。このように、AND と OR がある場合は、<br>AND が優先されます。<br>■ OR を優先させたい場合<br>AND と OR がある場合に、OR を優先させたい場合は、次のように、その条件をカッコで囲むようにします。</p> <p><a href="http://lh5.ggpht.com/-Z3SKzO5B5Tg/UVl4kUjWi7I/AAAAAAAAO-k/VdmdHvlJBTY/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-_7ansblVZQc/UVl4rt75SzI/AAAAAAAAO-s/xKzk3wHGLMA/image_thumb%25255B14%25255D.png?imgmax=800" width="375" height="152"></a></p> <p>このように OR を含む条件をカッコで囲むことで、その条件を先に処理できるようになります。また、カッコは、OR だけでなく、先に処理させたい条件を囲むために利用できるので、覚えておくと役立ちます。</p> <h5 id="id003">BETWEEN 演算子による範囲検索</h5> <p>SQL では、BETWEEN 演算子を利用すると、“~以上 ~以下” という条件を簡単に記述できるよ<br>うになります。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 列名1, 列名2, …<br>FROM テーブル名<br>WHERE 列名 BETWEEN 値1 AND 値2</p></td></tr></tbody></table> <h6>Let's Try</h6> <ol> <li>次のように入力して、“給与が 50 万円以上~ 70 万円以下の社員” を検索してみましょう。<br><a href="http://lh6.ggpht.com/-sSS24k4pqlA/UVl4wXTD0OI/AAAAAAAAO-0/9LOQHFmIyNk/s1600-h/image%25255B33%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-RvGrgw1wthI/UVl43txnqMI/AAAAAAAAO-8/aci1xVG8UYI/image_thumb%25255B15%25255D.png?imgmax=800" width="244" height="192"></a><br>給与が 50 万円以上で、かつ 70 万円以下の社員を取得できたことを確認できます。<br>この BETWEEN 演算子と同じ結果は、次のように論理演算子「AND」を利用して、取得す<br>ることもできます。<br><a href="http://lh3.ggpht.com/-Nu4hHUEz45w/UVl48Twg18I/AAAAAAAAO_E/b2xpeo0LCHs/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-EwH3wwvgms0/UVl5DWqTw5I/AAAAAAAAO_M/qHUnvcRjzy4/image_thumb%25255B16%25255D.png?imgmax=800" width="196" height="87"></a></li></ol> <h5 id="id004">IN 演算子による複数値の検索</h5> <p>SQL では、IN 演算子を利用すると、“A または B または C” というように、"複数の値を指定し<br>た条件" を簡単に記述できるようになります。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 列名1, 列名2, …<br>FROM テーブル名<br>WHERE 列名 IN (値1, 値2, 値3, …)</p></td></tr></tbody></table> <h6>Let's Try</h6> <ol> <li>次のように入力して、“給与が 30 万円または 70 万円” の社員を検索してみましょう。<br><a href="http://lh3.ggpht.com/-XqZWyFR-ArM/UVl5IFsZDRI/AAAAAAAAO_U/ajOCmJGpOJg/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Wes5DRcghWo/UVl5PRgw_mI/AAAAAAAAO_c/2J0FdVEMczw/image_thumb%25255B18%25255D.png?imgmax=800" width="415" height="180"></a><br>給与が 30 万円または 70 万円の社員を取得できたことを確認できます。<br>この IN 演算子と同じ結果は、論理演算子「OR」を利用して、次のように取得することもで<br>きます。<br><a href="http://lh4.ggpht.com/-L3BPN4TlOAg/UVl5UBQm8hI/AAAAAAAAO_k/VWw0S7lhovc/s1600-h/image%25255B44%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-JtZ73hO63OQ/UVl5bUq-CyI/AAAAAAAAO_s/2W2OkXxqIdk/image_thumb%25255B20%25255D.png?imgmax=800" width="414" height="183"></a> <li>次に、IN 演算子を利用して、"山田 一郎" さんと "鈴木 太郎" さんを検索してみましょう。<br><a href="http://lh5.ggpht.com/-esUKyRyHvls/UVl5gKqPpFI/AAAAAAAAO_0/aSj_CSFvnzw/s1600-h/image%25255B48%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-qvb4d6TBeTc/UVl5nZSMV7I/AAAAAAAAO_8/aWqkFYdg9BM/image_thumb%25255B22%25255D.png?imgmax=800" width="416" height="172"></a></li></ol> <h5 id="id005">IS NULL 演算子による NULL 値の検索</h5> <p>NULL 値は、Step 4 で説明しますが、”0” でもスペース(空白)でもない特殊な値なので、比較<br>演算子やの「=」や「>」などを利用して検索することはできません。NULL 値を検索したい場合<br>には、「IS NULL」演算子を使用する必要があります。</p> <h6>Let's Try</h6> <ol> <li>次のように入力して、“給与が NULL 値の社員” を検索してみましょう。<br><a href="http://lh5.ggpht.com/-kbjq-i9WnSo/UVl5sKT7G1I/AAAAAAAAPAE/7lxurrFlp8w/s1600-h/image%25255B52%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-1TnayBHDBCM/UVl5zZmysBI/AAAAAAAAPAM/YoVZH1z47GA/image_thumb%25255B24%25255D.png?imgmax=800" width="405" height="159"></a><br>このように、NULL 値を検索したい場合は、IS NULL 演算子を利用するようにします。<br><br><font color="#9b00d3">Note: 複数条件指定時の NULL の検索</font><br>NULL 値は、前述の「IN」演算子の中では利用することができません。「IN」演算子を利用する場合は、別途条件<br>式を記述する必要があります。たとえば、“給与が30 万円または70 万円または NULL” のデータを検索したい場<br>合は、次のように記述しなければなりません。<br><a href="http://lh5.ggpht.com/-OJxn5dxkbEg/UVl54bPuR8I/AAAAAAAAPAU/H_gPLSZ4Ajg/s1600-h/image%25255B56%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-v4JZRWqrt4I/UVl5_iD8X4I/AAAAAAAAPAc/qorzILXpQY0/image_thumb%25255B26%25255D.png?imgmax=800" width="399" height="202"></a></li></ol> <h5 id="id006">LIKE 演算子による文字列検索</h5> <p>SQL では、LIKE 演算子を利用すると、文字データをワイルドカード検索できるようになります。<br>ワイルドカードには、「%」と「_」があり、それぞれ次のように利用できます。</p> <ul> <li> % は、“0 文字以上の任意の文字列” <li> _ は、”任意の1 文字”</li></ul> <h6>Let's Try</h6> <ol> <li>まずは、LIKE 演算子で % を利用して、「氏名」が「田」で始まる社員を検索してみましょ<br>う。<br><a href="http://lh6.ggpht.com/-0-vuWWY3_zE/UVl6Enn248I/AAAAAAAAPAk/KV-Ko8Qe5Y8/s1600-h/image%25255B60%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/--p-qu8cX6ro/UVl6LomssyI/AAAAAAAAPAs/dStjXtnAtXM/image_thumb%25255B28%25255D.png?imgmax=800" width="376" height="142"></a><br>このように、“田%” のように最後に「%」をつければ、先頭が ”田” で始まる社員を検索する<br>ことができます。 <li>次に、氏名に「田」というキーワード(文字)を含んだ社員を検索してみましょう。<br><a href="http://lh3.ggpht.com/-2nmrkD6hDfo/UVl6QrSrMQI/AAAAAAAAPA0/DQqRs4JIGwc/s1600-h/image%25255B64%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-aosgVATVmGk/UVl6YYmWMoI/AAAAAAAAPA8/VnTDSLfprMw/image_thumb%25255B30%25255D.png?imgmax=800" width="372" height="232"></a><br>このように、最後の「%」に加えて、先頭にも「%」を指定すれば、特定のキーワードを含<br>んだ文字データを検索できるようになります。 <li>次に、"氏名に「田」または「郎」を含む社員" を検索してみましょう。<br><a href="http://lh3.ggpht.com/-RbCFQwPAYUY/UVl6dDBxBVI/AAAAAAAAPBE/bhAPH_xDsL8/s1600-h/image%25255B68%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-8tMjnzvLqF8/UVl6khonQlI/AAAAAAAAPBM/uLpab-Tbwy0/image_thumb%25255B32%25255D.png?imgmax=800" width="374" height="282"></a><br>このように、OR 演算子を利用して、複数の LIKE 条件を指定すれば、複数のキーワードを<br>含んだデータを検索できるようになります。<br><br><font color="#9b00d3">Note: キーワード検索はパフォーマンスが悪いことに注意</font><br>先頭に % を指定したキーワード検索は、インデックスを利用したピンポイント検索が行われず、全件検索が実行されてしまうので、非常にパフォーマンスが悪くなります(特にデータ量が多い場合は、非常に低速になります)。<br>また、OR 演算子を利用して、複数のキーワードを指定した場合は、さらに低速になります(2 個のキーワードなら 2 倍、3 個なら 3 倍へと、実行時間が長くなっていきます)。<br>したがって、キーワード検索は、なるべく避けるようにし、複数のキーワード検索もできる限り避けるように、アプリケーションのインターフェースを工夫することをお勧めします。また、それらの検索実行には、実行時間が長くかかることをアプリケーションへ明記することにより、ユーザーに簡単に実行されないような工夫をしておくことも重要です。なお、インデックスについては、本自習書シリーズの「インデックスの基礎とメンテナンス」で詳しく説明していますので、こちらもぜひご覧いただければと思います。<br> <p><font color="#9b00d3">Note: 「%」や「_」が入った文字を LIKE で検索するには</font><br>「果汁100%ジュース」という商品データがある場合に、これを “100%” というキーワードで検索したい場合があるとします。しかし、「%」はワイルドカード文字なので “100%” をそのまま使った場合は、”100 個”や ”1000 台” などの商品データも検索対象になってしまいます。そこで、「%」をワイルドカードとしてではなく、検索文字の一部として利用する方法があります。これは、次のように ESCAPE 句を使用します。<br>SELECT * FROM 商品 WHERE 商品名 LIKE '果汁100\%%' ESCAPE '\'「ESCAPE '\'」と記述すると、”\ マークがあった場合に、次の文字をワイルドカードとはみなさない” とすることができます。したがって、「100\%」と記述すれば「100%」という文字を検索キーワードにすることができます。また、「100\%%」の最後の % は、ワイルドカード文字なので、”100%ジュース” や ”100%オレンジ”などを検索できるようになります。<br>なお、\ マークは、「エスケープ文字」とも呼ばれます(\ マーク以外をエスケープ文字にすることもできますが、一般的には \ マークを利用します)。エスケープ(escape)は「逃げる、避ける」という意味の英単語なので、エスケープ文字は「ワイルドカードになるのを避ける(例外にする)」という意味です。</p></li></ol> <h6>_(任意の 1 文字)の利用</h6> <ol> <li>次に、ワイルド カードの「_」を利用して、"氏名の 2 番目の文字が「田」の社員" を検索し<br>てみましょう。<br><a href="http://lh6.ggpht.com/-vFbSfXNyuhg/UVl6pjiOqxI/AAAAAAAAPBU/51abloE_pIY/s1600-h/image%25255B72%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuG_bUAH7BxWXo-HYl6oA9CkTJk3dMT_-djQZzAeg5PX_4-yVs9uPNa4D9QgDzIKmWAcDMHV9IKwz33lrWZ8RzPX9fG2Ln4Yun5I5j0p2lLuDU2t4R3yIqIaX-ePZIvr5ae0a1dXADEWn_/?imgmax=800" width="325" height="187"></a><br>このように、_ は、任意の1 文字のワイルド カードとして利用することができます。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-56256028642981082712013-03-30T23:08:00.001+09:002013-03-30T23:08:41.033+09:00◆データの検索(SELECT) の基本操作<ul> <li><a href="#id001">事前作業(サンプル スクリプトのダウンロードとセットアップ)</a> <li><a href="#id002">SELECT ステートメントによるデータ検索</a> <li><a href="#id003">データの並べ替え: ORDER BY 句</a> <li><a href="#id004">算術演算子: +、-、*、/</a> <li><a href="#id005">文字列連結演算子: +</a> <li><a href="#id006">重複データの排除: DISTINCT</a> </li></ul> <h5 id="id001">事前作業(サンプル スクリプトのダウンロードとセットアップ)</h5> <p>Windows エクスプローラーを起動して、<a href="http://go.microsoft.com/?linkid=9816465">サンプル スクリプト</a>をダウンロードしたフ<br>ォルダーを展開し、このフォルダー内の「CreateTables.txt」ファイルをダブル クリックし<br>て開きます。ファイルの内容をすべてコピーして、クエリ エディターへ貼り付けます。</p> <p><a href="http://lh4.ggpht.com/-isoZ72r-xVE/UVburyoyQmI/AAAAAAAAO4c/u0tl4VUJNhQ/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-TegxfW7BbS8/UVbuz0g6sgI/AAAAAAAAO4k/e_hFtbyCNec/image_thumb%25255B2%25255D.png?imgmax=800" width="539" height="510"></a></p> <p>貼り付け後、ツールバーの[!実行]ボタンをクリックしてクエリを実行します。これにより、<br>「sampleDB」という名前のデータベースが作成され、その中へ「社員」と「部門」テーブ<br>ルが作成されます。実行後、「社員」テーブルの 6 件のデータと「部門」テーブルの 2 件の<br>データが表示されれば、実行が完了です。</p> <h5 id="id002">SELECT ステートメントによるデータ検索</h5> <p>SQL では、SELECT というステートメントを利用することで、テーブル内のデータを検索できる<br>ようになります。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 列名1, 列名2, … FROM テーブル名 WHERE 検索する行の条件</p></td></tr></tbody></table> <p>SELECT の後には、取得したい列の名前をカンマで区切って指定します。列名へ、アスタリスク「*」を指定した場合は、すべての列という意味になり、テーブル内のすべての列を取得することができます。WHERE 句は省略することも可能で、この場合は、すべての行が取得の対象となります。</p> <h6>Let's Try</h6> <ol> <li>まずは、[スタート]メニューの[すべてのプログラム]から[Microsoft SQL Server 2012]<br>の[SQL Server Management Studio]をクリックして、Management Studio を起動し<br>ます。 <li>[サーバーへの接続]ダイアログが表示されたら、[サーバー名]で SQL Server の名前を入<br>力します。<br>[認証]では、「Windows 認証」を選択して、[接続]ボタンをクリックします。なお、認<br>証については、本自習書シリーズの「ログイン認証とオブジェクト権限」編で詳しく説明して<br>いますので、こちらもぜひご覧いただければと思います。 <li>Management Studio が起動したら、次のようにツールバーの[新しいクエリ]ボタンをク<br>リックして、[クエリ エディター]を開きます。<br><a href="http://lh4.ggpht.com/-pCL6XYXA7CE/UVbu5Ff9h-I/AAAAAAAAO4s/thc-_QICMMo/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-8IlX_Io5J6Q/UVbvAd5gIwI/AAAAAAAAO40/sLI3b75g5mA/image_thumb%25255B4%25255D.png?imgmax=800" width="350" height="134"></a> <li>クエリ エディターでは、次のように入力して、sampleDB データベースへ接続して、「社員」<br>テーブルのデータを参照します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>USE sampleDB SELECT * FROM 社員</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-pkHSz4IS7aw/UVbvFXJz3OI/AAAAAAAAO48/B5vWBIQrvk4/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-sXMQcfLXfJc/UVbvN64dpDI/AAAAAAAAO5E/T6iJ8CXsfdA/image_thumb%25255B6%25255D.png?imgmax=800" width="350" height="399"></a><br>SELECT ステートメントの記述が完了したら、ツールバーの[!実行]ボタンをクリックし<br>て、ステートメントを実行します。これにより、結果ウィンドウがグリッド形式(表形式)へ<br>変わって、「sampleDB」データベース内の「社員」テーブルのすべてのデータを取得するこ<br>とが確認できます。「USE」は、データベースへ接続するための SQL Server 独自のステート<br>メント(Transact-SQL ステートメント)で、SELECT ステートメントで「*」を指定したこ<br>とで、社員テーブルのすべてのデータを取得することができます。<br><br><font color="#9b00d3">Note: SQL ステートメント内の記号は半角で入力<br></font>SQL ステートメントでは、英数字や記号(スペース、カッコ、カンマ、単一引用符)は、すべて半角で入力する必<br>要があります。日本語入力(IME)がオンになっている場合は、デフォルトでは、スペースが全角になるので、半<br>角へ変換し忘れないように注意してください。 <li>次に、社員テーブルの「氏名」列と「給与」列のデータのみを取得してみましょう。特定の列<br>データのみを取得するには、次のように 列名をカンマで区切って指定します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT 氏名, 給与 FROM 社員</td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-F9v4Z0cIrnw/UVbvSl0nFdI/AAAAAAAAO5M/ztiTIAkM6ok/s1600-h/image%25255B15%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-4g-RL0nVOk8/UVbvZxab0DI/AAAAAAAAO5U/_oYX3jJvtg8/image_thumb%25255B7%25255D.png?imgmax=800" width="244" height="172"></a><br>このように、列名をカンマで区切って記述することで、その列のデータのみを取得できるよう<br>になります。 <p><font color="#9b00d3">Note: 実行したいステートメントだけを選択してから[!実行]をクリック</font><br>クエリ エディターでは、ツールバーの[!実行]ボタンをクリックすると、エディター内へ記述したすべてのス<br>テートメントが実行されてしまいます。これを回避するには、実行したいステートメントだけを選択(マウスでド<br>ラッグ&ドロップまたはキーボードの[Shift]キーを押しながら矢印キーで選択)してから[!実行]ボタンを<br>クリックするようにします。</p></li></ol> <h6>行の絞り込み: WHERE 句</h6> <p>ここまで利用した SELECT ステートメントは、WHERE 句を省略していましたが、この場合は、<br>すべての行データを取得します。SELECT ステートメントでは、WHERE 句を利用すると、絞り<br>込み条件を指定して、特定の行のみを取得できるようになります。では、これを試してみましょう。</p> <ol> <li>次のように記述して、「社員番号」が「2」番の社員のみを取得してみます。<br><a href="http://lh5.ggpht.com/-TP0C-sPRvOU/UVbve-1LdsI/AAAAAAAAO5c/SYf5JQb6i3Y/s1600-h/image%25255B19%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-58DAxsdx3eE/UVbvmD7WJ5I/AAAAAAAAO5k/yhZVMVsDJZA/image_thumb%25255B9%25255D.png?imgmax=800" width="469" height="171"></a><br>社員番号が 2 番の「鈴木 太郎」さんのみを取得できたことを確認できます。 <li>次に、「氏名」が「浅田 あさみ」さんのデータのみを取得してみましょう。<br><a href="http://lh3.ggpht.com/-2cQgPNgKTLg/UVbvq7OFGbI/AAAAAAAAO5s/mpLrDqfrh_k/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-4HlSILqo5ho/UVbvyM9lgHI/AAAAAAAAO50/ufKAibMLz6Y/image_thumb%25255B11%25255D.png?imgmax=800" width="467" height="172"></a><br><br><font color="#9b00d3">Note: 文字列と日付データ型の値は単一引用符で囲む<br></font>「氏名」列のデータを「'」(単一引用符)で囲んでいるのは、SQL ステートメントでは「文字列(char)と日付<br>(date)データ型の値は、単一引用符で囲まなければならない」という決まりがあるためです。文字列を利用する<br>場合は、単一引用符を忘れずに付けるようにしましょう。</li></ol> <h5 id="id003">データの並べ替え: ORDER BY 句</h5> <p>SELECT ステートメントでは、ORDER BY 句を利用すると、特定の列を基準にデータを並べ替え<br>て、結果を取得できるようになります。これは、次のように利用します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 列名1, 列名2, …<br>FROM テーブル名<br>OREDER BY 列名[ ASC | DESC ]</p></td></tr></tbody></table> <p>ORDER BY の後には、並べ替えの基準となる列名を指定します。デフォルトでは、昇順(数値の<br>場合は「小から大へ」、英字の場合は「A から Z へ」、日付の場合は「古から新へ」)で並べ替え<br>ることができ、DESC キーワードを利用した場合は、降順で並べ替えることができます。</p> <h6>Let's Try: 昇順に並べ替え</h6> <ol> <li>次のように入力して、「社員」テーブルのデータを "給与の低い順(昇順)" に並べ替えて、<br>表示してみましょう。<br><a href="http://lh6.ggpht.com/-BcqDjnA8S7E/UVbv2_xgl-I/AAAAAAAAO58/7IM5BWs8izU/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-6kVKXUppHVo/UVbv-MGe2HI/AAAAAAAAO6E/CsOTYFlVkw0/image_thumb%25255B13%25255D.png?imgmax=800" width="353" height="210"></a><br>ORDER BY 句で指定した「給与」列を基準にデータを昇順に並べ替えることができたことを<br>確認できます。<br>データを昇順に並べ替える場合には、次のように「ASC」キーワードを利用しても同じ結果を<br>取得することができます。<br><a href="http://lh4.ggpht.com/-1OoM77Etg2U/UVbwDOwJxzI/AAAAAAAAO6M/1Ov_X9Zn7k8/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-L97mGa1zl58/UVbwKXaa7HI/AAAAAAAAO6U/83szca4rLE8/image_thumb%25255B15%25255D.png?imgmax=800" width="353" height="232"></a><br>ASC キーワードを省略した場合と同じ結果を取得できたことを確認できます。 <li>次に、"給与の高い順" に並べ替えてみましょう。降順で並べ替えるには、次のように DESC<br>キーワードを利用します。<br><a href="http://lh4.ggpht.com/-twPr3Thbe8g/UVbwPXPI1sI/AAAAAAAAO6c/ZjnQUI_D9WI/s1600-h/image%25255B35%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-IPxo32GO3LY/UVbwWlpKbEI/AAAAAAAAO6k/XPn-ehXbQsk/image_thumb%25255B17%25255D.png?imgmax=800" width="353" height="222"></a><br>「給与」列の後へ、DESC キーワードを記述したので、給与が高い順(降順)に並べ替える<br>ことができたことを確認できます。<font color="#9b00d3">Note: NULL 値の並べ替え順<br></font>NULL 値については、Step 4 で説明しますが、SQL Server では、NULL 値は最も小さい値として扱われます。<br>なお、Oracle の場合は逆になり、NULL 値が最も大きい値として扱われます(NULLS FIRST オプションを指定<br>して、NULL 値を最初に表示することも可能です)。 <li>次のように入力して、"給与の高い順" に並べ替えて、"同じ給与の社員がいた場合には、社員番号の小さい順" に並べ替えるようにしてみましょう。<br><a href="http://lh5.ggpht.com/-CoeTR-AIwqI/UVbwbQ-xumI/AAAAAAAAO6s/fyA6Hz46Xek/s1600-h/image%25255B39%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-PC9RtvZkqPo/UVbwinl0S-I/AAAAAAAAO60/ky3p1uhc0pE/image_thumb%25255B19%25255D.png?imgmax=800" width="359" height="164"></a><br>このようにカンマで区切って複数の列を指定すれば、1 つ目の列で同じ値だったとしても、2<br>つ目の列で並べ替えることができます。2 つ目の列の並べ替え順は、デフォルトでは昇順です。 <li>次に、"同じ給与の社員がいた場合には、社員番号の大きい順" に並べ替えるようにしてみましょう。<br><a href="http://lh4.ggpht.com/-For8JS9vt7w/UVbwnSas1SI/AAAAAAAAO68/Mv90azxmTWo/s1600-h/image%25255B43%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ZrLtSLzkw34/UVbwulIWpII/AAAAAAAAO7E/wWHMoUZqnes/image_thumb%25255B21%25255D.png?imgmax=800" width="357" height="160"></a><br><font color="#9b00d3">Note: ORDER BY 句を指定しない場合の並べ替え順は保証されない<br></font>SQL の世界では、ORDER BY 句を指定しない場合の並べ替え順は保証されません。どういった形で取得されるか<br>は、そのときの状況によって変化し、社員番号で並べ替えられるかもしれないし、給与で並べ替えられるかもしれ<br>ません。 <p><font color="#9b00d3">Note: ORDER BY 句には数値も指定できる<br></font>ORDER BY 句では、列名の代わりに “数値” を指定して、次のように記述することもできます。<br>SELECT 社員番号, 給与 FROM 社員<br>ORDER BY 2 DESC, 1</p></li></ol> <h5 id="id004">算術演算子: +、-、*、/</h5> <p>SQL では、算術演算子を利用して、数値データに対して演算を行うことができます。算術演算子<br>には、加算の「+」や減算の「-」、乗算の「*」、除算の「/」などがあります。<br></p> <h6>Let's Try</h6> <ol> <li>次のように算術演算子の「*」を利用して、「給与」の値を「3 倍」にした結果を取得してみま<br>しょう。<br><a href="http://lh6.ggpht.com/-Ep38XifR2Og/UVbwzrytyqI/AAAAAAAAO7M/koDzFWmJPCk/s1600-h/image%25255B47%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-IxOI-qFWxJc/UVbw67YYZ2I/AAAAAAAAO7U/jBbNle79JJA/image_thumb%25255B23%25255D.png?imgmax=800" width="349" height="205"></a><br>このように、算術演算子を利用することで、数値データに対して、演算を行えるようになりま<br>す。 <p><font color="#9b00d3">Note: NULL 値に対する演算結果は「NULL」</font><br>NULL 値に対しては、何かしらの演算を行っても、結果は NULL 値になります。<br><font color="#9b00d3">Note: 列に対する別名をつける: AS</font><br>算術演算子を使って取得した列の名前は「(列名なし)」と表示されます。列名は、ORDER BY 句やアプリケーシ<br>ョンからデータを取得する際に必要になるので、このままだと操作がしづらくなります。このような場合のために、<br>結果列に対して、新しい列名をつける機能として「AS」があります。これは、次のように利用します。<br><a href="http://lh3.ggpht.com/-8OliZZqrXpU/UVbw_6A5u0I/AAAAAAAAO7c/7CYN2haDKYg/s1600-h/image%25255B51%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-DAY7porm_dA/UVbxHHu3UGI/AAAAAAAAO7k/LCUvh9ryK7U/image_thumb%25255B25%25255D.png?imgmax=800" width="419" height="205"></a><br>このように、AS を使用すると、結果列に対して新しい列名を付け、ORDER BY 句やアプリケーションから利用<br>できるようになります。<br>また、AS は、既存の列に対して別名を付けたり、AS を省略して、次のように使用することもできます。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxu8iuIFUXmRx6JqAQ4Xc1XHphgQzQVmuq6Ig2mp8Q03UY2FXqNCZSYh5-P5tkhgEvEXakzYyrT8xKwMJ_WwYk3th3P_rHqKbmelV1kIMxEzTJSoIjuDW4gIZi8s7h4lGJdqvvJUpC4khd/s1600-h/image%25255B55%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-LjXHFVWH57o/UVbxTLeND0I/AAAAAAAAO70/v52rRJnIQ4k/image_thumb%25255B27%25255D.png?imgmax=800" width="416" height="184"></a></p></li></ol> <h5 id="id005">文字列連結演算子: +</h5> <p>SQL では、「+」(文字列連結演算子)を利用すると、文字データを連結することができます。た<br>だし、これは SQL Server 独自の演算子で、SQL 規格(他のデータベース)では、「||」(縦線:<br>Vertical Line を2 個)を使用して、文字データを連結します。</p> <h6>Let's Try</h6> <ol> <li>次のように入力して、社員の「氏名」列へ “さん” をつけて、表示してみましょう。<br><a href="http://lh3.ggpht.com/-W8NWHs-Z-e4/UVbxYHxE_uI/AAAAAAAAO78/0jnGjRyYpbk/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-WJOs7FEZ7lM/UVbxfcMj7uI/AAAAAAAAO8E/vfTsoQPbJF4/image_thumb%25255B29%25255D.png?imgmax=800" width="340" height="154"></a><br>このように、「+」を利用すると、文字データを連結できるようになります。<br>表示された結果には、氏名と “さん” の間に余分なスペースがありますが、これは、「氏名」<br>列のデータ型が Char(50) と定義されているためです。Char データ型では、指定したバイ<br>ト数に満たない部分をスペースで埋めます(データ型については、本自習書シリーズの<br>「Transact SQL 入門」で詳しく説明しています)。<br><br><font color="#9b00d3">Note: 余分なスペースを詰める関数<br></font>Char データ型では、指定したバイト数に満たない部分をスペースで埋めると説明しましたが、その余分なスペー<br>スを詰めることができる関数もあります。それが「RTRIM」関数です。この関数を使用すると、次のように余分<br>なスペースを詰められるようになります(RTRIM 関数については、本自習書シリーズの「Transact SQL 入門」<br>で説明しています)。<br><a href="http://lh6.ggpht.com/-tOeApTb3oho/UVbxkX4yA0I/AAAAAAAAO8M/hK2kFVQ-GBQ/s1600-h/image%25255B63%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-9NHxV3DK2pw/UVbxrh4MdvI/AAAAAAAAO8U/BUxxe7wuk3w/image_thumb%25255B31%25255D.png?imgmax=800" width="343" height="159"></a></li></ol> <h5 id="id006">重複データの排除: DISTINCT</h5> <p>SQL では、DISTINCT 句を利用して、重複したデータを取り除くことができます。これは、次の<br>ように利用します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT DISTINCT 列名 FROM テーブル名</td></tr></tbody></table> <p><a href="http://lh6.ggpht.com/-CRmAXDnnOhk/UVbxwmbJ0jI/AAAAAAAAO8c/mra6Ic_v02I/s1600-h/image%25255B68%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-rs2uL7DO1dw/UVbx3raw1HI/AAAAAAAAO8k/sKn1aPeA-40/image_thumb%25255B34%25255D.png?imgmax=800" width="385" height="161"></a></p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-10567439594848239602013-03-30T16:15:00.001+09:002013-03-30T16:15:49.838+09:00◆インデックスの構造と内部動作2<ul> <li><a href="#id001">自動的に作成されるインデックス(主キー制約と UNIQUE 制約)</a> <li><a href="#id002">クラスター化がある場合の非クラスター化インデックスの内部構造</a> <li><a href="#id003">カバリング インデックス(複合インデックス)</a> <li><a href="#id004">付加列インデックス(Include オプション)</a> </li></ul> <h5 id="id001">自動的に作成されるインデックス(主キー制約と UNIQUE 制約)</h5> <p>PRIMARY KEY(主キー)制約または UNIQUE 制約を設定している場合は、自動的にインデックスが作成されます。デフォルトでは、PRIMARY KEY制約の場合には「クラスター化インデックス」、UNIQUE制約の場合には「非クラスター化インデックス」が作成されて、インデックスの名前は制約の名前と同じになります。<br>どちらの制約もデータを一意に保つためのものなので、この列を検索条件に指定した場合は、必ず 1件の検索結果が返ります。インデックスは、大量のデータの中から数件のデータを取り出すときに最も効果を発揮するので、一意な値を持つ列に対して作成されたインデックスは、最も効果があります。また、主キーは検索条件としても頻繁に使用されるものなので、インデックスが自動的に作成されることはうれしいことです。</p> <h6>Let's Try</h6> <ol> <li>クエリ エディターへ次のように入力して、「社員番号」列を主キーとする「社員2」という名前のテーブルを作成します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE TABLE 社員2<br>(社員番号 int PRIMARY KEY, 姓 char(20), 名 char(20))</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-IAK3-DDRz0o/UVZNHHp3ZMI/AAAAAAAAOqs/5FgQDawGLdw/s1600-h/image_thumb%25255B46%25255D%25255B2%25255D.png"><img title="image_thumb[46]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[46]" src="http://lh6.ggpht.com/-IzUqmryRUCw/UVZNORdGQ7I/AAAAAAAAOq0/A5ICpNIwmyc/image_thumb%25255B46%25255D_thumb.png?imgmax=800" width="254" height="82"></a> <li>作成後、オブジェクト エクスプローラーで、「社員2」テーブルの[インデックス]フォルダーを展開して、「PK_社員2_~(クラスター化)」というインデックスが作成されていることを確認してみましょう。<br><a href="http://lh3.ggpht.com/-BkH_GBzHbJA/UVZNTSI_MVI/AAAAAAAAOq8/0pMNqECiXi0/s1600-h/image_thumb%25255B48%25255D%25255B2%25255D.png"><img title="image_thumb[48]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[48]" src="http://lh6.ggpht.com/-9fDBNbPSZfM/UVZNas1tpfI/AAAAAAAAOrE/P83GfoM8j8w/image_thumb%25255B48%25255D_thumb.png?imgmax=800" width="413" height="270"></a><br></li></ol> <h5 id="id002">クラスター化がある場合の非クラスター化インデックスの内部構造</h5> <p>ここでは、クラスター化インデックスと非クラスター化インデックスの両方が存在する場合について説明します。<br>クラスター化インデックスを作成すると、非クラスター化インデックスの構造が変更され、リーフレベルへ格納されるポインターが「行識別子」から「クラスター化インデックスの値」へ変更されます。たとえば、社員テーブルの「社員番号」列へクラスター化インデックスを作成し、「姓」列に、非クラスター化インデックスを作成している場合は、次の図のようなインデックス構造になります。</p> <p><a href="http://lh6.ggpht.com/-7vDQN1lHZuY/UVZNgNtnl_I/AAAAAAAAOrM/IWiXYiS4NTk/s1600-h/image_thumb%25255B51%25255D%25255B2%25255D.png"><img title="image_thumb[51]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[51]" src="http://lh6.ggpht.com/-xZEdfi-OHWU/UVZNn1GsZUI/AAAAAAAAOrU/1u_hQ84jOVg/image_thumb%25255B51%25255D_thumb.png?imgmax=800" width="646" height="390"></a></p> <p>このような構造になるメリットは、「姓」と「社員番号」列のみを取得する検索を非常に高速に取得できるようになる点です。</p> <h6>Let's Try</h6> <ol> <li>まずは、「姓」列へ作成していたクラスター化インデックスを削除します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">DROP INDEX 社員.index_姓</td></tr></tbody></table> <li>次に、「社員番号」列に対してクラスター化インデックスを作成します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE CLUSTERED INDEX index_社員番号 ON 社員(社員番号)</p></td></tr></tbody></table> <li>続いて、「姓」列に対して非クラスター化インデックスを作成します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX index_姓 ON 社員(姓)</p></td></tr></tbody></table> <li>次に、「Aoki」さんの「社員番号」と「姓」列のみを取得するようにデータを検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 社員番号, 姓 FROM 社員 WHERE 姓 = 'Aoki'</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-Q_TFouVcZqo/UVZNs_5b0pI/AAAAAAAAOrc/xJIoqUommDo/s1600-h/image_thumb%25255B53%25255D%25255B2%25255D.png"><img title="image_thumb[53]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[53]" src="http://lh6.ggpht.com/-MYBtHhFSbco/UVZN0OP_oGI/AAAAAAAAOrk/oazG9sktOFM/image_thumb%25255B53%25255D_thumb.png?imgmax=800" width="632" height="185"></a><br>グラフィカル実行プランを表示すると、「姓」列へ作成した非クラスター化インデックス「社員.index_姓」の Index Seek のみが実行されたことを確認できます。RID Lookupアイコンが表示されないことがポイントです。「社員番号」と「姓」列のデータは、非クラスター化インデックスのリーフ レベルへ格納されているので、実際のデータを探すことなく(RID Lookup をすることなく)、インデックスの Seek のみで検索が完了しているのです(検索が高速に実行できています)。 <li>続いて、「社員番号」と「姓」列以外の列として「名」列も取得してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT 社員番号, 姓, 名 FROM 社員 WHERE 姓 = 'Aoki'</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-VxJO71XnWbE/UVZN5R9K_BI/AAAAAAAAOrs/KYjaSt1puXY/s1600-h/image_thumb%25255B56%25255D%25255B2%25255D.png"><img title="image_thumb[56]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[56]" src="http://lh5.ggpht.com/-Xz1C_bx3apo/UVZOAvfM6oI/AAAAAAAAOr0/a-q34A677LA/image_thumb%25255B56%25255D_thumb.png?imgmax=800" width="555" height="307"></a><br><br>今度は、グラフィカル実行プランに「キー参照」アイコンが表示されます。これは、RID Lookup と同様に、実際のデータを参照したという内部動作です。RID Lookup からキー参照へ変わったのは、実際のデータへのポインターがクラスター化インデックスの値(キー)に変更されたためです。「名」列の値は、非クラスター化インデックスのリーフ レベルに格納されていないので、取得するには、実データへアクセスしなければなりません。<br><a href="http://lh4.ggpht.com/-TUzLxeHeHLw/UVZOF1LmbvI/AAAAAAAAOr8/G0K9_BVwpXE/s1600-h/image_thumb%25255B60%25255D%25255B2%25255D.png"><img title="image_thumb[60]" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image_thumb[60]" src="http://lh4.ggpht.com/-g1TJepqZjZg/UVZONIrBpaI/AAAAAAAAOsE/qHWtZcRlkZU/image_thumb%25255B60%25255D_thumb.png?imgmax=800" width="565" height="368"></a></li></ol> <h5 id="id003">カバリング インデックス(複合インデックス)</h5> <p>カバリング インデックス(Covering Index)は、非クラスター化インデックスのリーフレベルへ検索で取得したいデータを格納して、キー参照(または RID Lookup)を行うことなく、Index Seekのみで完了する、つまり検索をインデックスのみでカバーするテクニックです。<br>たとえば、前の Step で試した次の検索は、「名」列のデータを取得するために、「キー参照」が行われていました。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT 社員番号, 姓, 名 FROM 社員 WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-_GTK_ha11pw/UVZOSHgsvHI/AAAAAAAAOsM/MthQWIReN1I/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Cu4ZWyfIZjA/UVZOZatoIxI/AAAAAAAAOsU/XR_Fr5Olez0/image_thumb%25255B2%25255D.png?imgmax=800" width="482" height="241"></a> <p>このような場合に、「姓」と「名」列のカバリング インデックスを作成しておけば、リーフ レベルへ「名」列の値を格納できるようになるので、実際のデータを探す(キー参照をする)ことなく、インデックスのみで検索を完了させることができます。これにより、パフォーマンスを向上させることができます。</p> <p><a href="http://lh6.ggpht.com/-laqlcLo-V9I/UVZOeTH-67I/AAAAAAAAOsc/1UHcVpQS7eg/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-U1mqdhHKRk8/UVZOll0j8LI/AAAAAAAAOsk/Hkz8Fsdunv8/image_thumb%25255B4%25255D.png?imgmax=800" width="488" height="282"></a></p> <h6>カバリング インデックスの作成</h6> <p>カバリング インデックスを作成するには、CREATE INDEX ステートメントを次のように利用します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX インデックス名 ON テーブル名(列1, 列2, …)</p></td></tr></tbody></table><br>列名をカンマで区切って指定することで、それらの列をインデックスのリーフ ページへ含めることができるようになります。</p> <h6>GUI での作成</h6> <p>カバリング インデックスを、オブジェクト エクスプローラーから作成する場合は、次のように操作します。</p> <p><a href="http://lh3.ggpht.com/-XoMcMnuGTDQ/UVZOqQvzuNI/AAAAAAAAOss/GrzrDOTXhWo/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-2dhpJUpmdwQ/UVZOxl1rBUI/AAAAAAAAOs0/TR4T4j3R8Cs/image_thumb%25255B6%25255D.png?imgmax=800" width="438" height="122"></a></p> <p>インデックスを作成する列の選択時に、複数の列を選択することで、カバリング インデックスを作成することができます。</p> <p><a href="http://lh4.ggpht.com/-ECOdqCdT4h0/UVZO2x_2_ZI/AAAAAAAAOs8/2aNDe0p7MEo/s1600-h/image%25255B16%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-fFXnQH6jHgM/UVZO-KDoGyI/AAAAAAAAOtE/EH7DIdbcRGc/image_thumb%25255B8%25255D.png?imgmax=800" width="438" height="207"></a></p> <h6>Let's Try</h6> <p>それでは、カバリング インデックスを作成してみましょう。</p> <ol> <li>まずは、「姓」列へ作成していた非クラスター化インデックスを削除します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">DROP INDEX 社員.index_姓</td></tr></tbody></table> <li>次に、「姓」と「名」列を含めたカバリング インデックスを作成してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX index_姓名 ON 社員(姓, 名)</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-3HGdnQJMUzA/UVZPC-oIFFI/AAAAAAAAOtM/b0MvfosMtCU/s1600-h/image%25255B19%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Q1h-TbbL_KI/UVZPKPYllCI/AAAAAAAAOtU/u7BQGj5_M_M/image_thumb%25255B9%25255D.png?imgmax=800" width="223" height="108"></a> <li>作成が完了したら、姓が「Aoki」さんの「社員番号」と「姓」、「名」列を取得する検索を実行してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT 社員番号, 姓, 名 FROM 社員 WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-s5G7lzfNZhY/UVZPPNGknOI/AAAAAAAAOtc/aFH8fKKfEvQ/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-oYB4JLbPBdI/UVZPWF4edFI/AAAAAAAAOtk/42UivpGoIA0/image_thumb%25255B10%25255D.png?imgmax=800" width="244" height="113"></a></li></ol> <h6>カバリング インデックスの注意</h6> <p>カバリング インデックスを作成するときは、列の順番に注意する必要があります。(姓, 名) と指定する場合と (名, 姓) と指定する場合では、構造が異なります。カバリング インデックスは、</p> <p>一番左へ指定した列でツリー構造が作成されるので、WHERE 句の検索条件で指定される列を、一番左へ指定しておくようにします。<br>2つ目以降の列データは、リーフ レベルへ格納されますが、その分余計にディスク領域を消費することにも注意する必要があります。特に追加した列のデータ サイズが大きい場合には要注意です。また、実際のデータの更新時のインデックス自身を更新するオーバー ヘッドもデータ サイズが増える分だけ大きくなります。</p> <h6>カバリング インデックスの正確な内部構造</h6> <p>カバリング インデックスで 2つ目以降へ指定した列データは、正確には、次の図のように、中間ページとルート ページにも格納されます。また、中間ページとルート ページには、行識別子(RID)またはクラスター化インデックスのキー値も格納されます。</p> <p><a href="http://lh5.ggpht.com/-F9H1bBdvQFg/UVZPbPpyDsI/AAAAAAAAOts/viUoQo5BHC4/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-hFMLwoxnfTg/UVZPio358VI/AAAAAAAAOt0/WsXr7A3wg20/image_thumb%25255B13%25255D.png?imgmax=800" width="576" height="366"></a></p> <p>このように、カバリング インデックスでは、中間ページとルート ページにも 2つ目以降へ指定した列データ格納するので、その列データのサイズが大きい場合には、非常にインデックス サイズが大きくなってしまい、パフォーマンスの低下に繋がるケースがあります。<br>これを解消してくれる機能が、次に説明する「付加列インデックス」(Include オプション)です。付加列インデックスの場合は、次のように中間とルートへ値を格納することなく、リーフのみへ値を格納できるようになります。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgerooQM56f9O4JOK3Iu046mVdsG7jxHKyLc_dthcM2pqIWZIm5jU6vbgJTaVWT1z2LdJwaQw-Kbwn4zwR83pERnS71oQKOGeNaeH9e6HSKGZJIWhQ-2BtHMLeKxNpb7FaoG7O1UEpwnzjc/s1600-h/image%25255B32%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-H-vuimFyNaA/UVZPvG5UNNI/AAAAAAAAOuE/3jV1dUvwcZ4/image_thumb%25255B16%25255D.png?imgmax=800" width="569" height="400"></a></p> <p>付加列インデックスは、カバリング インデックスの欠点を補うために搭載された機能です。</p> <h5 id="id004">付加列インデックス(Include オプション)</h5> <h6>付加列インデックス</h6> <p>付加列インデックスは、「Include オプション」とも呼ばれ、インデックスのリーフ レベルへ指定した列を含める(Include する)ことができる機能で、SQL Server 2005 から提供されました。カバリング インデックス (複合インデックス) との違いは、前のページにも掲載しましたが、次の図のとおりです。</p> <p><a href="http://lh3.ggpht.com/-aBqe-henZuQ/UVZP0KB82KI/AAAAAAAAOuM/wfsWMOC0GRg/s1600-h/image%25255B38%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-8EemtjixK1g/UVaQId3ngvI/AAAAAAAAO24/2kejsiyChS0/image_thumb20.png?imgmax=800" width="575" height="295"></a></p> <p>カバリング インデックスは、インデックスのリーフ レベルだけでなく、ルートと中間ページにもカバリングへ含めた列(「名」列)の値を格納しますが、付加列インデックスの場合はリーフ レベルにしか値を格納しません。これにより、インデックス サイズを小さくすることができます。インデックス サイズを小さくできれば、インデックスの Seek および Scan で読み取るページ数 (I/O 数) を少なくできるので、パフォーマンスが向上します。<br>付加列インデックスは、カバリング インデックス(複合インデックス)の欠点を補った機能なので、どちらを利用するかを迷ったら、迷わず付加列インデックスを利用することをお勧めします。</p> <h6>付加列インデックスの作成</h6> <p>付加列インデックスを作成するには、CREATE INDEX ステートメントを次のように利用します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX index_姓名<br>ON 社員(列名)<br>INCLUDE(リーフへ含めたい列名1, 列名2, …)</p></td></tr></tbody></table> <p>INCLUDE キーワードを付けて、含めたい列をカッコ内へ記述します。</p> <h6>GUI でのインデックスの作成</h6> <p>付加列インデックスを、オブジェクト エクスプローラーから作成する場合は、次のように操作します。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDe53Y8ZXjlBkxobERj1_wX31vmBdloXJGk693gIQWDIQR1Ecq9gKkJHiNggCCSHtvc0lompyIOGw6n5M5UHhYnNjSt7EAY7DWJsh5jYmMkdUIWtiblcmJAobrzxyYxIMDnSa3ktsclbpv/s1600-h/image44.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-a5ZdhVfqgLo/UVaQVQpk8GI/AAAAAAAAO3I/iYOqajCEyoE/image_thumb24.png?imgmax=800" width="531" height="595"></a></p> <p>[新しいインデックス]ダイアログで、インデックスを作成する列を追加したあとに、次のように[含まれている列]タブをクリックして開きます。</p> <p><a href="http://lh3.ggpht.com/-3FN-ceA-6dg/UVaQaVnzZVI/AAAAAAAAO3Q/mxvfPnIb6jo/s1600-h/image49.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Op0wvBoQdxw/UVaQh0JRevI/AAAAAAAAO3Y/cIMRIzKGDXM/image_thumb27.png?imgmax=800" width="528" height="349"></a></p> <h6>Let's Try</h6> <p>それでは、付加列インデックスを作成してみましょう。</p> <ol> <li>まずは、前の Step で作成したカバリング インデックス「index_姓名」を削除します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">DROP INDEX 社員.index_姓名</td></tr></tbody></table> <li>次に、「姓」列に対して、「名」列を含む付加列インデックスを作成します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX index_姓名 ON 社員(姓) INCLUDE(名)</p></td></tr></tbody></table> <li>作成が完了したら、カバリング インデックスのときと同じ検索を実行してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT 社員番号, 姓, 名FROM 社員WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-MEmfLY2ttKI/UVaQm2l5ZII/AAAAAAAAO3g/9JBoGm-LFMQ/s1600-h/image53.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-VHyIC2gB65c/UVaQuYIxYxI/AAAAAAAAO3o/obq6_GpliAM/image_thumb29.png?imgmax=800" width="543" height="253"></a><br>結果は、カバリング インデックスのときと同様、Index Seek のみが実行されたことを確認できます。</li></ol> <p><font color="#9b00d3">Note: カバリングと付加列インデックスの中身の参照</font><br>隠しコマンドの DBCC IND と DBCC PAGE を利用して、実際のカバリング インデックスの中身(中間またはルート ページ)を参照すると、次のようになります。</p> <p><a href="http://lh3.ggpht.com/-XPIDYqDkAcY/UVaQzJPMSKI/AAAAAAAAO3w/GHHyhNv_1eU/s1600-h/image57.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-pxyTsa3QrD0/UVaQ6hgPSvI/AAAAAAAAO34/0n_DLcJhvvs/image_thumb31.png?imgmax=800" width="521" height="314"></a><br>カバリング インデックスは、中間またはリーフ ページにも、「名」列が含まれていることを確認できます。<br>これに対して、付加列インデックスの中身を参照すると、次のようになります。</p> <p><a href="http://lh4.ggpht.com/-ZzOzr5N3quw/UVaQ_s0vMlI/AAAAAAAAO4E/2TdQacSKPtc/s1600-h/image61.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-zp-ArPQOwlk/UVaRGyFTDiI/AAAAAAAAO4M/OaDvU0JJFzE/image_thumb33.png?imgmax=800" width="519" height="234"></a></p> <p>付加列インデックスの場合は、「名」列が含まれていないことを確認できます。<br>これにより、付加列インデックスの方がカバリング インデックスよりも、インデックスのサイズを小さくすることができます。この差はデータのサイズが大きければ大きいほど差が顕著になります。<br>前述したように、インデックス サイズが小さくなれば、インデックスの Seek および Scan で読み取るページ数 (I/O 数) を少なくできるので、付加列インデックスの方がパフォーマンスを向上させることができます。</p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-70992529932480411182013-03-30T16:06:00.001+09:002013-03-30T16:06:16.621+09:00◆インデックスの保守<ul> <li><a href="#id001">断片化とは</a> <li><a href="#id002">断片化の調査: dm_db_index_physical_stats</a> <li><a href="#id003">断片化の解消(インデックスの再構築と再構成)</a> <li><a href="#id004">断片化の事前防止策: FILLFACTOR</a> </li></ul> <h5 id="id001">断片化とは</h5> <p>インデックスのリーフ ページは、Index Scan を高速化するために、次のようにそれぞれのページ同士がリンクしています。</p> <p><a href="http://lh3.ggpht.com/-G_RatnWGHsI/UVaK8PrQRmI/AAAAAAAAOxk/ZDxIqrBhHX4/s1600-h/image3.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-IrnTKyj5ME8/UVaLDdsdRfI/AAAAAAAAOxs/pHFRy3qhaJk/image_thumb1.png?imgmax=800" width="459" height="144"></a></p> <p>リーフ ページは、物理的に連続して格納されている場合は、先読み(先行読み取り)機能が効果的に働き、パフォーマンス良くデータを取得することができます。<br>断片化(fragmentation: フラグメンテーション)は、次のようにリーフ ページが連続的ではなく断片的に格納された状態です。</p> <p><a href="http://lh3.ggpht.com/-mWAIJxsGAQI/UVaLIYXHEuI/AAAAAAAAOx0/5FFZ_P0K2ho/s1600-h/image7.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-T_r661LMEJE/UVaLPkl5QZI/AAAAAAAAOx8/sTfxT884y1o/image_thumb3.png?imgmax=800" width="454" height="158"></a></p> <p>このように、リンクしているページが物理的に離れた場所にある状態が断片化が発生した状態です。このままでは、先読み機能が効果的に働かなくなり、Index Scan のパフォーマンスが大きく低下します。したがって、インデックスを作成した後は、断片化を事前防止する、あるいは断片化を解消するための保守を行うことが非常に重要になります。</p> <h6>断片化の原因(ページ分割)</h6> <p>デフォルトでは、ページ内は、追加または更新されたデータによって満杯に埋まっています。この状態で、間に割り込む値が追加されると、「ページ分割」が発生します。たとえば、次の図は、「姓」列へ作成したインデックスのリーフ ページが満杯に埋まっている状態を表していますが、このとき「Akiba」さんという間に割り込む値が INSERT されたとすると、ページには入りきらないので、物理的に離れた場所へ新しいページが作成されます。</p> <p><a href="http://lh4.ggpht.com/-52UrBJh-V_k/UVaLUnqjk-I/AAAAAAAAOyE/rVRWIq5Cy5s/s1600-h/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-SiqHmRRE200/UVaLcOGHvvI/AAAAAAAAOyM/cFVYIV9599U/image_thumb5.png?imgmax=800" width="443" height="337"></a></p> <p>この動作(ページ分割)は、インデックスが常に昇順に並べ替えられた状態に保たれている必要があるために行われています。間に割り込む値が追加されると、その値によって溢れ出たデータを格納するための新しいページが必要になるのです。また、このページには、溢れ出た1行のみが格納されるのではなく、元のページと新しいページが半分半分になるように調整されます。これは、もう一度間に割り込む値が追加されたとしても、簡単にページ分割を発生させないようにするための処置です(ページ分割を連続して発生させないための処置です)。このようなページ分割は、元のページと新しいページに半分半分にデータを割り当てることから、「50-50ページ分割」とも呼ばれています。<br>50-50ページ分割によって作成された新しいページは、物理的には、最後の空きページが利用される場合がほとんどです。これによって、リーフ ページが断片化した状態が生まれます。</p> <h5 id="id002">断片化の調査: dm_db_index_physical_stats</h5> <p>断片化が発生しているかどうかを調べるには、Step3 でインデックスの階層数と使用ページ数を調べるために利用した dm_db_index_physical_stats 動的管理関数を利用します。構文は次のとおりです。</p> <p><a href="http://lh5.ggpht.com/-p4rO4QbkBlg/UVaLhJ6234I/AAAAAAAAOyU/jKAHzIGcOQk/s1600-h/image15.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-NViqvCHfuOM/UVaLof1j96I/AAAAAAAAOyc/IeVs_bXvC0I/image_thumb7.png?imgmax=800" width="558" height="48"></a></p> <p>この関数の出力結果のうち、主なものは次のとおりです。</p> <p><a href="http://lh5.ggpht.com/-tpUEvny-fIg/UVaLtGeuTGI/AAAAAAAAOyk/AQ-EoAx3Oz4/s1600-h/image19.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-6x1jyPYIwIU/UVaL0mA87zI/AAAAAAAAOys/iHF0tgRCygE/image_thumb9.png?imgmax=800" width="562" height="262"></a></p> <p>avg_fragmentation_in_percent という列で、断片化の割合を確認することができます。</p> <h6>スキャン モード</h6> <p>dm_db_index_physical_stats 関数の第5引数で指定するスキャン モードは、次の 3種類があります。</p> <p><a href="http://lh5.ggpht.com/-TDZAPzwdbRA/UVaL5lEwGXI/AAAAAAAAOy0/KkXap_m7384/s1600-h/image23.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-CLsV5ghbG_0/UVaMBAxlKEI/AAAAAAAAOy8/q-VsbI0CLrs/image_thumb11.png?imgmax=800" width="556" height="144"></a></p> <p>LIMITED モードを使用すると、最も高速に断片化を調査することができます。</p> <h6>LIMITED モードが高速な理由</h6> <p>dm_db_index_physical_stats 関数の LIMITED モードが高速な理由は、インデックスのリーフ レベルをスキャンせずに、ルート ページと中間ページのみを調査して、断片化の割合を算出するためです。次の図のように、中間ページにはリーフ ページのページ番号が格納されているので、ページ番号が連続か不連続であるかをチェックするだけで、断片化の割合を調べることができます。</p> <p><a href="http://lh3.ggpht.com/-ZcHygYH2Eo4/UVaMGPGQFXI/AAAAAAAAOzE/U6_ODm38pgU/s1600-h/image27.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-5FFXC6Q-rPQ/UVaMNncTIgI/AAAAAAAAOzM/SBP-ThPgpwc/image_thumb13.png?imgmax=800" width="543" height="341"></a></p> <p>ただし、LIMITED モードの場合には、avg_page_space_used_in_percent(ページの平均使用密度)や、record_count(ページ内の行数)など、結果を取得できない列もあります。詳しくは、オンライン ブックの次の場所を参考にしてください。</p> <p><a href="http://lh3.ggpht.com/-Nf9RP3U2qFQ/UVaMSlLmoII/AAAAAAAAOzU/4akxDWd9_LI/s1600-h/image30.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-BsZEGwIgZw0/UVaMZvpje7I/AAAAAAAAOzc/ZhwyygD6ips/image_thumb14.png?imgmax=800" width="244" height="91"></a></p> <h6>Let's Try</h6> <ol> <li>まずは、前の Step で作成した付加列インデックス「index_姓名」の、インデックス ID を調べましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT name, index_id, * FROM sys.indexes WHERE object_id = OBJECT_ID('社員')</p></td></tr></tbody></table><a href="http://lh4.ggpht.com/-900kC0cQqaU/UVaMevVziSI/AAAAAAAAOzk/0io8vEzfZtQ/s1600-h/image34.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-W0fnZYjnR90/UVaMl0T6hCI/AAAAAAAAOzs/OCPyoIbStno/image_thumb16.png?imgmax=800" width="347" height="167"></a><br>インデックスID(index_id)が「3」であることを確認できます。 <li>続いて、dm_db_index_physical_stats 関数を利用して、「index_姓名」の断片化の状態をチェックしてみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT<br>avg_fragmentation_in_percent, index_id, index_level, page_count<br>FROM<br>sys.dm_db_index_physical_stats<br>(DB_ID('sampleDB'), OBJECT_ID('社員'), 3, NULL , 'DETAILED')<br>ORDER BY index_id, index_level DESC</p></td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-hLzmO3nQALo/UVaMq0YGSTI/AAAAAAAAOz0/xJOTxZu1g2s/s1600-h/image38.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-2DmOZRhmbLo/UVaMyCJGmSI/AAAAAAAAOz8/ntquRwBEB60/image_thumb18.png?imgmax=800" width="570" height="212"></a><br>「avg_fragmentation_in_percent」列は、すべての階層(index_level)で、「0」となっていることから、断片化が全く発生していないことを確認できます。また、このときの、リーフ レベル(index_level が 0)のページ数(page_count 列)が、「35」ページであることも確認できます。 <li>次に、社員テーブルに対して、データを 1件 INSERT してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>INSERT INTO 社員 VALUES(10001, 'Aiba', 'Jiro', '男性', '03-1234-xxxx', '')</p></td></tr></tbody></table> <li>データの追加後、もう一度同じクエリを実行して、index_姓名の断片化の状態をチェックしましょう。<br><a href="http://lh3.ggpht.com/-sQVuud9z-ns/UVaM2wLR92I/AAAAAAAAO0E/z8yut1SvVnU/s1600-h/image43.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-DgFiGAHCo2U/UVaM-O3MmaI/AAAAAAAAO0M/640VWRM0bhk/image_thumb21.png?imgmax=800" width="521" height="111"></a><br>今度は、リーフ レベルで、「5.555…」パーセントの断片化が発生し、ページ数がデータを追加する前より 1ページ増えて、「36」ページになっていることを確認できます。 <li>続いて、さらに、社員テーブルに対して、9 件のデータを追加してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>INSERT INTO 社員<br>VALUES(10002, 'Eto', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10003, 'Fujiwara', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10004, 'Goto', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10005, 'Inoue', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10006, 'Kato', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10007, 'Matsumoto', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10008, 'Oshima', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10009, 'Saito', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10010, 'Yamamoto', 'Goro', '男性', '03-1234-xxxx', '')</p></td></tr></tbody></table> <li>データの追加後、もう一度断片化の状態をチェックします。<br><a href="http://lh4.ggpht.com/-X_Cv43fXrFA/UVaNDOWxMiI/AAAAAAAAO0U/jvvSNmTDpgU/s1600-h/image47.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-q83QHdZS6zg/UVaNKUDq71I/AAAAAAAAO0c/ZqlBBq8VogE/image_thumb23.png?imgmax=800" width="611" height="123"></a><br>今度は、リーフ レベルで、「37.20…」パーセントの断片化が発生していることを確認できます。また、ページ数も「43」ページへ増えています。</li></ol> <h5 id="id003">断片化の解消(インデックスの再構築と再構成)</h5> <p>断片化を解消するには、次の 3つの方法があります。</p> <ul> <li>インデックスのオフライン再構築 <li>インデックスのオンライン再構築(Enterprise エディション のみで利用可能) <li>インデックスの再編成</li></ul> <p>これらは、いずれも ALTER INDEX ステートメントを利用して実行することができます。</p> <h6>インデックスのオフライン再構築(REBUILD)</h6> <p>インデックスの再構築(オフライン)は、内部的には新しい領域へインデックスを再作成し、古いインデックスを削除することで、断片化を解消します。したがって、インデックスの再構築中は、インデックス全体がロックされるので、再構築中は、別のトランザクションからはインデックスに対して一切アクセスすることができません。再構築が完了するまでは、ユーザー操作は待ち状態になります。これが「オフライン」と呼ばれる理由です。<br>クラスター化インデックスの場合は、実際のデータそのものをインデックス内へ格納しているので、再構築中にはテーブル データすべてがアクセスできないことに注意する必要があります。</p> <p><font color="#9b00d3">Note: 再構築にかかる時間<br></font>再構築にかかる時間は、インデックスが使用するページ数(ディスク容量)が大きければそれだけ時間がかかります。使用するページ数が大きいのは、行サイズが大きいインデックスで、クラスター化インデックス、カバリングインデックスなどです。特にクラスター化インデックスは、実際のデータそのものを格納しているため、再構築には非常に時間がかかります。</p> <p>インデックスをオフライン再構築(REBUILD)するには、ALTER INDEX ステートメントを次のように利用します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名 ON テーブル REBUILD</p></td></tr></tbody></table></p> <h6>Let's Try</h6> <ol> <li>社員テーブルへ作成した付加列インデックス「index_姓名」を再構築してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX index_姓名 ON 社員 REBUILD</p></td></tr></tbody></table><br>「コマンドは正常に完了しました」と表示されれば、再構築が完了しています。 <li>次に、dm_db_index_physical_stats 関数をクエリして、断片化の状態をチェックしてみましょう。<br><a href="http://lh3.ggpht.com/-sLNnK7YCTIo/UVaNPN48VsI/AAAAAAAAO0k/BvFBn-9sPss/s1600-h/image52.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-3x_xRrcaoXE/UVaNWLIw-7I/AAAAAAAAO0s/_5fUwBy2x3Q/image_thumb26.png?imgmax=800" width="392" height="88"></a><br>リーフ レベルの avg_fragmentation_in_percent が「0」% になり、断片化が完全に解消されたことを確認できます。また、page_count も「35」ページ(断片化する前と同じ)へ戻っていることを確認できます。</li></ol> <h6>インデックスのオンライン再構築</h6> <p>インデックスの再構築は、オンラインで行うことも可能です。これは、SQL Server 2005 から提供された機能で、Enterprise エディションでのみ利用することができます。インデックスの再構築をオンラインで実行した場合は、再構築中にユーザーがデータへアクセスすることができます。<br>インデックスの再構築をオンラインで行うには、ALTER INDEX ステートメントを次のように実行します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名 ON テーブル REBUILD WITH ONLINE = ON</p></td></tr></tbody></table> <h6>インデックスの再編成(REORGANIZE)</h6> <p>インデックスの再編成は、SQL Server 2000 までは、DBCC INDEXDEFRAG コマンドとして提供されていた機能です。インデックスの再編成は、リーフ ページの断片化のみを解消し、再編成の実行中もユーザーがアクセスすることができます。ただし、ロック中のページはスキップされて、また、断片化の度合いがひどい場合には、インデックスの再構築(REBUILD)よりも実行時間のかかってしまうので注意してください。<br>インデックスの再編成を実行するには、ALTER INDEX ステートメントを次のように実行します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名 ON テーブル REORGANIZE</p></td></tr></tbody></table> <h6>再構築と再編成の違い</h6> <p>インデックスの再編成は、同じ領域を再利用して、それぞれのページを比較して並び替えを行うことで、断片化を解消しています。このような内部動作の違いがあるので、再編成は断片化の割合が大きい場合には、非常に時間がかかってしまいます。<br>これに対して、インデックスの再構築は、新しい領域にインデックスを再作成します。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizEibVV2A2ckzOdY1FWB4Q1MgkWKWAq76dd4AegFddZwUt-ssevQc_-x1gad-x-hOqeC-BbSqpJ4qgUOatq5Npz6hMwHhkxocGw9bahJzyYCMSew4SXEuI4dkQJ3lOlEYEPiQ8RkVSNolb/s1600-h/image56.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghvQtUg_vBsx7R8-jfmB0cOmhJDiJc60bAlFhCCfvUmhnIhqfucsvPfmfXUmtO_YNEH92RrG9KmlhnPoBbc8gQHZf4j5yw8ld5y3AGJqB0UfxAt9JcC9Bjbo6d5kBGlJd4Ig8obNIL40m0/?imgmax=800" width="452" height="299"></a></p> <p><font color="#9b00d3">Note: 断片化の割合が 30% 未満なら再編成、それ以上なら再構築</font><br>再構築を行うか、再編成を行うのかのおおまかな指針は、断片化の割合が 30% 未満なら再編成、それ以上なら再構築です。再編成(REORGANIZE)は、断片化の割合がひどい場合には、非常に時間がかかります。たとえば、次の 2つのグラフは、弊社のお客様のデータで、再編成と再構築の実行時間を比較したものです。</p> <p><a href="http://lh3.ggpht.com/-SiA_SjdFzDg/UVaNn6ZNIUI/AAAAAAAAO1E/1stqrfyYyI0/s1600-h/image62.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-GnRMPriFHe8/UVaNvLxx2uI/AAAAAAAAO1M/R23V-qKK_4U/image_thumb32.png?imgmax=800" width="617" height="289"></a></p> <p><font color="#9b00d3">Note: 再構築と再編成の詳細比較<br></font>再構築と再編成の違いについては、次の表を参考にしてください。</p> <p><a href="http://lh4.ggpht.com/-1CHjtevcnMs/UVaN0DOFJAI/AAAAAAAAO1U/1BKzsEfeOGs/s1600-h/image66.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ZjT0saq_jds/UVaN7ZDVp0I/AAAAAAAAO1c/zT9S5tAedrc/image_thumb34.png?imgmax=800" width="620" height="274"></a></p> <h5 id="id004">断片化の事前防止策: FILLFACTOR</h5> <p>インデックスの再構築や再編成を実行して断片化を解消しても、データの追加や更新、削除が行われていくと、また断片化が発生します。断片化の度合いがひどくなっていくと、パフォーマンスへの悪影響も起こってきます。したがって、断片化がすぐに発生しないように事前防止策を講じておくことが重要です。これを行えるのが「FILLFACTOR」です。<br>FILLFACTORは、「充填率」とも呼ばれ、インデックスの再構築時にリーフ ページ内の領域をデータで占める割合を制御するためのオプションです。これは0~100%の間に設定することができます。たとえば、FILLFACTOR を 80%に設定した場合は、次のように 80%をデータで埋め、20%を空き領域として残すことができます。</p> <p><a href="http://lh4.ggpht.com/-lLRMWYskVDM/UVaOAYDsZnI/AAAAAAAAO1k/d6n2ZWJG524/s1600-h/image70.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-QJQrOLM5pHw/UVaOHnjp_mI/AAAAAAAAO1s/mrdWsT5t2WU/image_thumb36.png?imgmax=800" width="481" height="209"></a></p> <p>このように、事前に空き領域を作成しておけば、データが追加されてもこの領域が利用されるので、(空き領域がなくなるまでは)50-50 ページ分割の発生(断片化)を防ぐことができます。<br><br><font color="#9b00d3">Note: FILLFACTOR のデフォルト値</font><br>FILLFACTOR は、デフォルトは 0% に設定されますが、これは 100% と同じ意味で、ページ内の領域をデータですべて埋めます。したがって、デフォルトでは、リーフページは満杯に埋められ、この状態で間に割り込むデータが追加されると 50-50 ページ分割(断片化)が発生します。</p> <h6>FILLFACTOR を設定したインデックスの再構築</h6> <p>FILLFACTOR は、インデックスの再構築時に設定することができます。これは、ALTER INDEX ステートメントを次のように記述します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名 ON テーブル REBUILD WITH ( FILLFACTOR = 値 )</p></td></tr></tbody></table> <h6>Let's Try</h6> <ol> <li>まずは、社員テーブルに対してデータを 10 件 INSERT して、断片化が発生することを確認してみましょう。(前の Step では、インデックスの再構築を実行したので、断片化が完全に解消されている状態です)。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>INSERT INTO 社員<br>VALUES(10011, 'Aiba', 'Saburo', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10012, 'Matuda', 'Saburo', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10013, 'Kato', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10014, 'Matsumoto', 'Goro', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10015, 'Okada', 'Ryu', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10016, 'Yamamoto', 'Hiroko', '女性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10017, 'Oda', 'Hiroshi', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10018, 'Sakamoto', 'Kenji', '男性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10019, 'Uchia', 'Yumi', '女性', '03-1234-xxxx', '')<br>INSERT INTO 社員<br>VALUES(10020, 'Nakamura', 'Hiromi', '女性', '03-1234-xxxx', '')</p></td></tr></tbody></table> <li>データの追加後、dm_db_index_physical_stats 関数を利用して、断片化の状態をチェックしてみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT<br>avg_fragmentation_in_percent, index_id, index_level, page_count<br>FROM<br>sys.dm_db_index_physical_stats<br>(DB_ID('sampleDB'), OBJECT_ID('社員'), 3, NULL , 'DETAILED')<br>ORDER BY index_id, index_level DESC</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-zjIH9BXtwtA/UVaOMeMhLwI/AAAAAAAAO10/dJqSIdB2S0c/s1600-h/image73.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-NSyjfX43oRQ/UVaOTnZcBsI/AAAAAAAAO18/1lvldzpOtMc/image_thumb37.png?imgmax=800" width="206" height="87"></a><br>リーフ ページが「37.2…」パーセントの断片化が発生していることを確認できます。このように、インデックスを再構築した後に、データが追加された場合は、また、断片化が発生してしまうのです。 <li>次に、FILLFACTOR を利用して、断片化を事前防止してみましょう。FILLFACTOR を設定するには、次のように ALTER INDEX ステートメントを実行して、インデックスを再構築します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX index_姓名 ON 社員 REBUILD WITH ( FILLFACTOR = 70 )</p></td></tr></tbody></table><br>FILLFACTOR には、70 を指定することで、30% の空き領域を作っています。 <li>次に、dm_db_index_physical_stats 関数を利用して、断片化の状態をチェックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYOUpSV4RolZ69lc2i9ZuXmQB0KLhYjitjh5GYwgLZpSnlASD7dOQArm6pwPnqw8PKHJdOc5W1-T3FQ6_pQ5X5AGv2wdm6EQzUm32NaYKbbZb4TmkyLEpPHJuxJUSGBW5W7duHIAGzwHJW/s1600-h/image77.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-e_JQiqUrvw0/UVaOftEkDlI/AAAAAAAAO2M/yV52ak3wFTo/image_thumb39.png?imgmax=800" width="409" height="90"></a><br>断片化が「0」% になって、断片化が起きていない状態であることを確認できます。<br>また、リーフ レベルの page_count が、「50」ページへ増えていることにも注目します。これは、30% の空き領域を作ったことで、使用するページ数が増えているためです。 <li>dm_db_index_physical_stats 関数では、avg_page_space_used_in_percent 列を取得すると、ページの平均使用密度を調べることができます。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT<br>avg_page_space_used_in_percent<br>,avg_fragmentation_in_percent, index_id, index_level, page_count<br>FROM<br>sys.dm_db_index_physical_stats<br>(DB_ID('sampleDB'), OBJECT_ID('社員'), 3, NULL , 'DETAILED')<br>ORDER BY index_id, index_level DESC</p></td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-94hKYebb_t0/UVaOkYSFyqI/AAAAAAAAO2U/eT_ni3suJQU/s1600-h/image80.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-2-YNCKLsOws/UVaOroBPVEI/AAAAAAAAO2c/GgxQCkyX_rQ/image_thumb40.png?imgmax=800" width="190" height="59"></a><br>FILLFACTOR を70%へ設定しているので、avg_page_space_used_in_percent(ページの平均使用密度)が「69.4…」(約70%)であることを確認できます。 <li>次に、もう一度、社員テーブルに対して、データを 10件 INSERT してみましょう。 <li>データの追加後、dm_db_index_physical_stats 関数を利用して、断片化の状態をチェックしてみましょう。<br><a href="http://lh6.ggpht.com/-psUu4jIJoTQ/UVaOwfGKtoI/AAAAAAAAO2k/AvCojVEKl_U/s1600-h/image83.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9MkSKscfetLL_OF1O3-mB9PK6WtK6VFq1d-L8XGUoqvrd_v09LL4PXR8CD6E0vbvn50gG2dqyAVBj82NADCi82trSDjFovBQsVCa9SrHzN-J64hYFa0BvEfgPmksD1-AlDOnF-nyyLVcC/?imgmax=800" width="209" height="66"></a><br>結果は、断片化が発生していないことを確認できます。また、page_count も増えていないことを確認できます。<br>このように、FILLFACTOR を設定しておくと、今後追加されるデータを考慮して、あらかじめページに余裕をもたせて、インデックスを再構築することができるので、再構築後すぐに断片化が発生することを防ぐことができます。</li></ol> <h6>おわりに</h6> <p>最後までこの自習書の内容を試された皆さま、いかがでしたでしょうか?<br>インデックスは、SQL Server のパフォーマンスを大きく左右する非常に重要な機能です。1つのテーブルに対して複数のインデックスを作成することができますが、, むやみに作成するのは良くありません。更新のオーバーヘッド(データ更新時に、実際のデータだけでなく、インデックス自体を更新する負荷)があるからです。<br>また、せっかくインデックスを作成しても、インデックスが活用されなかったり、検索のパフォーマンスが向上しなかったりすることもあります。インデックスは、特性をきちんと理解した上で、付加列インデックスなどを効果的に活用して、パフォーマンスの向上に役立てていただければと思います。<br>また、インデックスの作成後は、定期的な断片化のチェックも重要です。これを怠ると、「ある日突然遅くなった」という事態になりかねません。FILLFACTOR を設定して断片化を事前防止し、定期的にインデックスを再構築または再編成を実行して、健全な状態へ保つようにしましょう。</p> <h6>列ストア インデックスによる飛躍的な性能向上(2012 の新機能)</h6> <p>SQL Server 2012 からは、パフォーマンスを飛躍的に向上させることができる「列ストア インデックス」という新しいタイプのインデックス機能も提供されました。このインデックスは、SQL Server 2008 R2 の PowerPivot for Excel で実装されたインメモリのカラムベース エンジン(xVelocity エンジン)を RDB へ応用したものです。このエンジンでは、列単位でインデックスを格納し、それらは高度に圧縮されています。<br>列ストア インデックスは、大量のデータに対する集計処理時に大きな性能向上を期待できる機能で、特に、夜間バッチ処理時(夜間バッチでの日次集計や月次集計処理など)や、DWH(データ ウェアハウス)環境での集計処理時に大変役立つ機能です。弊社のお客様データ(1億2千万件の DWH)を利用して、列ストア インデックスの性能効果を検証したところ、以下のような結果が得られました。</p> <h6>列ストア インデックスの作成方法</h6> <p>列ストア インデックスの作成方法は、非常に簡単で、次のように COLUMNSTORE キーワードを追加するだけです。</p> <p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE NONCLUSTERED COLUMNSTORE INDEX インデックス名<br>ON テーブル名 (列名1, 列名2, 列名3, ~)</p></td></tr></tbody></table></p> <p>列ストア インデックスは、性能を向上させるために大変役立つ機能なので、ぜひ活用してみてください。本自習書シリーズの新機能編「No.3 DWH(データ ウェアハウス)関連の新機能」では、列ストア インデックスの効果を簡単に確認できるようにしたスクリプト(ステップ バイ ステップ形式での検証スクリプト)を紹介していたり、列ストア インデックスの利用時の注意点などを説明したりしているので、こちらもぜひご覧いただければと思います。</p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com1tag:blogger.com,1999:blog-7901251401713078028.post-12151886140512932212013-03-30T00:04:00.001+09:002013-03-30T00:04:08.215+09:00◆インデックスの構造と内部動作1<ul> <li><a href="#id001">データベースの内部構造</a> <li><a href="#id002">非クラスター化インデックスの正確な構造</a> <li><a href="#id003">インデックスの階層数の調査: dm_db_index_physical_stats</a> <li><a href="#id004">インデックスを作成しても効果のない列</a> <li><a href="#id005">インデックスが役立たない例</a> <li><a href="#id006">クラスター化インデックス</a> </li></ul> <h5 id="id001">データベースの内部構造</h5> <h6>データベースの内部構造</h6> <p>インデックスを理解するには、データベースの内部構造を理解しておくことが重要です。まずは、データベースの内部構造から理解していきましょう。<br>データベースは、次のように「データ ファイル」(.mdf)と「トランザクション ログ ファイル」(.ldf)の 2種類で構成されます。データ ファイルには、テーブルやデータ、インデックス、ビュー、ストアド プロシージャなどが格納されます。</p> <p><a href="http://lh5.ggpht.com/-LdD-TQwJRXM/UVWnPrqZIEI/AAAAAAAAOic/qz30tg8sevQ/s1600-h/image4.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-oBKvpttYgEY/UVWnXUPGcZI/AAAAAAAAOik/dEFtRkTtRdk/image_thumb2.png?imgmax=800" width="653" height="369"></a></p> <h6>ページとヒープ</h6> <p>データ ファイルは、内部的には、「ページ」という 8 KBの大きさで区切られています。ページは、ディスク入出力の単位です。たとえば、テーブルの行サイズが 800バイトであれば、1ページには10行分のデータを格納することができます。実データが格納されているページ全体は「ヒープ」と呼ばれています。<br>インデックス自体もデータ ファイルへ格納されるので、内部的にはページへ格納されています。したがって、ルート ノードは「ルート ページ」、中間ノードは「中間ページ」、リーフ ノードは「リーフ ページ」とも呼ばれます。</p> <h6>エクステント</h6> <p>データ ファイル内の連続した 8 ページは、「エクステント」と呼ばれ、テーブルやインデックスに割り当てられる領域の単位になります。データが追加されると、エクステントが1つ確保され、その後に追加されるデータが連続した 8 ページに格納されます。これによって、同じオブジェクトのデータが連続した8ページに格納されることが保証されます。テーブル スキャンや後述の Index Scan などの全件検索または範囲検索は、連続したデータにアクセスするので、ページごとではなく、エクステント単位(64KBの大きさ)でまとめて読み込むことでパフォーマンスを向上させています。</p> <h5 id="id002">非クラスター化インデックスの正確な構造</h5> <h6>非クラスター化インデックスの正確な構造</h6> <p>これまでは、インデックスがイメージしやすいようにデータの範囲で説明してきましたが、正確には、インデックス ページにはデータの範囲ではなくデータの値(インデックスを作成した列の値)が格納されます。検索時には、この値と検索条件に指定された値との大小関係によってツリー構造が走査されます。<br>たとえば、前の Step で作成した、「社員」テーブルの「姓」列に対する非クラスター化インデックスの構造は、次のようになります(データ量が少ない場合は、2階層のインデックスが作成されます)。</p> <p><a href="http://lh4.ggpht.com/-1kFF0ZUWCCY/UVWnc6ibB2I/AAAAAAAAOis/MpxCM7t_NiQ/s1600-h/image10.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Tyo_QjOiouw/UVWnkr7jJfI/AAAAAAAAOi0/RFZpStUrTNM/image_thumb6.png?imgmax=800" width="649" height="434"></a></p> <p>このインデックスの構造の場合に、「Aoki」さんのデータを検索する場合は、まずルート ページへアクセスして、「Aoki」さんが、「Andou」さんと「Asari」さんの間のデータであることが分かるので、下位ページ(リーフ ページ)の 102 ページへアクセスします。<br>102 ページ内で「Aoki」さんのデータを探し、見つかったら、その行識別子(1:131:5)から、データ ファイルID が 1(.mdf ファイル)、ページ番号が 131、そのページ内の 5行目に、実際のデータがあることが分かります。<br>このように、インデックスを利用することで、すべてのデータを検索することなく、わずか数ページを読み取るだけで該当のデータを取得することができます。</p> <p><font color="#9b00d3">Note: SET STATISTICS IO コマンドでクエリが読み取ったページ数を確認<br></font>クエリが実際に読み取ったページ数は、SET STATISTICS IO コマンドを利用すると調べることができます。これは、次のように実行します。<br>SET STATISTICS IO ON<br>実行後、SELECT ステートメントを実行して、[メッセージ]タブをクリックすると、次のように「論理読み取り数」が表示されて、クエリが読み取ったページ数を確認することができます。<br><a href="http://lh3.ggpht.com/-tia4W55qRAk/UVWnpuFLD0I/AAAAAAAAOi8/SagKDV7sGDc/s1600-h/image14.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-BN5btkpLUE4/UVWnw8DO4EI/AAAAAAAAOjE/s_DxTp4qgiE/image_thumb8.png?imgmax=800" width="518" height="141"></a><br>このコマンドは、接続が終了するか、、次のように SET STATISTICS IO を OFF に設定するまで有功です。<br>SET STATISTICS IO OFF<br>読み取ったページ数</p> <h5 id="id003">インデックスの階層数の調査: dm_db_index_physical_stats</h5> <h6>インデックスの階層数の調査</h6> <p>dm_db_index_physical_stats 動的管理関数は、インデックスの階層数や使用しているページ数を調べることができる大変便利な関数です。構文は次のとおりです。</p> <p><a href="http://lh4.ggpht.com/-6yuzaMA84M8/UVWn1uNwJUI/AAAAAAAAOjM/6ltDOZxwSj8/s1600-h/image18.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-f11oaf09u2U/UVWn84GB16I/AAAAAAAAOjU/VOVF1cZNp74/image_thumb10.png?imgmax=800" width="644" height="137"></a></p> <p>index_level 列で、インデックスの階層番号(リーフが 0、その上位は 1 からの連番)、page_count 列で、使用しているページ数を取得することができます。<br>第1~第4引数へは、調べたいインデックスのデータベースやテーブル、インデックスを ID で指定し、NULL を指定することも可能です。NULL を指定した場合は「すべて」を意味し、たとえば、テーブル ID に NULL を指定した場合は、すべてのテーブルを対象にすることができます。<br>個別に ID を指定する場合は、「データベースID」は DB_ID 関数、「テーブルID」は OBJECT_ID 関数から取得することができます。<br>「インデックスID」は、次のように sys.indexes カタログ ビューから取得することができます。</p> <p><a href="http://lh5.ggpht.com/-Kx_5vJvIBSo/UVWoBroFhpI/AAAAAAAAOjc/zWzAJo3cZCw/s1600-h/image22.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-L53lq16hcD8/UVWoJTKWWyI/AAAAAAAAOjk/aVCD5OhagtU/image_thumb12.png?imgmax=800" width="649" height="58"></a></p> <p>name 列でインデックスの名前、index_id でインデックスID を取得することができます。</p> <h6>スキャンモード</h6> <p>dm_db_index_physical_stats 関数の第5引数のスキャン モードには、「LIMITED」、「SAMPLED」、「DETAILED」の 3つがあります。インデックスの使用ページ数を調べるには、DETAILED モードを指定する必要があります(3つのモードの詳しい違いについては、Step 4 で説明します)。</p> <h6>Let's Try</h6> <ol> <li>まずは、sys.indexes カタログ ビューをクエリして、Step 2 で作成した「index_姓」非クラスター化インデックスのインデックスID を調べます。<br><a href="http://lh5.ggpht.com/-41GSro-MeEE/UVWoOSYmj0I/AAAAAAAAOjs/d9lppWSFJyw/s1600-h/image27.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijq7hJ-8PFSei4k4bCJEx7-OjehmK8kyQAAh-pfAo8opWHxkYhdg5pyoCm-zHwIVWcsig_MFQRdwqaQU_xJq5A27c2BkKBnjhJFFyVO_GVRLcRp6Psa_M7969rFTSX9gsY3zEHOuESokZZ/?imgmax=800" width="336" height="54"></a><br><a href="http://lh3.ggpht.com/-tbdq_Sz02Xw/UVWoaNVlMzI/AAAAAAAAOj8/eTZDA0P6p6I/s1600-h/image35.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-c-wnkQEKJZQ/UVWohWM8fvI/AAAAAAAAOkE/PW3-iEqToCw/image_thumb19.png?imgmax=800" width="618" height="152"></a><br>index_id 列が「3」となっているので、インデックスID が 3 であることを確認できます。 <li>次に、dm_db_index_physical_stats 関数を利用して、「index_姓」インデックスの階層数と使用しているページ数を調べます。<br><a href="http://lh5.ggpht.com/-gpE6_Jkuwbg/UVWomDenquI/AAAAAAAAOkM/wMyXtVvk9z4/s1600-h/image40.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-_FVFtTJ1mqk/UVWot5EvRWI/AAAAAAAAOkU/vAhbxy28_ps/image_thumb22.png?imgmax=800" width="523" height="425"></a><br>index_level 列には、「1」と「0」が表示され、「0」はリーフ ページ、「1」以上は中間ページより上の階層になり、一番大きい値がルート ページになります。したがって、今回は「1」が一番大きい値なので、これがルート ページになります(インデックスが 2階層で構成されていることが分かります)。<br><br><font color="#9b00d3">Note: 隠しコマンドの DBCC IND と DBCC PAGE でインデックスの中身を見る</font><br>インデックスは、dm_db_index_physical_stats 関数で階層数と使用ページ数を調べることができますが、インデックスの中身までは見ることができません。インデックスの中身を見るには、ページの中身を見るための隠しコマンド(ヘルプへ記載されていないコマンド)の「DBCC IND」と「DBCC PAGE」を利用する必要があります。<br>DBCC IND コマンドでは、インデックスが使用しているページ番号を取得することができ、たとえば、次のように入力すると、index_姓(インデックスID が 3)の使用しているページ番号を取得することができます。<br>DBCC IND(sampleDB, 社員, 3)<br><a href="http://lh6.ggpht.com/-IagvpqiMydo/UVWoy2dVhKI/AAAAAAAAOkc/otx6-zjqwdc/s1600-h/image44.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-hwEpzdzjd6s/UVWo6CDl-fI/AAAAAAAAOkk/faj49ARH3FM/image_thumb24.png?imgmax=800" width="609" height="122"></a>結果の PagePID 列は、インデックスへ割り当てられているページ番号で、IndexLevel 列は「0」がリーフ ページで、「1」以上は、リーフより上の階層を表します。<br>ここで調べたページ番号を DBCC PAGE コマンドの第3引数へ与えることで、そのページの中身を見ることができるようになります。たとえば、次のように入力すると、ページ番号「179」の中身を参照することができます。<br>DBCC PAGE(sampleDB, 1, 179, 3)<br><a href="http://lh5.ggpht.com/-S_PXry5TH9U/UVWo-1v0BdI/AAAAAAAAOks/Ud6bzvlSVFs/s1600-h/image48.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-5yE1OqjjRvM/UVWpGmFs3HI/AAAAAAAAOk0/WJMzc140Ako/image_thumb26.png?imgmax=800" width="611" height="275"></a></li></ol> <h5 id="id004">インデックスを作成しても効果のない列</h5> <h6>インデックスを作成しても効果のない列</h6> <p>インデックスを作成すれば、基本的にはパフォーマンスが向上しますが、作成してもパフォーマンスの向上が見込めないケースがあります。代表的なのは次の 2つです。</p> <ul> <li>WHERE句の検索条件にほとんど使用されない列<br>検索条件にほとんど使用されない列に対してインデックスを作成しても、効果はありません。逆に、作成したことによってパフォーマンスの低下を招く恐れがあります。インデックスを作成すると、インデックスを作成した列のデータを更新した際に、インデックスそのもの(ツリー構造)も更新されるからです。したがって、検索条件にほとんど使用されない列で、かつ更新頻度が過剰な列に対してはインデックスを作成しないことをお勧めします。<br><br><font color="#9b00d3">Note: 使用されていないインデックスを探すには?<br></font>動的管理ビューの「dm_db_index_usage_stats」を利用すると、SQL Server の起動後に、1 度も使用されていないインデックス(未使用のインデックス)や、使用回数の少ないインデックスを探すことができます。具体的な利用方法については、本自習書シリーズの「監視ツールの基本操作」で詳しく説明しています。<br> <li>検索条件に該当するデータが大量にある場合<br>インデックスは、大量のデータから 1~ 数百件のデータを取り出すときに最も効果があります。検索条件に該当するデータが大量にある場合には、インデックスの効果が得られません。該当データが多い場合には、RID Lookup で(リーフレベルのポインターを使用して)実際のデータを取得すると、テーブル スキャンよりも効率が悪くなることがあるからです。<br><a href="http://lh4.ggpht.com/-0nEV3ngT1ck/UVWpLb9Jl4I/AAAAAAAAOk8/JjpaAhLzE9c/s1600-h/image52.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-RXc3gAOU0Tw/UVWpSqXP29I/AAAAAAAAOlE/X5g4ZUD9tlk/image_thumb28.png?imgmax=800" width="406" height="240"></a><br>SQL Serverは、インデックスを使用するよりもテーブル スキャンの方が効率が良いと判断した場合には、テーブル スキャンを使用してデータ検索を実行します。</li></ul> <h6>Let's Try: 「性別」列へインデックスを作成した場合の動作</h6> <ol> <li>まずは、社員テーブルの「性別」列へインデックスを作成してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE INDEX index_性別 ON 社員(性別)</p></td></tr></tbody></table> <li>次に、グラフィカル実行プランの表示を有効にして、性別が「男性」の社員のみを検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM 社員 WHERE 性別 = '男性'</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-c0qBMb1Vw1k/UVWpXhqrO-I/AAAAAAAAOlM/hWba4WsB-xo/s1600-h/image57.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-VsdnQDFZj34/UVWpeyH5lGI/AAAAAAAAOlU/0Oq1EHiOvCw/image_thumb31.png?imgmax=800" width="631" height="284"></a>結果は、テーブル スキャンが実行されていることを確認できます。<br>このようにデータの種類が少ない場合(性別列は「男性」と「女性」の 2種類)は、検索条件に該当するデータが自ずと多くなるので、インデックスを作成しても利用されません。SQL Server は、インデックスを使用するよりもテーブル スキャンの方が効率が良いと判断したした場合は、テーブル スキャンを使用してデータ検索を実行するのです。 <li>このインデックス「index_性別」は、不要なので削除しておきます。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">DROP INDEX 社員.index_性別</td></tr></tbody></table></li></ol> <h6>検索条件に該当するデータ件数が多い場合</h6> <p>続いて、「姓」列に対して、検索条件に該当するデータ件数が多い場合に、インデックスが利用されるかどうかを確認してみましょう。</p> <ol> <li>データ件数を多く取得するために、次のように LIKE 演算子を利用して、姓が「A」で始まる社員を検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>SELECT * FROM 社員 WHERE 姓 LIKE 'A%'</p></td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-Cg0HgoCfB-0/UVWpj9WNTKI/AAAAAAAAOlc/iEXw1c9eVHg/s1600-h/image61.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-KIlawLEYcik/UVWprCNyBXI/AAAAAAAAOlk/F5u1tibCA8A/image_thumb33.png?imgmax=800" width="629" height="283"></a>結果が表示され、293 件のデータが検索条件に該当し、テーブル スキャンが実行されたことを確認できます。このインデックスは、「姓='Aoki'」という検索条件のときには Index Seek が実行されていました。<br>このように、検索条件に該当するデータ件数が多い場合には、インデックスが利用されず、テーブル スキャンが実行されます。<br><br><font color="#9b00d3">Note: クエリ オプティマイザーによる最適な実行プランの選択</font><br>データの検索時に、どのインデックスを利用するかや、インデックスを利用しないでテーブル スキャンを実行するかは、SQL Server の「クエリ オプティマイザー」という機能(プログラム)が判断します。クエリ オプティマイザーは、検索条件に該当するデータを取得するために、インデックスよりもテーブル スキャンを利用したほうが「高速」だと判断した場合は、テーブル スキャンを実行します。オプティマイザーは、I/O コスト(ディスク入出力の負荷がどれくらいか)、と CPU コスト(CPU への負荷がどれくらいか)などをもとに判断しています。</li></ol> <h5 id="id005">インデックスが役立たない例</h5> <p>条件に該当するデータ件数が少ない場合でも、SQL ステートメントの書き方が悪いと、インデックスを利用しない(Index Seek が実行されない)非効率な検索が行われます。これは、次の 3 つのケースです。</p> <ul> <li>LIKE 演算子を利用する際に、先頭に %(ワイルド カード文字)を指定している場合<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWIMt-UueN9IEzkvobLyzTrcKk6QEJ_Sbxlj08fMo5nVcEVl7_qbwFPvlx-pu4rVshCBxlvFihsHrAaObmvyL7elllgw0Mmcubu4NNHH7QdtUbFj00mD3PjeaptJhjnk6HO6wdULyixB0g/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-2NGZZBvB2ck/UVWp3UOtkVI/AAAAAAAAOl0/seqSCA1jrHo/image_thumb%25255B1%25255D.png?imgmax=800" width="341" height="38"></a> <li><strong></strong>演算子の左辺へ「関数」や「計算式」を記述している場合<br><a href="http://lh4.ggpht.com/-WUQ83LU8q_A/UVWp8JHW3zI/AAAAAAAAOl8/ltcEMzlF5U0/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-zATW91dek98/UVWqDaYLdiI/AAAAAAAAOmE/bIyXBc3F0jc/image_thumb%25255B3%25255D.png?imgmax=800" width="340" height="39"></a> <li>列へ設定してある照合順序とは、異なる照合順序を指定している場合<br><a href="http://lh5.ggpht.com/-hLzVvi_K05A/UVWqIPoz0ZI/AAAAAAAAOmM/9cv7ux4Hckg/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-wAkwAOtlWZY/UVWqPRJ8rxI/AAAAAAAAOmU/grtKi6SrIos/image_thumb%25255B7%25255D.png?imgmax=800" width="430" height="36"></a></li></ul> <h6>Let's Try: LIKE の先頭に % を指定した場合</h6> <ol> <li>まずは、LIKE 演算子で先頭をワイルドカード指定した場合の検索を試してみましょう。ここでは、検索条件に「姓 LIKE '%oki'」と指定して、姓が「oki」で終わる社員を検索してみます。<br><a href="http://lh6.ggpht.com/-_naTm84aEeU/UVWqUUNzSJI/AAAAAAAAOmc/HoTl1gVmcG4/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-H6W5iPchbZc/UVWqbsNt_JI/AAAAAAAAOmk/guyUE1qy768/image_thumb%25255B8%25255D.png?imgmax=800" width="206" height="62"></a><br><a href="http://lh3.ggpht.com/-mueiTnLREpQ/UVWqgfD0TOI/AAAAAAAAOms/P7Hc6Bki5yo/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-0elk3QuR5Uk/UVWqnlBO1CI/AAAAAAAAOm0/nyDSNFNMA4g/image_thumb%25255B11%25255D.png?imgmax=800" width="599" height="170"></a><br>グラフィカル実行プランには、Index Scan と RID Lookup が表示され、Index Seek ではないことを確認できます。<br>このように、LIKE 演算子の先頭がワイルド カード文字の場合は、Index Seek が実行されません。</li></ol> <h6>Index Scan とは</h6> <p>Index Scan(インデックス スキャン)は、Index Seek のアイコンと非常に似ていますが、内部動作は大きく異なります。Index Scan は、インデックスのツリー構造を利用せずに、リーフ ノードを横方向へ全スキャンして、該当データを探し出す内部動作です。したがって、Index Scan は、Index Seek よりも多くのページにアクセスすることになるので、効率の悪い検索方法です(Index Seek よりも実行時間がかかります)。<br><a href="http://lh5.ggpht.com/-PVaTT3qqOY0/UVWqsta9YHI/AAAAAAAAOm8/xbyFbGH2JnA/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-7LdFweY9s8A/UVWqzgsKHWI/AAAAAAAAOnE/o_345DZbQFQ/image_thumb%25255B13%25255D.png?imgmax=800" width="485" height="182"></a></p> <h6>関数処理をしている場合</h6> <ol> <li>ここでは、UPPER 関数を利用して、検索を実行してみましょう。<br><a href="http://lh5.ggpht.com/-Tdtd6dXhXE8/UVWq4-W0sII/AAAAAAAAOnM/jKlsyQ1jnAA/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-0TvZ3Tu5jVc/UVWrAOfAePI/AAAAAAAAOnU/cMfQSZZp7Zs/image_thumb%25255B16%25255D.png?imgmax=800" width="588" height="250"></a><br>結果は、Index Scan と RID Lookup になり、Index Seek が実行されていないことを確認できます(Index Scan によって、リーフページを全スキャンしているので、効率の悪い検索が実行されています)。</li></ol> <h5 id="id006">クラスター化インデックス</h5> <h6>クラスター化インデックス</h6> <p>今までは、「非クラスター化インデックス」について説明してきましたが、ここからは、もう 1つのインデックス「クラスター化インデックス」について説明します。<br>非クラスター化インデックスとクラスター化インデックスの違いは、非クラスター化インデックスではリーフ レベルへ実際のデータへのポインター(行識別子:RID)が格納されるのに対して、クラスター化インデックスの場合は、リーフ レベルにはポインターではなく実際のデータそのものを格納するところです。<br>クラスター化インデックスを作成すると、実際のデータが物理的に並べ替えられて、クラスター化インデックスのリーフ ページとなり、ヒープが削除されます。<br>たとえば、社員テーブルの「姓」列に対してクラスター化インデックスを作成すると、次のようになります。</p> <p><a href="http://lh3.ggpht.com/-_Y7yHKEDBFE/UVWrFJWdiPI/AAAAAAAAOnc/eIBb0uGzR6k/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-eGag2jiDdDA/UVWrM-V674I/AAAAAAAAOnk/MQcOZ-o2AlQ/image_thumb%25255B19%25255D.png?imgmax=800" width="639" height="477"></a><br>このように、クラスター化インデックスでは、実際のデータがインデックス内へ吸収されるので、非クラスター化インデックスよりも検索のパフォーマンスが向上します。</p> <h6>クラスター化インデックスの作成</h6> <p>クラスター化インデックスを作成するには、CREATE INDEX ステートメントで、次のように CLUSTERED を指定します。</p> <p><a href="http://lh6.ggpht.com/-sW2eIkH1A2I/UVWrS2eJ49I/AAAAAAAAOns/IIBVYa5CAMA/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-6ccPAuESkXQ/UVWrZ0G6i9I/AAAAAAAAOn0/PN-NbFpnmzo/image_thumb%25255B21%25255D.png?imgmax=800" width="417" height="38"></a></p> <p>クラスター化インデックスを、オブジェクト エクスプローラーから作成する場合は、次のように操作します。<br><a href="http://lh6.ggpht.com/-wWulWcyBbEk/UVWrelxmwvI/AAAAAAAAOn8/Hok7cxLPwcs/s1600-h/image%25255B44%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-on_DolxL1Ko/UVWrl5UEM2I/AAAAAAAAOoE/EaRPEGnDBeY/image_thumb%25255B23%25255D.png?imgmax=800" width="467" height="143"></a></p> <p><a href="http://lh5.ggpht.com/-79ZLCAQGcYE/UVWrqm-Fz_I/AAAAAAAAOoM/0OjQSoJc6D4/s1600-h/image%25255B48%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-86wXjdOVt0c/UVWrx9STweI/AAAAAAAAOoU/0XGVA6LwSKg/image_thumb%25255B25%25255D.png?imgmax=800" width="470" height="367"></a></p> <h6>Let's Try</h6> <p>それでは、クラスター化インデックスを作成してみましょう。</p> <ol> <li>まずは、「姓」列へ作成していた非クラスター化インデックス「index_姓」を削除します。<br><a href="http://lh5.ggpht.com/-iVciHgC4iCI/UVWr2zPEPWI/AAAAAAAAOoc/ibd9by1fshk/s1600-h/image%25255B51%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-cIGthxoxNs4/UVWr94F0uxI/AAAAAAAAOok/XVVzb1GNXRo/image_thumb%25255B26%25255D.png?imgmax=800" width="215" height="39"></a> <li>次に、「姓」列に対してクラスター化インデックスを作成します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiywR1PwKqWZCy4xvgEJ-fHkLeHZ6xjJ-X4aQiao1h_Dl7g_ydXHPR6ILqRtwomYAN7kRseFQK7MBjcf4AMcYmoakygRk3YA68qxXidZV4zNiUjKQMAYU3ZweHlEYIZvGYRfV25doDDWhEs/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-FaBsSGjVhQs/UVWsJ_RCwcI/AAAAAAAAOo0/XTSOCuHkK3Q/image_thumb%25255B27%25255D.png?imgmax=800" width="244" height="53"></a> <li>次に、sys.indexes カタログ ビューをクエリして、クラスター化インデックスのインデックス ID を確認してみましょう。<br><a href="http://lh3.ggpht.com/-mc_OA_-pI1Q/UVWsO_giY1I/AAAAAAAAOo8/6EZ5bxZs4H8/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-zhKYXi-O78Q/UVWsV7SyNSI/AAAAAAAAOpE/mj0SSVEeSMs/image_thumb%25255B30%25255D.png?imgmax=800" width="283" height="53"></a><br><a href="http://lh3.ggpht.com/-Kya8mbwl7G0/UVWsa7qGeyI/AAAAAAAAOpM/RsMYspLAoTU/s1600-h/image%25255B62%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-mm599r81osU/UVWsh0sqSgI/AAAAAAAAOpU/rm9xEJ2Cezs/image_thumb%25255B31%25255D.png?imgmax=800" width="283" height="118"></a><br>インデックスID(index_id)は、「1」であることを確認できます。このように、クラスター化インデックスのID は、常に 1 になります。 <li>続いて、dm_db_index_physical_stats 関数を利用して、クラスター化インデックスの階層数と使用しているページ数を確認してみましょう。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRqUOj7s3Xtr8-O28ecGXYNEnvHRKDGQd88ayjPWXLkS9RI2Y0lNsI-U-_FZC6ICGiKP9MwJ3XwJ1sEcoo6QaquM7WYfb35jmg1NsmKwSOB4cFvHoLZaF2UYusLfbqiEQMGAbaI2FzNT4k/s1600-h/image%25255B66%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-V1R8yAlFGqQ/UVWst23VJ_I/AAAAAAAAOpk/DXjVyuIngJI/image_thumb%25255B33%25255D.png?imgmax=800" width="504" height="129"></a><br>index_level が、「2」、「1」、「0」とあることから、このインデックスが 3階層であることが分かります。index_level の「0」がリーフ ページ、「1」が中間、「2」がルート ページです。また、クラスター化インデックスの場合は、リーフ ページ「0」が実データのページ数なので、実際のデータが「560」ページであることも確認できます。 <li>次に、「姓」列が「Aoki」さんの社員を検索して、クラスター化インデックスの効果を確認してみましょう。<br><a href="http://lh5.ggpht.com/-xbTa02vQyig/UVWsyoH1yMI/AAAAAAAAOps/cfcIu0A7yg4/s1600-h/image%25255B74%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-n5Vc1j4E_S4/UVWs57GXoFI/AAAAAAAAOp0/fvQBFV5Wmok/image_thumb%25255B34%25255D.png?imgmax=800" width="170" height="56"><br><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-0PvVffqj_dQ/UVWs-9lIc1I/AAAAAAAAOp8/8GevPDIhKQY/image_thumb%25255B37%25255D.png?imgmax=800" width="606" height="167"><br></a>実行プランを確認すると、「Clustered Index Seek」というアイコンが表示され、インデックス名「社員.index_姓」を確認することができます。これは、クラスター化インデックスを利用して検索を行ったという内部動作です。また、RID Lookup のアイコンがないところにも注目してください。クラスター化インデックスのリーフページは実際のデータが格納されているので、RID Lookup は必要なく、クラスター化インデックス内の Seek だけでデータを取得することができるのです。<br>このように、クラスター化インデックスは、RID Lookup をしなくて済む分、非クラスター化インデックスよりも検索のパフォーマンスが向上します。 <li> <p>次に、検索条件を「姓 LIKE 'A%'」へ変更して、該当データ件数が多くなるように、データを検索してみましょう(これは、非クラスター化インデックスのときは、テーブル スキャンになった検索です)。<br><a href="http://lh3.ggpht.com/-YjmTWknQQdQ/UVWtDlinc6I/AAAAAAAAOqE/5c8516bdEQ0/s1600-h/image%25255B79%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-SiPlJIGbBOM/UVWtLWSSftI/AAAAAAAAOqM/cPmOnTqY1v0/image_thumb%25255B40%25255D.png?imgmax=800" width="601" height="372"></a><br>結果は、293 件の結果が返り、実行プランを確認すると、Clustered Index Seek が表示されて、クラスター化インデックスの Seek のみで検索が完了したことを確認できます。<br>クラスター化インデックスでは、リーフ レベルに実際のデータが昇順に並び替えられているので、このような検索(先頭文字を指定した範囲検索)であっても、インデックスを利用して高速にデータを取得することができます。</p></li></ol> <h6>Clustered Index Scan とテーブル スキャン</h6> <p>次に、クラスター化インデックスを作成した「姓」列以外の列を利用して検索してみましょう。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihgYq9pHGfzpx02zqGFdi3-vbr9J9XqllGscjQ5ILACFWVRTUq3F9n9dkkb1ELb9L48T2u4BLFWzTaTShTrpfWDA49G2sHJA0U_SDPGIXyRvzcTdBM58XgtCzr9swZuUCw3kU70QO3wa-W/s1600-h/image%25255B83%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-x68Z5KPDL-A/UVWtXWiOY7I/AAAAAAAAOqc/T4urhllWMUI/image_thumb%25255B42%25255D.png?imgmax=800" width="462" height="285"></a></p> <p>実行プランを確認すると、「Clustered Index Scan」というアイコンが表示されます。このアイコンは、Clustered Index Seek と非常に似ていますが、意味は大きく異なります。<br>「名」列には、インデックスを作成していないので、テーブル スキャンが実行されるはずですが、Clustered Index Scan と表示されています。このアイコンは、クラスター化インデックスのリーフレベルを横方向へ全件スキャンしたという内部動作です。つまり、実行されていることはテーブル スキャンとほとんど同じです。これは、クラスター化インデックスを作成すると、実際のデータがインデックスに吸収されるので、「テーブル = クラスター化インデックス」となり、テーブル スキャンという概念がなくなるためです。</p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-87087700911409471472013-03-28T11:48:00.001+09:002013-03-28T11:48:20.932+09:00◆プレフィックスまたはサフィックスが無効です<p>クライアントのSSMSから2012サーバーに「編集」でつないだらエラーが発生。<br><a href="http://msdn.microsoft.com/ja-jp/library/ms163804(v=sql.105).aspx">プレフィックスまたはサフィックスが無効です。(Microsoft Visual Database Tools)</a></p> <p>どのDBもダメ。<br>参照はOK。</p> <p>雰囲気的にSSMSをバージョンアップする必要があるのかな。</p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-31250509355145361962013-03-23T22:47:00.001+09:002013-03-23T22:47:00.593+09:00◆インデックスの基礎<ul> <li><a href="#id001">インデックスがない場合の検索時の内部動作</a> <li><a href="#id002">インデックスの作成: CREATE INDEX</a> <li><a href="#id003">インデックスの内部構造</a> <li><a href="#id004">インデックスの削除と無効化</a> </li></ul> <h5 id="id001">インデックスがない場合の検索時の内部動作</h5> <p>Step1 で説明したように、インデックスが存在しないテーブルでは、どんなデータを検索する場合にも、必ず先頭から最後まで探し続けなければなりません=テーブル スキャン(全件検索)が実行されます。</p> <p>それでは、これを試してみましょう。</p> <ol> <li>まずは、Management Studio を起動して、クエリ エディターを開きます。 <li>次に、Step 1 でアタッチした「sampleDB」データベース内へ作成済みの「社員」テーブルのデータを確認します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>USE sampleDB<br>SELECT * FROM 社員</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-p2ziHFAysqw/UU2vSX60Y5I/AAAAAAAAOeU/3_7u_WnVuVI/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-S5Lvf5fPdCU/UU2vZ5tqKyI/AAAAAAAAOec/HcrQLBnlxRo/image_thumb%25255B1%25255D.png?imgmax=800" width="610" height="241"></a><br>このテーブルには、5,600件の社員データを格納してあり、「社員番号」や「姓」、「名」、「性別」、「TEL」などを格納しています。</li></ol> <h6>グラフィカル実行プランで内部動作の確認</h6> <p>SQL Server では、内部的な処理がどのように実行されたのかを簡単に確認できる「グラフィカル実行プラン」という機能があります。これにより、データ検索がどのように実行されたのかを確認することができます。では、これを試してみましょう。</p> <ol> <li>グラフィカル実行プランは、Management Studio で、「クエリ」メニューの「実際の実行プランを含める」をクリックすることで表示することができます。<br><a href="http://lh6.ggpht.com/-fT0fYB8FJPE/UU2ve24RsgI/AAAAAAAAOek/Ck2gMzVoDxE/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-rSfkY8ReZnE/UU2vmWHVULI/AAAAAAAAOes/xaq-kFEA6vY/image_thumb%25255B4%25255D.png?imgmax=800" width="393" height="312"></a> <li>続いて、クエリ エディターで次のように入力して、「社員」テーブルから「姓」列が「Aoki」さんのデータを検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>USE sampleDB<br>SELECT * FROM 社員 WHERE 姓 = 'Aoki'</p></td></tr></tbody></table><a href="http://lh4.ggpht.com/-CqfmcFj9Clc/UU2vrL_WbEI/AAAAAAAAOe0/eCUMhNxx7FU/s1600-h/image%25255B11%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-o8AVdL9NX0U/UU2vyfAOZeI/AAAAAAAAOe8/mdn67b0lJuA/image_thumb%25255B5%25255D.png?imgmax=800" width="244" height="130"></a><br>結果は、12件の「Aoki」さんを取得することができました。 <li>次に、[実行プラン]タブをクリックして、実行プラン(内部的な実行方法)を表示します。<br><a href="http://lh6.ggpht.com/-M8LlF_wBM80/UU2v3UuDhrI/AAAAAAAAOfE/QyVVlWruJXE/s1600-h/image%25255B15%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-FCZyZ69cox8/UU2v-lWOcuI/AAAAAAAAOfM/gA4vAxI9EKw/image_thumb%25255B7%25255D.png?imgmax=800" width="430" height="180"></a><br>「Table Scan」アイコンが表示されて、テーブル スキャン(全件検索)が実行されたことを確認できます。「社員」テーブルには、インデックスを 1つも作成していないので、SQL Server が Aoki さんのデータを探すには、テーブル スキャンをするしかありません。</li></ol> <h6>グラフィカル実行プランを表示しないようにする</h6> <p>一度設定した「実際の実行プランを含める」は、クエリ エディターを閉じるか、次のように、もう一度[クエリ]メニューから[実際の実行プランを含める]をクリックするまで有効です。<br><a href="http://lh6.ggpht.com/-Wap0CmX12ls/UU2wDdrTr5I/AAAAAAAAOfU/piae4EPY4mg/s1600-h/image%25255B19%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ewNAx-rwoVE/UU2wNJlwqJI/AAAAAAAAOfc/kU-6Isrs7Rc/image_thumb%25255B9%25255D.png?imgmax=800" width="476" height="142"></a></p> <p><font color="#9b00d3">Note: 推定実行プランの表示(クエリを実行せずに実行プランを確認)</font><br>グラフィカル実行プランは、クエリを実行しなくても確認することができます。これを行うには、次のように[クエリ]メニューの[推定実行プランの表示]をクリックします。<br><a href="http://lh5.ggpht.com/-LSNwERQhF_U/UU2wRyJ0QUI/AAAAAAAAOfk/Pi-Bro5eYcQ/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-QO4GSsrrvpk/UU2wZJh3pOI/AAAAAAAAOfs/_YeGNQhvuCs/image_thumb%25255B11%25255D.png?imgmax=800" width="255" height="189"></a></p> <h5 id="id002">インデックスの作成: CREATE INDEX</h5> <h6>インデックスの作成</h6> <p>インデックスを作成するには、CREATE INDEX ステートメントを利用します。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>CREATE [CLUSTERED または NONCLUSTERED] INDEX インデックス名<br>ON テーブル名(列名)</p></td></tr></tbody></table> <p>CLUSTERED を指定した場合は「クラスター化インデックス」、NONCLUSTERED を指定した場合は「非クラスター化インデックス」が作成されます(両者の違いについては、Step3 で説明します)。省略時は、非クラスター化インデックスが作成されます。</p> <h6>GUI でのインデックスの作成</h6> <p>インデックスは、オブジェクト エクスプローラーで、次のように[インデックス]フォルダーを右クリックして、[新しいインデックス]をクリックしても作成することができます。<br><a href="http://lh5.ggpht.com/-UJHIJOt55bg/UU2weNEbN0I/AAAAAAAAOf0/LYrT-dMjAXo/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-posV6TYVGIg/UU2wlM6MmVI/AAAAAAAAOf8/sg-sckT7XGA/image_thumb%25255B13%25255D.png?imgmax=800" width="450" height="212"></a></p> <p><a href="http://lh6.ggpht.com/-vY6FjgU9a-Y/UU2wqBgMCmI/AAAAAAAAOgE/PFw_apV3F88/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-BkUjXjXPuiQ/UU2wxbAFKVI/AAAAAAAAOgM/MGWTTtyeOdk/image_thumb%25255B15%25255D.png?imgmax=800" width="456" height="309"></a></p> <p>それでは、インデックスを作成してみましょう。</p> <ol> <li>クエリ エディターで次のように入力して、社員テーブルの「姓」列に対してインデックスを作成してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">CREATE INDEX index_姓 ON 社員(姓)</td></tr></tbody></table> <li>[クエリ]メニューから[実際の実行プランを含める]をクリックして、グラフィカル実行プランの表示を有効にしてから、前の手順でテーブル スキャンになったクエリ(Aoki さんの検索)を実行してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT * FROM 社員 WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh4.ggpht.com/-8i23WlyBtLc/UU2w2EFG0WI/AAAAAAAAOgU/AUMWyE6IECo/s1600-h/image%25255B37%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-tTsuQmD4Llw/UU2w9XVIigI/AAAAAAAAOgc/XavBSlmTUE4/image_thumb%25255B19%25255D.png?imgmax=800" width="518" height="271"></a><br>実行後、[実行プラン]タブを開くと、今度は、テーブル スキャンではなく、Index Seek と RID Lookup というアイコンが表示されていることを確認できます。Index Seek は、インデックスを利用した検索であることを表し、利用したインデックスの名前「社員.index_姓」が表示されます。RID Lookup については、後述します。<br> <li>次に、インデックスを作成していない、ほかの列で検索してみましょう。クエリ エディターへ次のように入力して、「名」列に「Taro」が格納されているデータを検索してみましょう。<br><a href="http://lh6.ggpht.com/-BtWU4A5G1Pg/UU2xCX1LLiI/AAAAAAAAOgk/IcyH46Nhnzs/s1600-h/image%25255B41%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Ufk2PmWmHqU/UU2xJbCPXAI/AAAAAAAAOgs/0iseYEEfmso/image_thumb%25255B21%25255D.png?imgmax=800" width="414" height="274"></a><br>結果は、テーブル スキャンになります。<br>インデックスは、列ごとに作成するものなので、作成した列以外の検索では使用されません。「名」列には、インデックスを作成していないので、SQL Server が、「Taro」というデータを探すには、テーブル スキャンをするしかないのです。したがって、「名」列での検索を高速化したい場合には、この列にもインデックスを作成する必要があります。<br></li></ol> <h5 id="id003">インデックスの内部構造</h5> <h6>インデックスの内部構造</h6> <p>インデックスは、内部的には次のようなツリー(Tree:木)構造で作成されます。</p> <p><a href="http://lh4.ggpht.com/-qyZNxJnd4Vc/UU2xOUit-yI/AAAAAAAAOg0/CrJB_qBy5dQ/s1600-h/image%25255B45%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/--BFg-ia6Uoo/UU2xVedijTI/AAAAAAAAOg8/LC8JEODVn_c/image_thumb%25255B23%25255D.png?imgmax=800" width="421" height="193"></a></p> <p>ツリーは、最上部を「ルート」ノード、中間部を「中間」ノード、最下部を「リーフ」ノードと呼びます。データは、昇順(小さい順)に並べ替えられて、データの範囲によって枝分かれします。ツリーの構造(枝分かれの数や何階層になるのか)は、データ量やデータサイズによって変化します。<br>SQL Server には、インデックスの種類として、「クラスター化インデックス」と「非クラスター化インデックス」の 2つがありますが、両者の違いについては、Step3 で詳しく説明します。</p> <h6>非クラスター化インデックスの内部構造</h6> <p>前の手順では、社員テーブルの「姓」列に対してインデックス(非クラスター化インデックス)を作成しましたが、この場合のインデックスの内部構造は、次のようになります。</p> <p><a href="http://lh3.ggpht.com/-8qQKllsZ6DI/UU2xacUAP3I/AAAAAAAAOhE/e_dO93Vh2KY/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-owHzSN5Qsag/UU2xiH4Qi-I/AAAAAAAAOhM/Q-TT00hN-F4/image_thumb%25255B26%25255D.png?imgmax=800" width="643" height="403"></a></p> <p>インデックス内は、「姓」列のデータが昇順に並べ替えられて、データの範囲によって枝分かれしています。リーフ ノードには、実際のデータへのポインター(位置情報)が格納され、ポインターには「行識別子」(RID:Row ID)が使用されています。<br>このように、インデックスを構成した場合の検索は、ツリーを調べるだけで済むので、高速になります。たとえば、前の手順で行った「WHERE 姓='Aoki'」という検索条件であれば、「A」が格納されている範囲のみを調べるだけで済むのです。<br>なお、正確なインデックスの内部構造については、Step3 で詳しく説明します。</p> <h5 id="id004">インデックスの削除と無効化</h5> <h6>インデックスの削除: DROP INDEX</h6> <p>インデックスを削除するには、DROP INDEX ステートメントを利用します。構文は、次のとおりです。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>DROP INDEX テーブル名.インデックス名<br>または<br>DROP INDEX インデックス名 ON テーブル名</p></td></tr></tbody></table> <h6>GUI でのインデックスの削除</h6> <p>オブジェクト エクスプローラーから、インデックスを削除したい場合には、次のように[インデックス]フォルダーを展開して、該当インデックスを右クリックし、[削除]をクリックします。</p> <p><a href="http://lh4.ggpht.com/-2gAMmLC9Noo/UU2xm4k7p3I/AAAAAAAAOhU/2GBb5b3b-wQ/s1600-h/image%25255B55%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-EkvDNBAXYHQ/UU2xuOq2FRI/AAAAAAAAOhc/cBPXVGeaJP8/image_thumb%25255B29%25255D.png?imgmax=800" width="252" height="397"></a></p> <h6>インデックスの無効化: ALTER INDEX .. DISABLE</h6> <p>インデックスは、無効化することも可能です。無効化は、削除に似ていますが、無効化した場合は、インデックスを再作成することなく、再構築するだけで、再び有効にすることができるので、一時的に外しておきたいインデックスがある場合に便利です(インデックスの再構築については、Step 4 で説明します)。<br>インデックスを無効化するには、ALTER INDEX ステートメントを次のように利用します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名<br>ON テーブル名 DISABLE</p></td></tr></tbody></table> <p>無効化したインデックスを、有効化したい場合は、次のように記述して、インデックスを再構築します。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX インデックス名<br>ON テーブル名 REBUILD</p></td></tr></tbody></table> <h6>GUI でのインデックスの無効化と有効化</h6> <p>オブジェクト エクスプローラーから、インデックスを無効化したい場合は、[インデックス]フォルダーを展開して、該当インデックスを右クリックし、[無効化]をクリックします。また、無効化したインデックスを有効化したい場合は[再構築]をクリックします。</p> <p><a href="http://lh5.ggpht.com/-jEPbrG20kYk/UU2xzK57X4I/AAAAAAAAOhk/Dddz_Pn0M4U/s1600-h/image%25255B60%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-XKy08irazjI/UU2x6Wdkh_I/AAAAAAAAOhs/CPgPaD6i3qU/image_thumb%25255B32%25255D.png?imgmax=800" width="606" height="257"></a></p> <p>それでは、インデックスの無効化を試してみましょう。</p> <ol> <li>前の手順で作成した「index_姓」インデックスを無効化してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX index_姓<br>ON 社員 DISABLE</p></td></tr></tbody></table> <li>次に、インデックスが無効化されたことを確認するために、「姓」列が「Aoki」さんのデータを検索してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT * FROM 社員WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-HkUg-EukR40/UU2x_LanciI/AAAAAAAAOh0/xu13GeInCRk/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-HYylk6jh738/UU2yGfU3tuI/AAAAAAAAOh8/73DklCPz9x4/image_thumb%25255B35%25255D.png?imgmax=800" width="331" height="213"></a><br>グラフィカル実行プランを表示すると、結果は、テーブル スキャンとなり、インデックスが利用されていないことを確認できます。 <li>次に、「index_姓」インデックスを有効化してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>ALTER INDEX index_姓<br>ON 社員 REBUILD</p></td></tr></tbody></table> <li>有効化(再構築)が完了したら、もう一度同じ検索を実行してみましょう。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT * FROM 社員 WHERE 姓 = 'Aoki'</td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-LVvnByRfcEI/UU2yLQ5ujLI/AAAAAAAAOiE/qyv33-HmfkQ/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ZIoKKxJa-O0/UU2ySoIm7rI/AAAAAAAAOiM/ajT7WuKAzWw/image_thumb%25255B37%25255D.png?imgmax=800" width="341" height="227"></a><br>今度は、Index Seek が表示されて、インデックスが利用されたことを確認できます。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com1tag:blogger.com,1999:blog-7901251401713078028.post-77002000728725589142013-03-23T21:11:00.001+09:002013-03-23T21:11:17.610+09:00◆インデックスの概要 と自習書を試す環境について<h5>インデックス(Index)の概要</h5> <h6>インデックスがない場合の検索時の内部動作</h6> <p>インデックスがないテーブルでは、データがどこに格納されているのかが分かりません。基本的には、データを追加(INSERT)した順に格納されていきますが、データを削除(DELETE)した場合には、その削除された領域は再利用されるので、追加や削除が繰り返される環境では、どこにどのデータが格納されているのかが分からなくなります。これについて、次の「社員」テーブルを例に考えてみましょう。</p> <p><a href="http://lh5.ggpht.com/-Im2c8-CHlgA/UU2ayvfhP1I/AAAAAAAAOcs/Bmhnb50DJ3A/s1600-h/image%25255B2%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-OR9jXBKYsnc/UU2a6LM70gI/AAAAAAAAOc0/LqJfGL4YKss/image_thumb.png?imgmax=800" width="244" height="231"></a></p> <p>この社員テーブルには、インデックスを作成していません。このとき、次の SELECT ステートメントを実行して、姓が「Aoki」さんの社員を検索したとします。</p> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT * FROM 社員WHERE 姓 = 'Aoki'</td></tr></tbody></table> <p>SQL Server にとっては、「Aoki」さんのデータは、どこにあるのかが分からず、また、何件の「Aoki」さんがあるのかも分かりません。したがって、SQL Server が該当データを検索するには、次のように先頭から最後まで探し続けなければなりません。</p> <p><a href="http://lh4.ggpht.com/-Y7GsI4oZuP8/UU2a_bOBxWI/AAAAAAAAOc8/IoM8IbY7bsc/s1600-h/image%25255B5%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-HPo-OKYs6dw/UU2bGm-LUUI/AAAAAAAAOdE/1UHWm9ESaPI/image_thumb%25255B1%25255D.png?imgmax=800" width="242" height="244"></a></p> <p>このように、インデックスが存在しない場合には、どんなデータを検索する場合にも、必ず先頭から最後まで探し続けなければなりません。この動作は「Table Scan:テーブル スキャン」、「全表走査」、「全件検索」などと呼ばれています。テーブル スキャンは、データ量が 1,000件、2,000件など少ない場合には、(今のコンピューターの性能では)瞬間的に結果が返りますが、1,000万件、1億件など大量データになった場合には大変な実行時間がかかってしまいます。</p> <p><font color="#9b00d3">Note: テーブル スキャンの速度はどれくらい?<br></font><font color="#ffffff">テーブル スキャンの速度は、ディスクの読み取り速度(実測値)に大きく依存します。たとえば、テーブル サイズが 500MB で、ディスクの読み取り速度が 100MB / 秒だったとすると、テーブル スキャンには最低でも 5秒かかるわけです(メモリにキャッシュされていない場合)。また、テーブル サイズが 5GB だったとすると、最低でも 50秒はかかってしまうことになります。このように、テーブル スキャンの速度は、データ量が増えれば増えるほど、非常に時間がかかってしまうので、これを解決するための検索手法が「インデックス」です。</font></p> <h6>グラフィカル実行プラン</h6> <p>SQL Server には、内部的な実行方法(テーブル スキャンが実行されたか、インデックスが利用されたかなど)を簡単に知ることができる、グラフィカル実行プランという機能があります。</p> <p><a href="http://lh5.ggpht.com/-4ufgWcAyJ_s/UU2bLTFCfrI/AAAAAAAAOdM/zLrK4ZNMfaw/s1600-h/image%25255B9%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-iwweR56tR7k/UU2bSgJkL7I/AAAAAAAAOdU/2PczoTlQiB0/image_thumb%25255B3%25255D.png?imgmax=800" width="324" height="169"></a></p> <p>この機能のおかげで、テーブル スキャンが実行される、効率の悪い検索を簡単に調べることができるので、大変便利です。Step 2 以降では、これらの利用方法を具体的に説明します。</p> <h5>サンプル データベース(sampleDB)の作成</h5> <p>この自習書では、すべての手順で<a href="http://go.microsoft.com/?linkid=9818746">サンプル スクリプト</a>に含まれる「sampleDB」データベースを利用しているので、STEP2 以降を始める前に、このデータベースを SQL Server 2012 上へ作成しておいてください。作成の手順は、次のとおりです。</p> <ol> <li>まずは、[スタート]メニューの[すべてのプログラム]から[Microsoft SQL Server 2012]の[SQL Server Management Studio]をクリックして、Management Studio を起動します。</li> <li>接続完了後、次のようにツールバーの[ファイルを開く]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-Yok1iAo0FiA/UU2bXZPj6NI/AAAAAAAAOdc/0pNmJWydahM/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-pTsvReZjccU/UU2beq_wG7I/AAAAAAAAOdk/F-6G4VAzR9I/image_thumb%25255B7%25255D.png?imgmax=800" width="590" height="178"></a><br>これにより、[ファイルを開く]ダイアログが表示されるので、サンプル スクリプトを解凍したフォルダーを展開して、「sampleDB.sql」ファイルを選択し、[開く]ボタンをクリックします。</li> <li>次のようにデータベースを作成するためのスクリプトが表示されるので、ツールバーの[実行]ボタンをクリックして、スクリプトを実行します。<br><a href="http://lh6.ggpht.com/-64RF_w1fd2A/UU2bji7KLRI/AAAAAAAAOds/M9iGijaKZts/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-H8XdqyB7cQU/UU2bq72uu5I/AAAAAAAAOd0/ztF_0msvpUs/image_thumb%25255B10%25255D.png?imgmax=800" width="511" height="248"></a></li> <li>数秒後に実行が完了して、次のように画面下に「クエリが正常に実行されました」と表示されることを確認します。<br><a href="http://lh5.ggpht.com/-KZwac_7920c/UU2bvpjNtXI/AAAAAAAAOd8/NhK8NIX8isk/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-uMnlWBorL_4/UU2b22dPRjI/AAAAAAAAOeE/GruqtxlxLCI/image_thumb%25255B12%25255D.png?imgmax=800" width="408" height="144"></a><br>以上でデータベースの作成が完了です。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-6795542583036578392013-03-20T23:15:00.001+09:002013-03-23T18:00:30.242+09:00◆パッケージの配置<ul> <li><a href="#id001">Integration Services サーバーとは</a> <li><a href="#id002">パラメーター化機能</a> <li><a href="#id003">SSISDB カタログの作成</a> <li><a href="#id004">Integration Services サーバーへのパッケージの配置</a> <li><a href="#id005">別マシンへのパッケージの配置</a> </li></ul> <h5 id="id001">Integration Services サーバーとは</h5> <h6>Integration Services サーバーとは</h6> <p>作成した SSIS パッケージは、「Integration Services サーバー」へ配置することで、パッケージをサーバーで管理したり、実行したりできるようになります。<br>Integration Services サーバーは、SQL Server 2012 のインストール時に、次のように[Integration Services]を選択したサーバーです。</p> <p><a href="http://lh3.ggpht.com/-N-V4gQLaRe4/UUm8PtpnMcI/AAAAAAAAORo/OODCs4KqRUQ/s1600-h/image3.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-1OfEWFE087I/UUm8XBHYusI/AAAAAAAAORw/00zJCWL-jPs/image_thumb1.png?imgmax=800" width="356" height="280"></a></p> <p>[Integration Services]をインストールすると、「SQL Server Integration Services 11.0」という名前サービスが追加されて、自動実行されます。これは、「SQL Server 構成マネージャー」ツールで次のように確認できます。</p> <p><a href="http://lh3.ggpht.com/-vr__lpi6FOc/UUm8b-dCrXI/AAAAAAAAOR4/e55lmiSuGRM/s1600-h/image7.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-8igD5JzykyI/UUm8jbP_2HI/AAAAAAAAOSA/5llDiXYTQbA/image_thumb3.png?imgmax=800" width="649" height="130"></a></p> <h5 id="id002">パラメーター化機能</h5> <h6>パラメーター化機能(サーバー名とパスのパラメーター化)</h6> <p>Integration Services サーバーへの配置を行う前に、作成したプロジェクトを、本番機など別のマシンへ配置する際に便利な「パラメーター化」機能を利用する手順を説明します。開発機と本番機では、接続する SQL Server の名前が違ったり、ファイルの配置してあるパスが違ったりするので、そういった場合にパラメーター化機能を利用すると大変便利です。</p> <ol> <li>まずは、データの転送先となる SQL Server の名前のパラメーター化を行ってみましょう。接続する SQL Server の名前をパラメーター化するには、次のように[接続マネージャー]の「サーバー名.ssisdb3」を右クリックして、[パラメーター化]をクリックします。<br><a href="http://lh3.ggpht.com/-m88ewCqclLk/UUm8oRgQgwI/AAAAAAAAOSI/zxYG6ArJMMg/s1600-h/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-tbJ-p30KqT8/UUm8vj6U09I/AAAAAAAAOSQ/2tEQyFgGAUw/image_thumb5.png?imgmax=800" width="304" height="330"></a><br><font color="#9b00d3"><font color="#9c85c0">Note: パラメーター化は SQL Server 2012 からの新機能</font><br></font>パラメーター化は、SQL Server 2012 から提供された新機能です。SQL Server 2008 R2 以前のバージョンを利用している場合は、「パッケージ構成ファイル」(.dtsConfig)という機能を利用することで、パラメーター化と同じようなことを実現することができます。 <li>[パラメーター化]をクリックすると、次のように[パラメーター化]ダイアログが表示されるので、次のように[プロパティ]で「ServerName」を選択します。<br><a href="http://lh4.ggpht.com/-_pwLKCVb8Ic/UUm80uEgj_I/AAAAAAAAOSY/l69oG6CNFPY/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-A-Wx7xddC2s/UUm876dzuFI/AAAAAAAAOSg/_yHLMnTJRFo/image_thumb%25255B1%25255D.png?imgmax=800" width="311" height="351"></a><br>[新しいパラメーターを作成する]が選択されていることを確認して、[名前]へ任意のパラメーター名(既定値は サーバー名Ssisdb3_ServerName)を入力し、[OK]ボタンをクリックします。 <li>[パラメーター]タブを開くと、作成したパラメーターが表示されていることを確認できます。<br>(リアルタイムには表示されなかったりするようだ。VS再起動で表示される)<br><a href="http://lh3.ggpht.com/-oXtn-LOzLxo/UUm9A2oWxpI/AAAAAAAAOSo/VQpsA_KJP4o/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-pzB9NfrY14c/UUm9H-fPYJI/AAAAAAAAOSw/2wYAUlXJ8oE/image_thumb%25255B3%25255D.png?imgmax=800" width="544" height="144"></a><br>これで、データ転送先となる SQL Server の名前のパラメーター化が完了です。 <li>次に、Foreach ループ コンテナーで指定した読み取り対象のフォルダーをパラメーター化してみます。次のように、[パラメーター]タブで、[新規作成]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-RLmbW8E-3OA/UUm9MxLLiXI/AAAAAAAAOS4/cgCLEeOxY9U/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hM5neTzl4Bg/UUm9T9JAoVI/AAAAAAAAOTA/DIpmsICsxoI/image_thumb%25255B5%25255D.png?imgmax=800" width="541" height="140"></a> <li>これにより、新しくパラメーターが作成されるので、次のように[名前]へ任意のパラメーター名(画面は importPath)を入力して、[データ型]へは「String」を選択し、既定値となる[値]へはサンプル スクリプトの import フォルダーへのパスを入力します。<br><a href="http://lh4.ggpht.com/-hSu1VIkfU5Q/UUm9Y5y7-uI/AAAAAAAAOTI/rLvDApLhiVQ/s1600-h/image%25255B16%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-F_m3o6k8QOg/UUm9gIdk5JI/AAAAAAAAOTQ/Q8RwJgd3hVM/image_thumb%25255B7%25255D.png?imgmax=800" width="540" height="140"></a> <li>続いて、[制御フロー]タブを開いて、[Foreach ループ コンテナー]をダブル クリックもしくは右クリックして[編集]を選択します。<br><a href="http://lh4.ggpht.com/-JZixzSHGKbA/UUm9lBRqKjI/AAAAAAAAOTY/Y-CBOeAWn9c/s1600-h/image%25255B20%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-fe-LjyoutSo/UUm9sYKd_xI/AAAAAAAAOTg/UNhhqIMavoE/image_thumb%25255B9%25255D.png?imgmax=800" width="392" height="182"></a> <li>[Foreach ループ エディター]ダイアログが開いたら、[コレクション]ページを開いて[Expressions]プロパティの[...]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-aVWdJoy2Lio/UUm9xLgAGPI/AAAAAAAAOTo/oqLhQeVJwPM/s1600-h/image%25255B24%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-l-Jfu4MZGqE/UUm94Q1eciI/AAAAAAAAOTw/Pi5x6YfDhjQ/image_thumb%25255B11%25255D.png?imgmax=800" width="392" height="171"></a><br>[プロパティ式エディター]ダイアログで[Directory]プロパティを選択して[...]ボタンをクリックします。 <li>[式ビルダー]ダイアログが開いたら、[変数とパラメーター]フォルダーを展開して表示されるパラメーターの一覧の中から、前の手順で作成した「$Package::importPath」パラメーターを[式]へドラッグ&ドロップします。<br><a href="http://lh4.ggpht.com/-eps6b3Dwfwo/UUm99GGKimI/AAAAAAAAOT4/ljoMOwfT50w/s1600-h/image%25255B28%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-_6-6NhvduaI/UUm-EY87e_I/AAAAAAAAOUA/LDpD7Xgdx4M/image_thumb%25255B13%25255D.png?imgmax=800" width="390" height="379"></a> <li>[プロパティ式エディター]ダイアログへ戻ったら、式が設定されていることを確認して、[OK]ボタンをクリックします。<br>[Foreach ループ エディター]ダイアログへ戻ったら、[OK]ボタンをクリックします。<br>これで、Foreach ループ コンテナーの読み取り対象のフォルダーのパラメーター化が完了です。</li></ol> <h5 id="id003">SSISDB カタログの作成</h5> <h6>SSIDB カタログの作成</h6> <p>Integration Services サーバーへパッケージを配置するには、事前に「SSIDB カタログ」(SSISDB データベース)を作成しておく必要があります。</p> <ol> <li>SSIDB カタログを作成するには、Integration Services サーバー上で Management Studio を起動して、[オブジェクト エクスプローラー]の[統合サービス カタログ]フォルダーを右クリックし、[カタログの作成]をクリックします。<br><a href="http://lh3.ggpht.com/-m4fG2PV61dg/UUm-JdPgazI/AAAAAAAAOUI/YjKY8S8F69w/s1600-h/image%25255B31%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-7vr55pilR5s/UUm-QXnZv8I/AAAAAAAAOUQ/igYyZkH23dM/image_thumb%25255B14%25255D.png?imgmax=800" width="216" height="145"></a> <li>[カタログの作成]ダイアログが表示されたら、[CLR 統合を有効にする]をチェックして、[パスワード]と[パスワードの再入力]へ暗号化のための任意のパスワードを入力して、[OK]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-osW8iYQv-M8/UUm-VXVXlMI/AAAAAAAAOUY/CyM929ybZkA/s1600-h/image%25255B35%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-g7HVOOAFQLA/UUm-coG13VI/AAAAAAAAOUg/ajV6_DJasaw/image_thumb%25255B16%25255D.png?imgmax=800" width="381" height="298"></a><br>これにより、SSISDB カタログが作成されます(SSISDB という名前のシステム データベースが自動作成されます)。</li></ol> <h6>SSIS パッケージ格納用のフォルダーの作成</h6> <p>次に、SSIS パッケージを格納するためのフォルダーを、SSISDB カタログ内に作成します。</p> <ol> <li>フォルダーを作成するには、次のように[統合サービス カタログ]を展開して、[SSISDB カタログ]を右クリックし、[フォルダーの作成]をクリックします。<br><a href="http://lh4.ggpht.com/-vFB6K5_wFu0/UUm-hV9RFDI/AAAAAAAAOUo/MUX15pB7XSs/s1600-h/image%25255B38%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-82PCEHBgeF0/UUm-oh-qIuI/AAAAAAAAOUw/F2MiN0P1kXw/image_thumb%25255B17%25255D.png?imgmax=800" width="192" height="160"></a> <li>[フォルダーの作成]ダイアログが表示されたら、[フォルダー名]へ任意のフォルダー名(画面は test)を入力して、[OK]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-cs_KTp9xW6E/UUm-tpSu0tI/AAAAAAAAOU4/PGjer1nUXK4/s1600-h/image%25255B41%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-rGJhxtVY8a0/UUm-0k1pqKI/AAAAAAAAOVA/OBtR94BydCY/image_thumb%25255B18%25255D.png?imgmax=800" width="244" height="113"></a></li></ol> <h5 id="id004">Integration Services サーバーへのパッケージの配置</h5> <h6>Integration Services サーバーへのパッケージの配置</h6> <p>次に、SSIS パッケージを Integration Services サーバーへ配置します。配置には、SQL Server Data Tools を利用します。</p> <ol> <li>SQL Server Data Tools でプロジェクト(SSISoyo2)を開き、次のように[ソリューション エクスプローラー]で、[プロジェクト名](SSISoyo2)を右クリックして、[配置]をクリックします。<br><a href="http://lh6.ggpht.com/-OzyklvTxaZM/UUm-5T1_kcI/AAAAAAAAOVI/IeIzzcmq2Og/s1600-h/image%25255B45%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-gTSBloBqxTg/UUm_Amot2TI/AAAAAAAAOVQ/QsS--uITYvU/image_thumb%25255B20%25255D.png?imgmax=800" width="251" height="120"></a> <li>これにより、[Integration Services 配置ウィザード]が起動するので、[次へ]ボタンをクリックします。 <li>次の[ソースの選択]ページでは、[プロジェクト配置ファイル]が選択されていることを確認して、[次へ]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-981k4JZK3Ag/UUm_FqkqkmI/AAAAAAAAOVY/ZJkArOjTTH8/s1600-h/image%25255B49%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-FceyAxM7xQg/UUm_MrmqR7I/AAAAAAAAOVg/RL0bvxJtKAE/image_thumb%25255B22%25255D.png?imgmax=800" width="329" height="154"></a> <li>次の[配置先の選択]ページでは、[サーバー名]へ配置先のサーバー名(Integration Services サーバーの名前)を入力します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8zpDw72mcyi2zuHxfkBY7Yiw0o8wXn_1zXKp7MJEz5opq4BKvBZOnQQWzBa2mUsuy1gND2vCMMjGkxq01NYW9LUubeLE5McUMZD6XXX8nAqW8_jsyzuQewHeMyX4BXdXdfTcXX_E2Rsby/s1600-h/image%25255B53%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-s1A_k8ZVcXA/UUm_ZRhapGI/AAAAAAAAOVw/xKXmZ-hSDG8/image_thumb%25255B24%25255D.png?imgmax=800" width="331" height="282"></a><br>続いて、[パス]の[参照]ボタンをクリックして、[フォルダーまたはプロジェクトの参照]ダイアログで、プロジェクトの配置先となるフォルダーとして、前の手順で作成したフォルダー(今回は、test)を選択して、[OK]ボタンをクリックします。 <li>[配置先の選択]ページへ戻ったら、[次へ]ボタンをクリックします。 <li>次の[確認]ページでは、これまで設定してきた内容が表示されます。確認後、[配置]ボタンをクリックして、配置を実行します。 <li>[結果]ページでは、配置が完了し、すべての[Result]が「成功」になったことを確認して、[閉じる]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-P1vrSkOBVGQ/UUm_eV4GYvI/AAAAAAAAOV4/cLCS2J7cbik/s1600-h/image%25255B56%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-FJ1Yhmei4JE/UUm_l3KflKI/AAAAAAAAOWA/plfoDSLDkSA/image_thumb%25255B25%25255D.png?imgmax=800" width="231" height="244"></a><br>これで、サーバーへの配置が完了です。</li></ol> <h6>配置したパッケージの確認</h6> <ol> <li>配置したパッケージを確認するには、Management Studio のオブジェクト エクスプローラーで SSISDB カタログを展開して、「test」フォルダーを展開します。<br><a href="http://lh4.ggpht.com/-2fK3I3D8evk/UUm_q7Db2eI/AAAAAAAAOWI/RPetbrXDz0I/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-2EoZo6geR0U/UUm_yGt5-OI/AAAAAAAAOWQ/TNAAcHTli-s/image_thumb%25255B26%25255D.png?imgmax=800" width="244" height="160"></a><br>「プロジェクト」フォルダー内に SSISoyo2 プロジェクトが配置され、「パッケージ」フォルダーには、パッケージ(Package.dtsx)が表示されていることを確認できます。</li></ol> <h6>Management Studio からの実行</h6> <p>パッケージが正しく配置されたことを確認するため、Management Studio から実行してみましょう。</p> <ol> <li>データの転送の結果を確認しやすくするために、まずは、次のように Management Studio のクエリ エディターから「ssisdb3」データベースの「ABC」テーブルに対して、TRUNCATE TABLE ステートメントを実行して、全行を削除しておきます。 <li>次に、オブジェクト エクスプローラーで[SSISDB カタログ]内の SSISoyo2 プロジェクトを展開して、パッケージ名(Package.dtsx)を右クリックし、[実行]をクリックします。<br><a href="http://lh3.ggpht.com/-9OrmU3s9Voc/UUm_20l2jkI/AAAAAAAAOWY/Bt_iugFRwHk/s1600-h/image%25255B64%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ifACxZpvhjM/UUm_-PzRUcI/AAAAAAAAOWg/OROCy0r6M5E/image_thumb%25255B29%25255D.png?imgmax=800" width="594" height="269"></a><br>[パッケージ実行]ダイアログが表示されたら、[OK]ボタンをクリックします。これにより、パッケージが実行されます。 <li>実行後、「概要レポートを今すぐ開きますか?」というメッセージが表示されるので、[はい]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/--RazIAF64Q0/UUnAFi4pZlI/AAAAAAAAOWs/Jb8nrgGEV-g/s1600-h/image%25255B68%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4NLSdCLKQ9Lw0L2TwrQkhEbz9oBLF1Iy1kVHNSV5XsI5BGxDPoa3hZYQJuOUqlyg2mxdyMcsU-p8165ut6NwVKWTUFaTrDQyANCDXaVxGxv7rHGL_XXWLJK4C4zST2LuUpYJ16gaAbmFD/?imgmax=800" width="355" height="96"></a> <li>これによって、実行したパッケージの概要レポートを表示することができます。<br><a href="http://lh3.ggpht.com/-H5nXUS81oZM/UUnASCdJcMI/AAAAAAAAOW8/leHoINcOFdU/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUEhv8sJy4FbK_dYSJusp2dqarsP8MKVs_Xd5nD_hzxGBpYnUagICQqzbuegK72hX74NQNbD7TVagk2iDP5MKwqP9SRNWcB3wYB_5eEx98b-lV9JT9fsILtLOsr2RJMAHnj_4ylixD6Z6U/?imgmax=800" width="590" height="464"></a><br>[状態]が「成功」になっていれば、実行が成功です。 <li>転送されたデータを確認するために、オブジェクト エクスプローラーで[ABC]テーブルを右クリックして、[上位 1000 行の選択]をクリックします。<br><a href="http://lh5.ggpht.com/-vN_2jztZxGU/UUnAenk935I/AAAAAAAAOXM/clbyrSXUaAU/s1600-h/image%25255B77%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-a7cgK-HEtno/UUnAmJAbk_I/AAAAAAAAOXU/bglcvIh9sDE/image_thumb%25255B36%25255D.png?imgmax=800" width="497" height="283"></a></li></ol> <h5 id="id005">別マシンへのパッケージの配置</h5> <h6>別マシンへのパッケージの配置</h6> <p>次に、Management Studio を利用して、パッケージを別のマシンへ配置します。ここからの手順は別のマシンが必要になりますが、同一マシンで試している方は、別マシンの部分を同一マシンへ置き換えて読み進めてください。また、同一マシンで試している場合は、手順 2~5の作業を省略してください。</p> <ol> <li>まずは、パッケージが利用するサンプル スクリプト内の「import」フォルダー(Foreach ループ コンテナーの読み取り対象のフォルダー)配下のファイルをすべて、別マシン上の任意のフォルダー(C:\import2 など)へコピーします。<br><a href="http://lh5.ggpht.com/-iI40KkaVmoU/UUnAqxM4coI/AAAAAAAAOXc/osuVt-XSMx0/s1600-h/image%25255B80%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ORiWzZwAUJ4/UUnAyKspOeI/AAAAAAAAOXk/3FHjfFL47DQ/image_thumb%25255B37%25255D.png?imgmax=800" width="244" height="100"></a> <li>次に、ファイルの転送先となるデータベース(ssisdb3)とテーブル(ABC)を別マシン上で作成しておきます。Management Studio のクエリ エディターを開いて、次のようにステートメントを記述し、実行しておきます。<br><a href="http://lh4.ggpht.com/-d-VqC40DQnI/UUnA2-vrGfI/AAAAAAAAOXs/ZgIOo-bAdC8/s1600-h/image%25255B84%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/--hubNz1waYQ/UUnA-EQIbwI/AAAAAAAAOX0/GikH0uXsK74/image_thumb%25255B39%25255D.png?imgmax=800" width="397" height="101"></a><br>なお、この手順は、同一マシンで試している場合は、省略してください。 <li>次に、配置先となる別マシン上にも SSISDB カタログを作成します。Management Studio の[オブジェクト エクスプローラー]で、配置先マシンの[統合サービス カタログ]フォルダーを右クリックして、[カタログの作成]をクリックします。<br>[カタログの作成]ダイアログが開いたら、[CLR 統合を有効にする]をチェックして、[パスワード]と[パスワードの再入力]へ暗号化のためのパスワードを入力し、[OK]ボタンをクリックします。<br>これにより、配置先の別マシンにも SSISDB カタログが作成されます。 <li>次に、[統合サービス カタログ]展開して、SSISDB カタログを右クリックし、[フォルダーの作成]をクリックします。<br><a href="http://lh6.ggpht.com/-zyP16yQY3Yc/UUnBDGvASFI/AAAAAAAAOX8/Y62E7atpmJI/s1600-h/image%25255B89%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-ctOnEmZG5a4/UUnBKDgipaI/AAAAAAAAOYE/59XfMXFCGdo/image_thumb%25255B42%25255D.png?imgmax=800" width="549" height="274"></a><br>[フォルダーの作成]ダイアログが表示されたら、[フォルダー名]へ任意のフォルダー名を入力して(画面は test2)、[OK]ボタンをクリックします。 <li>これで、配置先の別マシン(画面は SERVER1)にもパッケージを配置するためのフォルダーを作成することができました。<br><a href="http://lh5.ggpht.com/-nq2c3Aw49zQ/UUnBPKyHeLI/AAAAAAAAOYM/9N3dgNwx7bc/s1600-h/image%25255B92%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-n5gdTFft5qA/UUnBWeBGX6I/AAAAAAAAOYU/lR6CuaUSqVU/image_thumb%25255B43%25255D.png?imgmax=800" width="184" height="172"></a> <li>次に、Management Studio のオブジェクト エクスプローラーで、元のマシンに配置してあるパッケージの[プロジェクト]フォルダーを右クリックして、[プロジェクトの配置]をクリックします。<br><a href="http://lh5.ggpht.com/-pUtSWtdDTAA/UUnBbID7aJI/AAAAAAAAOYc/V_UYyrK4vfE/s1600-h/image%25255B97%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-DNUJHqangzY/UUnBiySbsPI/AAAAAAAAOYk/J-e6hky6U1w/image_thumb%25255B46%25255D.png?imgmax=800" width="482" height="317"></a><br>これにより、[Integration Services 配置ウィザード]が起動します。 <li>次の[ソースの選択]ページでは、配布対象となるパッケージの場所を指定します。ここでは、[Integration Services カタログ]をチェックして、[サーバー名]へパッケージが配置されているサーバーの名前(画面は moon)を入力し、[パス]で[参照]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-Hy1tNULlR74/UUnBn_SZvGI/AAAAAAAAOYs/fCvNwuZetrI/s1600-h/image%25255B101%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-iq0bVy9vcFg/UUnBvOHE6fI/AAAAAAAAOY0/ZRYx4M67y0Y/image_thumb%25255B48%25255D.png?imgmax=800" width="483" height="389"></a><br>[プロジェクトの参照]ダイアログが表示されたら、test フォルダーの SSISoyo2 プロジェクトを選択して、[OK]ボタンをクリックします。<br>[ソースの選択]ページへ戻ったら、[次へ]ボタンをクリックします。 <li>次の[配置先の選択]ページでは、配置先となるサーバー名とフォルダーを指定します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk2hMdfH32fxpPEtX1uWyBX8QQuloXrBuzxDc5vbVvHIYHwVC4-4Gfa7SJUIF-OaNl0hZvKqzjuIiMjewx02p6zDyO0kxhsKyi0FTzw9P9UY48ciLAitaeZvMGtBtmrq1DCHq4IsBoG7pN/s1600-h/image%25255B105%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-li17blOcEoc/UUnB7Qncq5I/AAAAAAAAOZE/OWpAVBkTK14/image_thumb%25255B50%25255D.png?imgmax=800" width="491" height="349"></a><br>[サーバー名]へ配置先となる別マシン名(画面は SERVER1)を入力して、[パス]の[参照]ボタンをクリックし、作成したフォルダー名(画面は test2)を指定して、[次へ]ボタンをクリックします。 <li>次の[確認]ページでは、これまで設定してきた内容を確認して、[配置]ボタンをクリックし、配置を実行します。<br><a href="http://lh4.ggpht.com/-LZlVYbow0rg/UUnCALLaiqI/AAAAAAAAOZM/EtKguObTsg0/s1600-h/image%25255B109%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-8TnZHOze8A0/UUnCHaRhJzI/AAAAAAAAOZU/Ky1FczA4NRQ/image_thumb%25255B52%25255D.png?imgmax=800" width="349" height="251"></a> <li>次の[結果]ページでは、すべての[Result]が「成功」となっていることを確認して[閉じる]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-1ipmpZ2ExRw/UUnCMFcgGSI/AAAAAAAAOZc/1E5l5CKcYoU/s1600-h/image%25255B113%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-xfOJn7DvLRY/UUnCTScd8NI/AAAAAAAAOZk/B0S9oREwdlA/image_thumb%25255B54%25255D.png?imgmax=800" width="351" height="137"></a> <li>次に、オブジェクト エクスプローラーで、配置先のマシンの[SSISDB カタログ]内の[test2]フォルダーを右クリックして、[最新の情報に更新]をクリックし、test2 フォルダー内に SSISoyo2 プロジェクトが配置されていることを確認します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpFFk7zOXNDP3zJInwjAwNbp29pgGYGlbPDR92KLb74MLcFwZ691_Msv8CM-jK0FYwt7v_76ddx9LSyTTads3R4z9Mnt8xwyAKCk0HQWXELHVaLknWaOlFWA07MdyazDC6E3sEeA7ySmf9/s1600-h/image%25255B117%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-OUHTpakQiM0/UUnCftoqfZI/AAAAAAAAOZ0/kO8G16_4gmw/image_thumb%25255B56%25255D.png?imgmax=800" width="347" height="151"></a> <li>次に、新しく配置した「SSISoyo2」プロジェクト内の「Package.dtsx」を右クリックして、[構成]をクリックし、構成の編集(パラメーター値の編集)を行います。<br><a href="http://lh4.ggpht.com/-dk22lVCzoEU/UUnCkXax0sI/AAAAAAAAOZ8/ItkiCc6tPQE/s1600-h/image%25255B121%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-UET3nVHaxGU/UUnCrkpYXMI/AAAAAAAAOaE/OQHtp9CRV9k/image_thumb%25255B58%25255D.png?imgmax=800" width="342" height="198"></a> <li>[構成]ダイアログが表示されたら、「サーバー名Ssidb3_ServerName」パラメーターの[...]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-E4iOOPlTie4/UUnCwT5YiII/AAAAAAAAOaM/jyMvyiwJNLM/s1600-h/image%25255B125%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-8E1sFAzqH58/UUnC3fwMa8I/AAAAAAAAOaU/amfppmS8s54/image_thumb%25255B60%25255D.png?imgmax=800" width="341" height="76"></a> <li>[パラメーター値の設定]ダイアログが表示されたら、[値]で[値を編集する]を選択して、別マシンのサーバー名(画面は SERVER1)を入力し、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-HA2KoXOGgUY/UUnC8SSKAqI/AAAAAAAAOac/hTukjtZukAQ/s1600-h/image%25255B129%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-zM0alG3g55s/UUnDDo8HdcI/AAAAAAAAOak/IBuIPcHAXjE/image_thumb%25255B62%25255D.png?imgmax=800" width="338" height="196"></a> <li>次に、[構成]ダイアログへ戻ったら、「importPath」パラメーターの[...]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-pHQoFr9PCmw/UUnDIa9lUBI/AAAAAAAAOas/yy5O50US5pE/s1600-h/image%25255B133%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-62wuhBdUrSM/UUnDPpNNXhI/AAAAAAAAOa0/0JdDX3hx9_M/image_thumb%25255B64%25255D.png?imgmax=800" width="451" height="101"></a> <li>[パラメーター値の設定]ダイアログが表示されたら、[値]で[値を編集する]を選択して、「import」フォルダーをコピーしたローカル パス(画面はC:\import2)へ変更し、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-jCFJzZx_k38/UUnDUmCgL5I/AAAAAAAAOa8/ZqgPI-0tVOo/s1600-h/image%25255B137%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-4r2kOm4e_Dw/UUnDbmTiDLI/AAAAAAAAObE/3E9fPGvOuso/image_thumb%25255B66%25255D.png?imgmax=800" width="447" height="214"></a> <li>[構成]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>最後に、パッケージが正しく実行できるかどうかを試してみましょう。<br>オブジェクト エクスプローラーで、別マシン(画面は SERVER1)へ配置した SSISoyo2 プロジェクト内の Package.dtsx パッケージを右クリックして、[実行]をクリックします。<br><a href="http://lh5.ggpht.com/-5wrY1ippueY/UUnDgsNFsWI/AAAAAAAAObM/SwIisCwsnUU/s1600-h/image%25255B141%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-dw-tYKYywCQ/UUnDnuaNbDI/AAAAAAAAObU/gNu8tsmb86Q/image_thumb%25255B68%25255D.png?imgmax=800" width="356" height="203"></a> <li>[パッケージの実行]ダイアログが表示されたら、パラメーターの値が、前の手順で編集した値へ変更されていることを確認して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-_7N09iSbOLM/UUnDshE4p5I/AAAAAAAAObc/xOooYC_Co1I/s1600-h/image%25255B145%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-TbYhrJR_iOs/UUnDz8WZbpI/AAAAAAAAObk/82rEB2IWuaI/image_thumb%25255B70%25255D.png?imgmax=800" width="359" height="247"></a><br>これにより、パッケージが実行されます。 <li>実行後、「概要レポートを今すぐ開きますか?」メッセージが表示されたら、[はい]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-mytrrpgfuuI/UUnD4nnvKqI/AAAAAAAAObs/YOylfJ8XfFM/s1600-h/image%25255B149%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-SG6akjYBuxw/UUnD_026fCI/AAAAAAAAOb0/PI_n9mSakGQ/image_thumb%25255B72%25255D.png?imgmax=800" width="357" height="91"></a> <li>これにより、パッケージの概要レポートが表示されて、実行状態が「成功」であることを確認できれば、実行が成功です。<br><a href="http://lh4.ggpht.com/-GMO8v3ZdTT4/UUnEEj_w6EI/AAAAAAAAOb8/RP6kwrd5uUY/s1600-h/image%25255B153%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Ib30wcfdOZI/UUnEL_A2k8I/AAAAAAAAOcE/GjF5R2Omrng/image_thumb%25255B74%25255D.png?imgmax=800" width="358" height="227"></a> <li>完了後、Management Studio から「ssisdb3」データベースの「ABC」テーブルを開き、データが追加されていることを確認しておきます。<br><a href="http://lh3.ggpht.com/-FQPcEaXC5-M/UUnEQ8penhI/AAAAAAAAOcM/BA9_D2nmRoM/s1600-h/image%25255B157%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOZPwB4S05ehPUL9Y73ycW5Y-yHzgicMuoe91ft-sFtaUpexO1eoZBaE4qKOHXf1OS2Xq9rbkAo8XP4HWXIyTp2h0qiycpSdDDDVqT1x0Dbol-Js3cLyEyavVLq2lTrRUvvLVqrLf91P9C/?imgmax=800" width="362" height="226"></a><br>以上で、パッケージを別マシンへ配置する手順が完了です。このように、パラメーター化機能を利用することで、開発機と本番機でマシン構成が異なる場合でも容易に構成を変更でき、パッケージを実行できるようになるので、大変便利です。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-58490790696766879572013-03-20T13:37:00.001+09:002013-03-20T23:27:32.408+09:00◆Foreach ループと ブレークポイント<ul> <li><a href="#id001">Foreach Loop コンテナーによる複数ファイルの読み取り</a> <li><a href="#id002">ブレークポイントを利用したステップ実行</a> </li></ul> <h5 id="id001">Foreach Loop コンテナーによる複数ファイルの読み取り</h5> <p>Foreach Loop コンテナーを利用すると、同じフォルダー内の複数のファイルのデータをまとめて読み取って、転送させることができるようになります。この Step では、前の Step で作成した「SSISoyo2」プロジェクトへ Foreach Loop コンテナーを追加して、サンプル スクリプトにある「import」フォルダー内のテキスト ファイルをすべて取り込む手順を説明します。</p> <h6>転送するデータ</h6> <p><a href="http://lh3.ggpht.com/-yZ_IXLtQAtw/UUk3yyNGJkI/AAAAAAAAOLg/QZYZE1K1tA8/s1600-h/image4.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-tDzMIlzhW4U/UUk36gxb-VI/AAAAAAAAOLo/DQ9w35ypACY/image_thumb2.png?imgmax=800" width="613" height="357"></a></p> <h6>転送後のデータ</h6> <p><a href="http://lh4.ggpht.com/-9RsYzEV3CmY/UUk3_irURzI/AAAAAAAAOLw/63tOwWl946w/s1600-h/image8.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-iWWD_1tYcH4/UUk4G5DcbaI/AAAAAAAAOL4/dd2-yFG3LPA/image_thumb4.png?imgmax=800" width="310" height="355"></a></p> <h6>作成するパッケージ</h6> <p><a href="http://lh3.ggpht.com/-b3S5CA8kIcY/UUk4NzZ6xtI/AAAAAAAAOMA/eHll56YWoaA/s1600-h/image14.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-S8PYiinZ0-s/UUk4VN5z-NI/AAAAAAAAOMI/cgRCEZBodVU/image_thumb8.png?imgmax=800" width="631" height="289"></a></p> <h6>Foreach Loop コンテナーの配置</h6> <ol> <li>Foreach ループ コンテナーを利用するには、次のように[制御フロー]タブで、SSIS ツールボックスの[コンテナー]カテゴリの中から[Foreach ループ コンテナー]をドラッグ&ドロップして配置します。<br><a href="http://lh3.ggpht.com/-XHNmNdpkEGs/UUk4aFbF2tI/AAAAAAAAOMQ/wrnNA8uWp_E/s1600-h/image18.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-qWgp_r3-eZ4/UUk4hTsCPHI/AAAAAAAAOMY/Y1IlMSR-Dq0/image_thumb10.png?imgmax=800" width="589" height="189"></a> <li>次に、[データ フロー タスク]を[Foreach ループ コンテナー]の枠の中へドラッグ&ドロップして移動します。<br><a href="http://lh4.ggpht.com/-cw9Jlu6QAKM/UUk4mCyiv3I/AAAAAAAAOMg/KSygZHzVpCw/s1600-h/image23.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ew88Uv-ZQlg/UUk4tbySUNI/AAAAAAAAOMo/QRLAFFUdyb8/image_thumb13.png?imgmax=800" width="586" height="167"></a> <li>次に、Foreach ループ コンテナーの対象(繰り返し読み取るデータ)を設定するために[Foreach ループ コンテナー]をダブル クリックして、[Foreach ループ エディター]ダイアログを開きます。<br><a href="http://lh5.ggpht.com/-KK-5mVvMFEI/UUk4yUE6RaI/AAAAAAAAOMw/GJWA-zFH7Ic/s1600-h/image28.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-7OI_oE0XeQE/UUk45Zvz4QI/AAAAAAAAOM4/FWoZaUnXegc/image_thumb16.png?imgmax=800" width="581" height="312"></a> <li>このダイアログでは、[コレクション]ページをクリックします。<br>今回はサンプル スクリプトの中にある「import」フォルダーのテキスト ファイルを対象とするので、次のように[Enumerator](列挙子)で「Foreach File 列挙子」を選択し、[フォルダー]で[参照]ボタンをクリックます。<br><a href="http://lh6.ggpht.com/-sVrZ1GZ1Lh4/UUk4-hQaR6I/AAAAAAAAONA/U_hFjwcOAkM/s1600-h/image33.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-_OuZWjmQSLc/UUk5F8wqWbI/AAAAAAAAONI/5hwx7_zrtX0/image_thumb19.png?imgmax=800" width="579" height="313"></a><br>[フォルダーの参照]ダイアログが表示されたら、サンプル スクリプト内にある「import」フォルダーを選択して、[OK]ボタンをクリックします。 <li>[Foreach ループ エディター]へ戻ったら、次のように[ファイル]へ「*.txt」と入力します。<br><a href="http://lh4.ggpht.com/-SFuVq7fFFEA/UUk5K0uPMSI/AAAAAAAAONQ/6Xexb_WE0qU/s1600-h/image37.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-brLwhfpRU2s/UUk5SDJkpaI/AAAAAAAAONY/XYwM9xrFxtQ/image_thumb21.png?imgmax=800" width="379" height="234"></a><br>これで、import フォルダー内の拡張子が「.txt」のファイルをすべて読み取れるようになります。 <li>次に、読み取ったファイルの名前(パス)を変数へ割り当てるために、次のように[変数のマッピング]ページをクリックして開き、[変数]で「新しい変数」を選択します。<br><a href="http://lh5.ggpht.com/-QD9Cp4eVORE/UUk5W0pEaQI/AAAAAAAAONg/F_53-GyIxp4/s1600-h/image42.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-dau8Kca5M10/UUk5eZZfA3I/AAAAAAAAONo/sXG-3owaVfQ/image_thumb24.png?imgmax=800" width="577" height="248"></a><br>これにより、[変数の追加]ダイアログが表示されるので、[名前]へ任意の変数名(varFileName など)を入力して、[OK]ボタンをクリックします。 <li>[Foreach ループ エディター]へ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、設定した変数(Foreach ループ タスクで読み取ったファイルのパス)を転送元のファイルとして指定するために、次のように画面下部の[接続マネージャー]タブで「テキストファイルへの接続」をクリックします。<br><a href="http://lh5.ggpht.com/-zZ27Xl1TKHg/UUk5jHJLDUI/AAAAAAAAONw/erYyZmT6ZNw/s1600-h/image47.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-BQd9dU2eXj0/UUk5qRpcuPI/AAAAAAAAON4/wqg1fici1ug/image_thumb27.png?imgmax=800" width="575" height="142"></a><br>この「テキストファイルへ接続」は、「フラット ファイル ソース」で利用していた「MISS.txt」ファイルへの接続に利用していたものです。<br>続いて、[プロパティ]ウィンドウで、[Expressions]を選択して、「...」ボタンをクリックします。 <li>これにより、[プロパティ式エディター]ダイアログが表示されるので、次のように[プロパティ]で「ConnectionString」を選択して、「...」ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-p6yFeb9yvKc/UUk5vIjMPvI/AAAAAAAAOOA/ewdsSwEN3Mk/s1600-h/image51.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-9sflSBH48cs/UUk52qaUHTI/AAAAAAAAOOI/Z-MxGz1tXd4/image_thumb29.png?imgmax=800" width="575" height="320"></a><br>[式ビルダー]ダイアログが表示されたら、[変数]フォルダーを展開して表示される変数の一覧の中から「User::varFileName」を[式]へドラッグ&ドロップして、[OK]ボタンをクリックします。 <li>[プロパティ式エディター]へ戻ったら、[OK]ボタンをクリックして閉じます。<br>これにより、フラット ファイル ソースの接続先(ConnectionString)を変数(Foreach ループ タスクで読み取ったファイルのパス)へ割り当てることができます。 <li>データの転送の結果を確認しやすくするために、まずは、次のように Management Studio のクエリ エディターから「ssisdb3」データベースの「ABC」テーブルに対して、TRUNCATE TABLE ステートメントを実行して、全行を削除しておきます。<br><a href="http://lh5.ggpht.com/-pDpts5cPL2Y/UUk57XKqGxI/AAAAAAAAOOQ/FMDuWfR0Cb8/s1600-h/image54.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-9B0p6up_wJE/UUk6ComEkRI/AAAAAAAAOOY/p_wl8DaDB3o/image_thumb30.png?imgmax=800" width="202" height="127"></a> <li>SSIS デザイナーへ戻り、パッケージをデバッグ実行します。<br><a href="http://lh6.ggpht.com/-4S68WlyekDQ/UUk6HcV3UTI/AAAAAAAAOOg/CR5qRPBkITY/s1600-h/image57.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Mcrxm4yB-QI/UUk6Ox7HpNI/AAAAAAAAOOo/33h2C1sJ6O8/image_thumb31.png?imgmax=800" width="184" height="98"></a><br>すべてのタスクに緑のチェックマークが付いて、データの転送が成功していることを確認できます。 <li>確認後、デバッグを終了します。 <li>転送されたデータを確認するために、Management Studio から、[ABC]テーブルを右クリックして、[上位 1000 行の選択]をクリックします。<br><a href="http://lh6.ggpht.com/-sHsCKnsUI80/UUk6Tvg-weI/AAAAAAAAOOw/z4-D431AG_A/s1600-h/image63.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-sSlfBRc6ilU/UUk6bCZ1k_I/AAAAAAAAOO4/K13_40bgiS4/image_thumb35.png?imgmax=800" width="489" height="245"></a><br>4 つのファイルのデータがすべて追加されていることを確認できます。このように、Foreach ループ コンテナーを利用すると、複数のファイルをまとめて転送することができるので、大変便利です。 <li>最後に、[ファイル]メニューの[すべてを保存]をクリックして、SSISoyo2 プロジェクトを保存しておきます。</li></ol> <h5 id="id002">ブレークポイントを利用したステップ実行</h5> <h6>ブレークポイントを利用したステップ実行</h6> <p>SSIS デザイナーでは、ブレークポイントを設定して、タスクを 1 つ 1 つステップ実行することも可能です。これを利用すると、Foreach ループ コンテナーが読み取ったファイル名(変数)などを確認することができるので、大変便利です。<br>それでは、これを試してみましょう。</p> <ol> <li>ブレークポイントを設定するには、次のように[データ フロー タスク]を選択し、[デバッグ]メニューの[ブレークポイントの設定/解除]をクリックします。<br><a href="http://lh5.ggpht.com/-E1eOUyhHBp4/UUk6gMt7GjI/AAAAAAAAOPA/YjqtegzR51A/s1600-h/image67.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Olh13jBVQsw/UUk6nLXJpII/AAAAAAAAOPI/HGYghxsINaE/image_thumb37.png?imgmax=800" width="486" height="197"></a><br>これにより、[データ フロー タスク]へ "赤丸" のアイコンが追加されます。 <li>ブレークポイントの効果を確認するために、パッケージをデバッグ実行します。<br>すると、次のように[Foreach ループ コンテナー]が処理中に変わって、データ転送が一時的に中断されます。<br><a href="http://lh4.ggpht.com/-VxkejIf3FjU/UUk6sNE-CFI/AAAAAAAAOPQ/O_StABJu87U/s1600-h/image71.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-pOjZplDoAPw/UUk6zjOuaLI/AAAAAAAAOPY/Jch33b4FDoI/image_thumb39.png?imgmax=800" width="258" height="445"></a><br>同時にブレークポイントの赤丸アイコンに黄色の矢印が追加されて、ここで停止していることが分かります。変数の値を確認するには、この状態で、画面左下の[ローカル]タブをクリックして、[ローカル]ウィンドウを開き、[Variables](変数)を展開します。[ローカル]タブが表示されない場合は、[デバッグ]メニューの[ウィンドウ]から[ローカル]をクリックします。<br>続いて、[Variables]に表示される一覧の中から、[User::varFileName]を展開して[Value]の値を確認します。<br><a href="http://lh5.ggpht.com/-sn10MbscxKk/UUk64gZ1iII/AAAAAAAAOPg/vm_njLj4r2c/s1600-h/image75.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-c7BeOMsbBtw/UUk6_6fbsQI/AAAAAAAAOPo/TdAyOgLMPIk/image_thumb41.png?imgmax=800" width="490" height="148"></a><br>すると、「C:\\SampleScript\\import\\ABC.txt」のように表示され、サンプル スクリプトの import フォルダーから「ABC.txt」ファイルを読み取っていること(現在の「User::varFileName」変数の値)が分かります。 <li>次に、データの転送を続行するために[デバッグ]メニューの[続行]をクリックします。<br><a href="http://lh4.ggpht.com/-qSS4Zv4aMPA/UUk7E4BXZwI/AAAAAAAAOPw/sizHsYHHqEY/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Tt-H8-MtHlU/UUk7MOnpxtI/AAAAAAAAOP4/Nye_KJulgvE/image_thumb.png?imgmax=800" width="221" height="103"></a> <li>すると、[データ フロー タスク]には緑のチェックマークが付きますが、再びブレークポイントで、動作が一時的に中断します。<br><a href="http://lh5.ggpht.com/-aToS21ef8-I/UUk7Q5lMAqI/AAAAAAAAOQA/WhVa2oN_rtI/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheM-7g_u2unJ0ST8ERYUfo-bzfg8-O5p4niAdpXbmpCo8J3WPz0EWNgRQtS2Z_kQtUTFdz3zszunmpRJaej6lytQyGoniAgpKoMTA_Mnq02OtM1GeQcp2THGtckkjBRhmjJsaJqsW5eTxV/?imgmax=800" width="364" height="308"></a><br>[ローカル]ウィンドウの[User::varFileName]の[Value]値を確認すると、今度は、「C:\\SampleScript\\import\\DEF.txt」のように表示されて、2つ目のファイルが読み取られていることを確認できます。 <li>引き続き、データの転送を続行するために[デバッグ]メニューの[続行]をクリックします。<br><a href="http://lh3.ggpht.com/-TQCEho3PTJ8/UUk7dL5x99I/AAAAAAAAOQQ/Xo5nx7PGSkE/s1600-h/image%25255B10%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-oE6rfiHFZIQ/UUk7kg72l4I/AAAAAAAAOQY/trmgV029Zog/image_thumb%25255B3%25255D.png?imgmax=800" width="217" height="225"></a><br>今度は、3つ目のファイル「GHI.txt」を読み取っているところで止まります。<br><a href="http://lh3.ggpht.com/-NlXSbacsLH4/UUk7pVdMMdI/AAAAAAAAOQg/Woa79Lwfl4Y/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-zEhsV-hqLbQ/UUk7w5mX8xI/AAAAAAAAOQo/LJxNAh2RMzI/image_thumb%25255B5%25255D.png?imgmax=800" width="357" height="326"></a> <li>もう一度、データの転送を続行するために[デバッグ]メニューの[続行]をクリックします。<br>今度は、4つ目のファイル「MISS.txt」を読み取っているところで止まります。<br><a href="http://lh4.ggpht.com/-kMFfYmgw7Tk/UUk715y_S9I/AAAAAAAAOQw/SZPUh3ubz7Y/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-XQzxCq7ojgM/UUk79KK_jcI/AAAAAAAAOQ4/SVJ2nuB7Tj4/image_thumb%25255B7%25255D.png?imgmax=800" width="358" height="324"></a> <li>さらに、[デバッグ]メニューの[続行]をクリックします。<br><a href="http://lh5.ggpht.com/-MPTvi6ejzqk/UUk8B1vYZkI/AAAAAAAAORA/qvUTIqQKaVs/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-3ywRYMurwB4/UUk8JOjO3dI/AAAAAAAAORI/0pDNPA14IhI/image_thumb%25255B8%25255D.png?imgmax=800" width="178" height="127"></a><br>すべてのタスクに緑のチェックマークが付くと、データ転送がすべて完了です。 <li>すべての動作を確認後、デバッグを終了します。<br>以上のように、ブレークポイントを利用すると、タスクを 1つ 1つステップ実行できるようになって、また変数の値も確認できるので、大変便利です。</li></ol> <h6>ブレークポイントの削除</h6> <ol> <li>設定したブレークポイントを削除したい場合は、次のように[デバッグ]メニューの[すべてのブレークポイントの削除]をクリックします。<br><a href="http://lh6.ggpht.com/-wz1LIyFRPUc/UUk8OJdA-5I/AAAAAAAAORQ/TOUM7z4fFeo/s1600-h/image%25255B25%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hYtn3ERy3yc/UUk8VQlChFI/AAAAAAAAORY/t-QS8NCioTQ/image_thumb%25255B10%25255D.png?imgmax=800" width="463" height="237"></a> <li>最後に、[ファイル]メニューの[すべてを保存]をクリックして、SSISoyo2 プロジェクトを保存しておきます。このプロジェクトは、次の Step でも引き続き使用します。 </li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-24860119103986689172013-03-20T00:05:00.001+09:002013-03-20T00:09:49.329+09:00◆エラーコンポーネントの利用<ul> <li><a href="#id001">作成するパッケージの概要</a> <li><a href="#id002">SSISoyo2 プロジェクトの動作確認</a> <li><a href="#id003">エラーを無視する</a> <li><a href="#id004">エラー情報をファイルへ書き込む</a> <li><a href="#id005">スクリプト コンポーネント(C#)によるエラー メッセージの取得</a> </li></ul> <h5 id="id001">作成するパッケージの概要</h5> <h6>作成するパッケージの概要</h6> <p>この STEPでは、データ転送時に起こりがちな、転送元のデータにイレギュラーな値が入っていた場合に発生するエラーの対処方法について説明します。前の Step で作成したエラーの発生するパッケージ(SSISoyo2 プロジェクト)を利用して、エラーの対処方法を試していきます。</p> <h6>SSISoyo2 プロジェクトの内容</h6> <p>この Step の実習を行うには、事前に Step3 と Step4 を行い、SSISoyo2 プロジェクトを作成しておく必要があります。SSISoyo2 プロジェクトのパッケージは、次のようなテキスト ファイル(タブ区切り)のデータを SQL Server へ転送し、データ型のエラー(文字と数値が混在している場合のエラー)を発生させるものです。</p> <h6>転送するファイル</h6> <p><a href="http://lh6.ggpht.com/-J5nmglzCS2Q/UUh3c5ueAKI/AAAAAAAAOB4/t_jIc9MgNcQ/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-A1zI8Ehoxj4/UUh3kduQjYI/AAAAAAAAOCA/gk7FlzBqsY0/image_thumb%25255B1%25255D.png?imgmax=800" width="458" height="206"></a></p> <h6>作成するパッケージ</h6> <p>この Step では、SSISoyo2 プロジェクトを、次のように変更していきます。</p> <p><a href="http://lh6.ggpht.com/-2vRA-ua3aD0/UUh3pUR0ShI/AAAAAAAAOCI/K7ToSuNId4Y/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-FPWinYvT4Vc/UUh3whG7HNI/AAAAAAAAOCQ/PA-y7adg5HY/image_thumb%25255B3%25255D.png?imgmax=800" width="635" height="264"></a></p> <h5 id="id002">SSISoyo2 プロジェクトの動作確認</h5> <h6>SSISoyo2 プロジェクトの動作確認(エラーが発生することの確認)</h6> <p>まずは、前の Step で作成した SSISoyo2 プロジェクトのパッケージを実行して、動作確認をします。</p> <ol> <li>パッケージの実行結果を確認しやすくするために、転送先の ssisdb3 データベース内にある ABC テーブルを一度空にしておきます。<br><a href="http://lh6.ggpht.com/-X_Hke7173ko/UUh31no7ShI/AAAAAAAAOCY/4zOqRxQ_R7A/s1600-h/image%25255B10%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihfSqQihQ_kkYBxynL6dAIv1tEvgcerhEqH-NO4A9vS3cqh_s9EoZRoZjPzlLMrgo-NtxYdBBFHvlEWkSO-J0lR0pwTGaEFjFSOvMWEV1T1VfYzQpa9KNzmq6zzoqXIr-xPM9YNlZIufX7/?imgmax=800" width="243" height="150"></a> <li>次に、パッケージの[データ フロー]タブを開いてから、デバッグを実行します。<br><a href="http://lh4.ggpht.com/-6NFtM2T5188/UUh4ByxUsXI/AAAAAAAAOCo/GiDn1lMqr4k/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ehRykm7MtHI/UUh4JG3QOdI/AAAAAAAAOCw/ZdZjh3Tu2vU/image_thumb%25255B6%25255D.png?imgmax=800" width="570" height="174"></a><br>配置している[フラット ファイル ソース]が実行中に変わって、前の Step で作成した OnError イベント ハンドラーが実行されて、エラー番号とエラーの説明がメッセージ ボックスで表示されることを確認できます。<br>このメッセージ ボックスは、エラー(OnError イベント)が発生した数の分だけ表示されるので、その分[OK]ボタンをクリックします。 <li>すべてのメッセージ ボックスが表示され、[フラット ファイル ソース]がエラーになって、転送が失敗したことを確認できます。<br><a href="http://lh4.ggpht.com/-bKCDag55S3c/UUh4OJhLzhI/AAAAAAAAOC4/cU16XkjaZY0/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-AqNA0xIfW3c/UUh4VaZF1bI/AAAAAAAAODA/l9qUQ7_dWkc/image_thumb%25255B7%25255D.png?imgmax=800" width="150" height="122"></a><br>確認後、デバッグを終了します。 <li>次に、Management Studio から、[ABC]テーブルを右クリックして、[上位 1000 行の選択]をクリックし、データを確認します。<br><a href="http://lh4.ggpht.com/-chmapqAf9eQ/UUh4aTOCupI/AAAAAAAAODI/HTEjcUpc31o/s1600-h/image%25255B21%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-cFnN4xbSWbY/UUh4hd6r4FI/AAAAAAAAODQ/dW5u-pGLREY/image_thumb%25255B9%25255D.png?imgmax=800" width="293" height="152"></a><br>データ転送が失敗しているので、MISS.txt ファイルのデータは追加されていないことを確認できます。</li></ol> <h5 id="id003">エラーを無視する</h5> <h6>エラーを無視する</h6> <p>SSIS パッケージには、データ転送時にエラーが発生した場合でも、エラーを無視してデータ転送を続行させる機能があります。それでは、これを試してみましょう。</p> <ol> <li>エラーを無視させるには、次のように配置した[フラット ファイル ソース]をダブル クリックして[フラット ファイル ソース エディター]ダイアログを表示します。<br><a href="http://lh4.ggpht.com/-vF9KcWD88YY/UUh4mX5qzuI/AAAAAAAAODY/EqDNUUon1WY/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-deMSP-sLs9k/UUh4trQxbZI/AAAAAAAAODg/PrkA020h3fw/image_thumb%25255B12%25255D.png?imgmax=800" width="532" height="219"></a> <li>このダイアログでは、次のように[エラー出力]ページをクリックして、[列0]の[エラー]ドロップダウン リストを「エラーを無視する」へ変更し、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-GKYm6LAZ57g/UUh4yoEGirI/AAAAAAAAODo/EAAarKOurCo/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-l4zmJiOyUd0/UUh455rw2AI/AAAAAAAAODw/S85Ei_sJG_Y/image_thumb%25255B14%25255D.png?imgmax=800" width="530" height="160"></a> <li>エラーが無視されることを確認するために、パッケージをデバッグ実行します。<br><a href="http://lh3.ggpht.com/-3AO4BLT77UM/UUh4-xqL4GI/AAAAAAAAOD4/rpmoLzfWZMw/s1600-h/image%25255B33%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-3p8BeFfmG4A/UUh5GP6bMaI/AAAAAAAAOEA/PJ4C-WVbfl8/image_thumb%25255B15%25255D.png?imgmax=800" width="203" height="172"></a><br>エラーが無視されることを確認するために、パッケージをデバッグ実行します。<br>今度は、すべてのコンポーネントに緑のチェックマークが付いて、データ転送が成功していることを確認できます。 <li>確認後、デバッグを終了します。 <li>転送されたデータを確認するために、Management Studio から、「ABC」テーブルを右クリックして、[上位 1000 行の選択]をクリックします。<br><a href="http://lh3.ggpht.com/-XlPnLy_R8Zw/UUh5LB9KHYI/AAAAAAAAOEI/K__rtsYw_JQ/s1600-h/image%25255B37%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-KpgR-CSnKW8/UUh5SQwXZZI/AAAAAAAAOEQ/ebLHDyBXN9A/image_thumb%25255B17%25255D.png?imgmax=800" width="519" height="156"></a><br>MISS.txt のデータが追加されていることを確認できますが、「列0」列の文字データは「NULL」として追加され、正しいデータ(7, ZZZZ)はきちんと転送されていることを確認できます。<br><a href="http://lh5.ggpht.com/-_-I0zPN1ZQs/UUh5XSOHxNI/AAAAAAAAOEY/3YsxXrklzm8/s1600-h/image%25255B41%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-KoqN-fXwDOE/UUh5egY_gGI/AAAAAAAAOEg/mvxqVhrkF50/image_thumb%25255B19%25255D.png?imgmax=800" width="518" height="85"></a></li></ol> <p> </p> <h5 id="id004">エラー情報をファイルへ書き込む</h5> <h6>エラー情報をファイルへ書き込む</h6> <p>次に、「列0」でエラーが発生したときのエラー情報(エラーが発生した行データとエラー情報)をファイルへ書き込むようにパッケージを変更してみましょう。</p> <ol> <li>エラー情報をファイルへ書き込むようにするには、次のように SSIS ツールボックスから[その他の変換先]カテゴリにある[フラット ファイル変換先]をドラッグ&ドロップして配置します。<br><a href="http://lh4.ggpht.com/-jZZeSE2ZyfY/UUh5jkVZ8YI/AAAAAAAAOEo/sHeM2moujzM/s1600-h/image%25255B45%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-w-MR1xqJung/UUh5q-TJwtI/AAAAAAAAOEw/uQFhZirgQq8/image_thumb%25255B21%25255D.png?imgmax=800" width="534" height="231"></a> <li>次に、[フラット ファイル ソース]でエラーが発生した場合に、[フラット ファイル変換先]へ転送されるように、次のように[フラット ファイル ソース]をクリックして表示される "赤い矢印" を[フラット ファイル変換先]まで伸ばします。<br><a href="http://lh4.ggpht.com/-rrJaJYjwxf4/UUh5v4iYf0I/AAAAAAAAOE4/bsJgYK1MvGM/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-LtKQzKYIVL8/UUh53APeC4I/AAAAAAAAOFA/V4CFQtgGWKQ/image_thumb%25255B24%25255D.png?imgmax=800" width="602" height="238"></a><br>これにより、[エラー出力の構成]ダイアログが表示されるので、[列0]の[エラー]列を「エラーを無視する」から「行のリダイレクト」へ変更します。これで、「列0」列でエラーが発生した場合に、エラー情報を別のコンポーネントへリダイレクト(転送)できるようになります。 <li>次に、[フラット ファイル変換先]をダブル クリックして、リダイレクトされたエラー情報を書き込むファイルを設定します。<br><a href="http://lh4.ggpht.com/-vUpRKpqiGJY/UUh58Flr3MI/AAAAAAAAOFI/YYBzcHz_fRc/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-G66392gara4/UUh6DfAzCkI/AAAAAAAAOFQ/KXZxuYxa4XA/image_thumb%25255B26%25255D.png?imgmax=800" width="610" height="183"></a><br>[フラット ファイル変換先エディター]ダイアログが表示されたら、[新規作成]ボタンをクリックします。 <li>これにより、次のように[フラット ファイル形式]ダイアログが表示されて、書き込むファイルの書式を問われるので、「区切り記号」(カンマ区切り)が選択されていることを確認して、[OK]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-BviC0EC4AP8/UUh6IPtSlNI/AAAAAAAAOFY/L-F0lWy-MCE/s1600-h/image%25255B57%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-CYUZZhl3I9Q/UUh6PcURoVI/AAAAAAAAOFg/nQO21RxSZao/image_thumb%25255B27%25255D.png?imgmax=800" width="243" height="107"></a> <li>次に、[フラット ファイル接続マネージャー エディター]ダイアログが表示されるので、今回は、次のように[接続マネージャー名]へ「エラー情報の書き込み_errOut」、[ファイル名]へ「C:\errOut.txt」と入力して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-mdetPbgtgKA/UUh6USi35zI/AAAAAAAAOFo/__KUlHufUMg/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-XX4zzN7wv5s/UUh6bm6QImI/AAAAAAAAOFw/nveKhkJ23tk/image_thumb%25255B29%25255D.png?imgmax=800" width="386" height="162"></a> <li>[フラット ファイル変換先エディター]ダイアログへ戻ったら、[マッピング]ページをクリックして、ファイルへ書き込むデータのマッピングを設定します。<br><a href="http://lh4.ggpht.com/-MrwuwE5Zpo0/UUh6gc8yTcI/AAAAAAAAOF4/i3_JHl4U8JE/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Nox1ucMMWn8/UUh6nks6FmI/AAAAAAAAOGA/M50P-E1pKf4/image_thumb%25255B31%25255D.png?imgmax=800" width="389" height="129"></a><br>受け取るデータ(使用できる入力列)には、[フラット ファイル ソースのエラー出力列](エラーが発生した該当行)と、[ErrorCode](エラー番号)、[ErrorColumn](エラー列)があり、エラーに関する情報が転送されてくることを確認できます。この転送されたデータをそのままファイルへ書き込むように、各列が線で結ばれている(マッピングされている)ことを確認して、[OK]ボタンをクリックします。 <li>ここまでの設定を確認するために、パッケージをデバッグ実行します。<br><a href="http://lh6.ggpht.com/-XcNvJxO853g/UUh6s34ersI/AAAAAAAAOGI/4lVhbKbM-N4/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-7jVqWS2k_Mw/UUh60AYq0wI/AAAAAAAAOGQ/wJG6neNZGqk/image_thumb%25255B33%25255D.png?imgmax=800" width="378" height="154"></a><br>すべてのコンポーネントに緑のチェックマークが付いて、データ転送が成功していることを確認できます。また、青色の矢印(成功時の処理)には「1 行」、赤色の矢印(エラー時の処理)には「2 行」と表示されて、成功が 1 行、エラーが 2 行であったことを確認することができます。 <li>確認後、デバッグを終了します。 <li>転送されたデータを確認するには、Management Studio から、「ABC」テーブルを右クリックして、[上位 1000 行の選択]をクリックします。<br><a href="http://lh5.ggpht.com/-_S_JH522v-s/UUh65H-gS4I/AAAAAAAAOGY/bp3R2MwPMh0/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-9rDVYCdSHU0/UUh7AOcS-oI/AAAAAAAAOGg/B8yDkmPdEh4/image_thumb%25255B35%25255D.png?imgmax=800" width="381" height="101"></a><br>今度は、正しいデータのみが追加され、エラーが発生した行は追加されていないことを確認できます。 <li>次に、Windows エクスプローラーから C:\errOut.txt ファイルを開き、エラー情報を取得できたかどうかを確認します。<br><a href="http://lh5.ggpht.com/-_AbLpjHC8aE/UUh7FJ34BLI/AAAAAAAAOGo/mJ2VmuygiZg/s1600-h/image%25255B76%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-KBGn4BPh6Lk/UUh7PTrNrNI/AAAAAAAAOGw/wdDCW_q135s/image_thumb%25255B36%25255D.png?imgmax=800" width="149" height="106"></a><br>エラーが発生した該当行がそのまま格納され、カンマで区切られてエラー番号を取得できていることを確認できます。</li></ol> <h5 id="id005">スクリプト コンポーネント(C#)によるエラー メッセージの取得</h5> <h6>スクリプト コンポーネントによるエラー メッセージの取得</h6> <p>ここまでの手順では、「列0」列のエラー発生時に「エラー行」と「エラー番号」をファイルへ書き込むことができましたが、エラー番号だけではどういったエラーなのかがよく分かりません。そこで、エラー番号をもとにエラー メッセージを取得するようにパッケージを変更して、エラーの内容を分かりやすくしてみましょう。これを行うには、スクリプト コンポーネントを利用します。<br>それでは、これを試してみましょう。</p> <ol> <li>まずは、スクリプト コンポーネントを配置する前の作業として[フラット ファイル ソース]から[フラット ファイル変換先]へ伸びている「赤い矢印」を右クリックして、[削除]をクリックし、削除しておきます。<br><a href="http://lh5.ggpht.com/-aJFaj4mGa2o/UUh7UXcHhAI/AAAAAAAAOG4/ghpYAeIm2yY/s1600-h/image%25255B80%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-EqWxaSduGkI/UUh7bvVprnI/AAAAAAAAOHA/x2M_Wuq5Qh4/image_thumb%25255B38%25255D.png?imgmax=800" width="304" height="244"></a> <li>次に、SSIS ツールボックスから[共通]カテゴリの[スクリプト コンポーネント]をドラッグ&ドロップして[フラット ファイル変換先]の上部へ配置します。<br><a href="http://lh5.ggpht.com/-sbifqEO4xxQ/UUh7gnEiuyI/AAAAAAAAOHI/XGBj1oly2wk/s1600-h/image%25255B84%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-h55L6SoOUB4/UUh7n66UobI/AAAAAAAAOHQ/ehnhjJP33EE/image_thumb%25255B40%25255D.png?imgmax=800" width="606" height="170"></a><br>これにより、[スクリプト コンポーネントの種類を選択]ダイアログが表示されるので、[変換]が選択されていることを確認して、[OK]ボタンをクリックします。 <li>次に、[フラット ファイル ソース]をクリックして表示される赤い矢印を[スクリプト コンポーネント]まで伸ばします。<br><a href="http://lh3.ggpht.com/-Oas8nOjWWyk/UUh7tHiyzSI/AAAAAAAAOHY/1ljzY4n3Emo/s1600-h/image%25255B89%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-KBqPLHh4Zr8/UUh70VYJxfI/AAAAAAAAOHg/YqGZEd6gV2E/image_thumb%25255B43%25255D.png?imgmax=800" width="603" height="256"></a><br>これにより、[エラー出力の構成]ダイアログが表示されるので、「列0」列のエラーの情報を取得できるようにするために、「列0」の[エラー]列が「行のリダイレクト」になっていることを確認して、[OK]ボタンをクリックします。 <li>次に、[スクリプト コンポーネント]をダブル クリックします。<br><a href="http://lh5.ggpht.com/-Y1w7ps8-qYc/UUh75QH_A5I/AAAAAAAAOHo/Vp5AbzroHX4/s1600-h/image%25255B93%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-AvaUHRpozwg/UUh8AjN9DII/AAAAAAAAOHw/l_iQtRfllbQ/image_thumb%25255B45%25255D.png?imgmax=800" width="500" height="246"></a> <li>[スクリプト変換エディター]ダイアログが表示されたら、[入力列]ページをクリックして開き、[ErrorCode]にチェック マークをつけます。<br><a href="http://lh6.ggpht.com/-rxsQV0Ww8jU/UUh8FqiXVkI/AAAAAAAAOH4/prjjDRIHSKE/s1600-h/image%25255B98%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-iL6F_XXMOUM/UUh8M2GgkGI/AAAAAAAAOIA/_2Crjb1_yKw/image_thumb%25255B48%25255D.png?imgmax=800" width="496" height="275"></a><br>これにより、エラー発生時に[フラット ファイル ソース]からエラー番号を取得できるようになります。 <li>続いて、取得したエラー番号からエラー メッセージを取得するための列を追加します。[入力および出力]ページをクリックし、[出力0]を展開して[出力列]フォルダーを選択した状態で、[列の追加]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-utdEEGdBLzQ/UUh8RwsacRI/AAAAAAAAOII/j9McG3i8Vn4/s1600-h/image%25255B102%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-VS_b5ZPXhJI/UUh8ZGVJsxI/AAAAAAAAOIQ/j1c8yg737Fw/image_thumb%25255B50%25255D.png?imgmax=800" width="417" height="377"></a> <li>すると、次のように追加する列の名前を入力できるようになるので、今回は「ErrDesc」と入力します。<br><a href="http://lh4.ggpht.com/-8IA_WJNA4T4/UUh8eG8bf6I/AAAAAAAAOIY/T92gZst9LCM/s1600-h/image%25255B106%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-V5jhoNVNTaU/UUh8lLxtyoI/AAAAAAAAOIg/3itpCABfOcY/image_thumb%25255B52%25255D.png?imgmax=800" width="412" height="182"></a><br>右側のプロパティ画面では、[DataType]を「文字列 [DT_STR]」、[Length]を「100」へ変更します。 <li>続いて、エラーの説明を取り出すスクリプトを記述するために、次のように[スクリプト]ページをクリックして、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-_2uUW75nWt0/UUh8qGFYHAI/AAAAAAAAOIo/vOUHzEBjadE/s1600-h/image%25255B110%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-KLcAU4IhJ6s/UUh8xarBrgI/AAAAAAAAOIw/5to0nnfahe4/image_thumb%25255B54%25255D.png?imgmax=800" width="418" height="239"></a> <li>これにより、「スクリプト エディター」が起動されるので、「public override void 入力0_ProcessInputRow(~)」内に、次のコード(C#)を入力します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">Row.ErrDesc = this.ComponentMetaData.GetErrorDescription(Row.ErrorCode);</td></tr></tbody></table><br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6OvGYWdNMRw9njNKvHvsNVEXlgEzvIr2QwmYtWHtmQU2qZiaO3JgZsTLbrS_Yp_EULnotZWIpw6tfMjjjB24yli8eoXl28EPg5koFKJLLralmf0oC50xst1wDkOBMHZx6y8MeJSul0tva/s1600-h/image%25255B114%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-L3Veni_xhX4/UUh89oJmTOI/AAAAAAAAOJA/ne9ejs109Ww/image_thumb%25255B56%25255D.png?imgmax=800" width="416" height="102"></a><br>このコード内の Row は、今まさに処理をしている該当行を意味しています。「Row.ErrorCode」と指定することで、前の手順で受け取った ErrorCode 列のデータ(エラー発生時のエラー番号)を取得でき、「GetErrorDescription」メソッドへこの番号を与えることで、エラー番号に対応したエラーの説明(エラー メッセージ)を取得できるようになります。また、「Row.ErrDesc」は、前の手順で作成した「ErrDesc」列で、この列へエラー メッセージを代入するという意味です。なお、Visual Basic でコードを記述する場合は、「this」を「Me」へ変更して、最後のセミコロン(;)を削除します。<br>入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト変換エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>スクリプト コンポーネントで取得したエラーの説明をファイルへ書き込むようにするために、次のように[スクリプト コンポーネント]の青色の矢印を[フラット ファイル変換先]まで伸ばします。続いて、[フラット ファイル変換先]をダブル クリックします。<br><a href="http://lh4.ggpht.com/-eZCXGu7aaUs/UUh9CadCt9I/AAAAAAAAOJI/twkpUPBl87s/s1600-h/image%25255B118%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-bemRoTHsxyU/UUh9JjArrfI/AAAAAAAAOJQ/TNYtluWLa2c/image_thumb%25255B58%25255D.png?imgmax=800" width="411" height="213"></a><br>これにより、[フラット ファイル変換先エディター]ダイアログが表示されるので、[新規作<br>成]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-FOFvyHTW-jY/UUh9Ouf8JtI/AAAAAAAAOJY/jxEVIGqXHAE/s1600-h/image%25255B122%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-JQ2VACss1wI/UUh9V4sI0AI/AAAAAAAAOJg/6DeQ_f7aTdo/image_thumb%25255B60%25255D.png?imgmax=800" width="422" height="114"></a> <li>[フラット ファイル形式]ダイアログが表示されたら、[区切り記号](カンマ区切り)が選択されていることを確認して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-GGn5AKfggb0/UUh9apGp1xI/AAAAAAAAOJo/6_QHVrA9Yh8/s1600-h/image%25255B125%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-wWfL_onKCFc/UUh9h3YHwSI/AAAAAAAAOJw/78RJmdlVitM/image_thumb%25255B61%25255D.png?imgmax=800" width="244" height="188"></a> <li>次に、[フラット ファイル接続マネージャー エディター]ダイアログが表示されるので、今回は[接続マネージャー名]へ「エラーの説明を書き込む_errOut2」とし、[ファイル名]へ「C:\errOut2.txt」と入力します。<br><a href="http://lh6.ggpht.com/-Tw-G__NY2Z8/UUh9mwx2jpI/AAAAAAAAOJ4/_bu_D5MXT-Q/s1600-h/image%25255B129%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGzK4Pj2G8UePAgs9iygsKKZnuv402KuldbJz6VhByZHqdbvN1VRIImn-wYpPzz4Gq6Q7koVPXaeX36lLTzWKFAgOtZCL_eRw-Xf7aUPFF-pYh_yK5tHBqqktYR4uWwVFMaykZCbGYqRTw/?imgmax=800" width="407" height="148"></a> <li>続いて、[詳細設定]ページをクリックして、列のプロパティを確認します。<br><a href="http://lh5.ggpht.com/-asVZBaJsz4Q/UUh9y9PBnaI/AAAAAAAAOKI/TdQxJA1G9Lo/s1600-h/image%25255B133%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-CTer2cn2YO4/UUh96dIWwtI/AAAAAAAAOKQ/zWdWljH21fY/image_thumb%25255B65%25255D.png?imgmax=800" width="409" height="316"></a><br>[ErrDesc]列を選択して、前の手順で設定したように、[DataType]が「文字列」で、[OutputColumnWidth]が「100」となっていることを確認し、[OK]ボタンをクリックします。 <li>[フラット ファイル変換先エディター]ダイアログへ戻ったら、[マッピング]ページをクリックして、ファイルへ書き込むデータのマッピングを設定します。<br><a href="http://lh4.ggpht.com/-K-ftygQsMw4/UUh9_KU7q2I/AAAAAAAAOKY/GtIecM2Soyo/s1600-h/image%25255B137%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-WGS-jpHEkss/UUh-GUuf0NI/AAAAAAAAOKg/CpSC02xOQ8s/image_thumb%25255B67%25255D.png?imgmax=800" width="414" height="166"></a><br>[使用できる入力列]の「ErrDesc」列を、[使用できる変換先列]の「ErrDesc」列へドラッグ&ドロップして、スクリプト コンポーネントで取得したエラー メッセージをファイルへ書き込むようにします。 <li>パッケージをデバッグ実行して、ここまでの設定を確認します。<br><a href="http://lh4.ggpht.com/-7G04P00YFdY/UUh-LXzWtuI/AAAAAAAAOKo/Du1X-Cv_Pb0/s1600-h/image%25255B141%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/--pzPpVBxmtU/UUh-S7aC6YI/AAAAAAAAOKw/42UXHRmxo3U/image_thumb%25255B69%25255D.png?imgmax=800" width="417" height="167"></a><br>すべてのコンポーネントに緑のチェックマークが付いて、データ転送が成功したことを確認できます。 <li>確認後、デバッグを終了します。 <li>転送されたデータを確認するために、Management Studio から、[ABC]テーブルを右クリックして、[上位 1000 行の選択]をクリックします。<br><a href="http://lh6.ggpht.com/-hJnk83s2KRY/UUh-XwG6m5I/AAAAAAAAOK4/BqUltZoRkt0/s1600-h/image%25255B145%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-rxQSAu9biAo/UUh-fLUq8sI/AAAAAAAAOLA/bz4HK8oUwMA/image_thumb%25255B71%25255D.png?imgmax=800" width="416" height="126"></a> <li>次に、エクスプローラーから C:\errOut2.txt ファイルを開いて、内容を確認します。<br><a href="http://lh4.ggpht.com/-9018u_GG2_4/UUh-jzq8FEI/AAAAAAAAOLI/RILy_KQoO3Q/s1600-h/image%25255B149%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-kO1gPCJ2sOs/UUh-rJLGGlI/AAAAAAAAOLQ/bz6VzpgvhGU/image_thumb%25255B73%25255D.png?imgmax=800" width="415" height="93"></a><br>今度は、エラー番号に加えて、エラー メッセージ(エラーの説明)も取得できていることを確認できます。このように、スクリプト コンポーネントを利用すると、エラーが発生した該当行を取得して、かつエラーの内容を確認できるようになるので、大変便利です。 <li>最後に、[ファイル]メニューの[すべてを保存]をクリックして、SSISoyo2 プロジェクトを保存しておきます。このプロジェクトは、次の Step でも引き続き使用します。<br></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-22005736388530979752013-03-19T17:25:00.001+09:002013-03-19T17:33:49.727+09:00◆レポート サーバーの構成 と URL アクセス構文<ul> <li><a href="#id001">レポート サーバーの構成</a> <li><a href="#id002">レポート サーバーの構成を後から設定する方法</a> <li><a href="#id003">レポート マネージャーでのレポートの管理</a> <li><a href="#id004">URL アクセス構文とは</a> <li><a href="#id005">URL アクセス構文でのレポートの表示: rs:Command=Render</a> <li><a href="#id006">URL アクセス構文でのパラメーターの指定</a> <li><a href="#id007">そのほかの URL アクセス構文</a> <li><a href="#id008">URL アクセス構文でのレポートの出力形式の指定</a> </li></ul> <h5 id="id001">レポート サーバーの構成</h5> <h6>レポート サーバーの構成</h6> <p>Reporting Services のインストール時に、SQL Server のデータベース エンジンを同時にインストールする場合は、デフォルトでは「レポート サーバー」(Reporting Services のサーバー機能)が自動構成されます。これは、インストール時の次のページで確認できます。</p> <p><a href="http://lh3.ggpht.com/-NgUMVohakU0/UUggSk30LrI/AAAAAAAAN3Q/UyicxZLwg90/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-_wdaCvszVrQ/UUggTHD_wHI/AAAAAAAAN3Y/8ZXxA_VlIys/image_thumb%25255B1%25255D.png?imgmax=800" width="638" height="199"></a></p> <p><a href="http://lh6.ggpht.com/-_M84NGB-yk0/UUggTlx_EII/AAAAAAAAN3g/STxH5_Q46us/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-sjuFpgF6zl8/UUggUPS_PDI/AAAAAAAAN3o/YF8Eru4T9wc/image_thumb%25255B3%25255D.png?imgmax=800" width="638" height="152"></a></p> <p>上画面のように、[Reporting Services の構成]ページで「インストールと構成」が選択されている場合(デフォルトでは選択されている)は、レポート サーバーが自動構成されます。この場合は、次の URL でレポート サーバーへアクセスすることができます。</p> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">http://サーバー名/ReportServer</td></tr></tbody></table> <p><a href="http://lh4.ggpht.com/-owNqoq8r0Ls/UUggVRECu8I/AAAAAAAAN3w/NEsE1rF7M00/s1600-h/image%25255B11%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-RTOjarTso_M/UUggV7XlmXI/AAAAAAAAN34/l0rlKwC9EXU/image_thumb%25255B5%25255D.png?imgmax=800" width="550" height="220"></a></p> <p>また、次の URL でレポート マネージャー(レポートの管理用 Web サイト)へアクセスすることができます。</p> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">http://サーバー名/Reports</td></tr></tbody></table> <p><a href="http://lh6.ggpht.com/-7oJc_UbR7tY/UUggWdtElfI/AAAAAAAAN4A/QyFGQKGtJoA/s1600-h/image%25255B15%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-1jUN22LTzw4/UUggW-TNwFI/AAAAAAAAN4I/lkSGoLANcPk/image_thumb%25255B7%25255D.png?imgmax=800" width="528" height="238"></a></p> <p>SQL Server 上には、レポート サーバーのシステム データベースである「ReportServer」および「ReportServerTempDB」(一時領域用 DB)が自動作成されています。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSeucuEOVlLFVmYgUwRL52aRKz9ZXy1RQrpnADSxROQp17vL5PBcr3iZI5e_OpOjWmjTf6pId31xYXoj1Bs4pJdUcwhFTCCpanrqh77Fy5ihzyOqGGXYCgumacQ3QFH2TLRU3ykXcm60GT/s1600-h/image%25255B19%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-b5GNmmLFems/UUggYSk1LCI/AAAAAAAAN4Y/p__L8hUTkHU/image_thumb%25255B9%25255D.png?imgmax=800" width="285" height="188"></a></p> <h6>レポート サーバーの構成を確認/変更するには</h6> <p>レポート サーバーの構成を確認/変更したい場合は、Reporting Services 構成マネージャーを利用します。このツールは、次のように[Microsoft SQL Server 2012]の[構成ツール]メニューの[Reporting Services 構成マネージャー]から起動することができます。</p> <p><a href="http://lh3.ggpht.com/-SsBXTNzIHXo/UUggY2nP8LI/AAAAAAAAN4g/W8d0Y5U1tIA/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Jdh0aqZIyeM/UUggZUhx8EI/AAAAAAAAN4o/bR-GZFfSunU/image_thumb%25255B11%25255D.png?imgmax=800" width="275" height="193"></a></p> <p>このツールを起動すると、次のように[Reporting Services 構成の接続]ダイアログが表示されて、サーバー名とインスタンス名(既定のインスタンスの場合は MSSQLSERVER)が表示されるので、[接続]ボタンをクリックしてレポート サーバーへ接続することができます。</p> <p><a href="http://lh3.ggpht.com/-GU8rGq04-3Q/UUggZ7uncAI/AAAAAAAAN4w/PryArAWT1es/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-VLanhaCPfUQ/UUggbYjZo4I/AAAAAAAAN44/7ESqBafpcyU/image_thumb%25255B13%25255D.png?imgmax=800" width="393" height="305"></a></p> <p>Reporting Services 構成マネージャーでは、次のように「Web サービス URL」ページで、レポート サーバーの URL を確認することができます。</p> <p><a href="http://lh5.ggpht.com/-qwZFzwj_65w/UUggb1lllnI/AAAAAAAAN5A/omozXWxhaCg/s1600-h/image%25255B32%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-21Ww6OWWFcQ/UUggcTfR3vI/AAAAAAAAN5I/qbCPkuZDki8/image_thumb%25255B16%25255D.png?imgmax=800" width="601" height="270"></a></p> <p>また、「レポート マネージャー URL」ページで、レポート マネージャーの URL を確認することができます。</p> <p><a href="http://lh6.ggpht.com/-2OTqMIkFYp8/UUggcxB-WRI/AAAAAAAAN5Q/SPibAQCEC4w/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-MXMsDM6rCfM/UUggduqIhpI/AAAAAAAAN5Y/--CNaSrwr-A/image_thumb%25255B18%25255D.png?imgmax=800" width="599" height="212"></a></p> <h5 id="id002">レポート サーバーの構成を後から設定する方法</h5> <h6>レポート サーバーの構成を後から設定する方法</h6> <p>SQL Server のデータベース エンジンをインストールした後に、後から Reporting Services をインストールする場合には、レポート サーバーを自動構成することができません。この場合は、インストール時に次のように[インストールのみ](自動構成なし)しか選択できません。</p> <p><a href="http://lh5.ggpht.com/-Pp2pcusjCns/UUggeEHQQBI/AAAAAAAAN5g/cBD3TBIcQw0/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ve-k8UFKvHI/UUggepN7u0I/AAAAAAAAN5o/iv2mN2ROFPc/image_thumb%25255B20%25255D.png?imgmax=800" width="540" height="163"></a></p> <h6>レポート サーバーの構成手順</h6> <p>レポート サーバーを構成するには、Reporting Services 構成マネージャーを利用します。</p> <ol> <li>Reporting Services 構成マネージャーは、[Microsoft SQL Server 2012]の[構成ツール]メニューの[Reporting Services 構成マネージャー]から起動できます。<br><a href="http://lh5.ggpht.com/-SJvBFIk7Q6A/UUggfAaMvgI/AAAAAAAAN5w/o36onLomHZ4/s1600-h/image%25255B45%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-VdxtsMfMDtk/UUggfo4NA4I/AAAAAAAAN54/0TKlRRk1pDA/image_thumb%25255B23%25255D.png?imgmax=800" width="316" height="196"></a> <li>[Reporting Services 構成の接続]ダイアログが表示されたら、次のようにサーバー名とインスタンス名(既定のインスタンスの場合は MSSQLSERVER)が表示されるので、[接続]ボタンをクリックしてレポート サーバーへ接続します。<br><a href="http://lh4.ggpht.com/-XwyY6pcBX-4/UUgggDx32iI/AAAAAAAAN6A/-Uzv3isr3fY/s1600-h/image%25255B49%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-wfCOBLcI8Rw/UUgggZLk3lI/AAAAAAAAN6I/cXj7YmS-2T0/image_thumb%25255B25%25255D.png?imgmax=800" width="312" height="247"></a> <li>最初のページでは、Reporting Services サービスの停止や開始を行うことができます。<br><a href="http://lh5.ggpht.com/-pt0aLiKTZLQ/UUggg-oZ1kI/AAAAAAAAN6Q/ujZek9ONCqo/s1600-h/image%25255B55%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Vlb_sWDNgxY/UUgghqNQB9I/AAAAAAAAN6Y/AUjKcwD4iKs/image_thumb%25255B29%25255D.png?imgmax=800" width="591" height="295"></a> <li>次に、[Web サービス URL]ページを開いて、レポート サーバーの URL を設定します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1xLECrC0vlO1zloa5S_EALQLIxuDB-cxABQYfWlyh8YA2Wry-Rx_kjBQhdQ5TDGMifJLjErI7dRLRQFGOlAMAICiqc_A2PUhZFCOH9X3ZRiKZH4iQKOPMeakD9PvmSQGDF0FlwSPSmTrd/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-e7BJ9-nht7s/UUggiVmc0SI/AAAAAAAAN6o/zrNR0Ygz8QI/image_thumb%25255B31%25255D.png?imgmax=800" width="430" height="267"></a><br>このページでは[適用]ボタンをクリックします。<br>数十秒後に、次のように結果が表示されれば、成功です。<br><a href="http://lh3.ggpht.com/-k7soB4Z9iUo/UUggjHbCyWI/AAAAAAAAN6w/aCnsdYA5iHE/s1600-h/image%25255B63%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Csg8TkBw9pw/UUggjfmriII/AAAAAAAAN64/bBhje1KLJQU/image_thumb%25255B33%25255D.png?imgmax=800" width="428" height="190"></a> <li>次に、[データベース]ページを開いて、レポート サーバーが利用するシステム データベースを作成します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinizN9nnWpAAMDdoud93GewwxLzB3G7TEHUXyvVD3flCKnm-YuNUFWgbF8xSilTh4VUlfnntuGWlkxRWgsOe6k65FH-nJPPz5mEpmEBN8qXBuAR_NA4NBCQXHU20N61UWsZ26ZWt5k5p8o/s1600-h/image%25255B67%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-DFrKfNTYyBQ/UUggkROjiqI/AAAAAAAAN7I/Ta8FVZXTFxs/image_thumb%25255B35%25255D.png?imgmax=800" width="585" height="195"></a><br>ここでは、[データベースの変更]ボタンをクリックして、データベースを作成します。<br><a href="http://lh5.ggpht.com/-PEkZUtJv2vU/UUggk9bFcXI/AAAAAAAAN7Q/DvmsGlC-1Fg/s1600-h/image%25255B80%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Zks_GvBqeyc/UUgglkAN_DI/AAAAAAAAN7Y/fBSzZ4juuDU/image_thumb%25255B40%25255D.png?imgmax=800" width="585" height="219"></a><br><a href="http://lh5.ggpht.com/-jwReykzxKPM/UUggl94xruI/AAAAAAAAN7g/5iFV4TMtJyU/s1600-h/image%25255B177%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XAC6pra728E/UUggmcZguRI/AAAAAAAAN7o/i8QteMpvtWQ/image_thumb%25255B93%25255D.png?imgmax=800" width="584" height="214"></a><br><a href="http://lh4.ggpht.com/-fxRgAAHWE_0/UUggm8mcTZI/AAAAAAAAN7w/MytGf7UHoBI/s1600-h/image%25255B178%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-XIVOw5__oDQ/UUggnunVBTI/AAAAAAAAN74/4Aey7aC0fc4/image_thumb%25255B94%25255D.png?imgmax=800" width="585" height="219"></a><br>データベースの作成が完了すると、次のように表示されます。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDouqXn50ylZobJgEB-aAD1ENz6eE8N5izwRLDWCd7QBn9Yyh4b5NEouq-PjkesSFinti1TdNMLcelhc800lh9HOnom2ptJPUY3Q_fwSolojuXoztz7kBkkn7yxBd4RB6oaQi0Yu00RgMa/s1600-h/image%25255B179%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-pAahyQHBBe8/UUggo7AkNWI/AAAAAAAAN8I/-qidbEF0zqw/image_thumb%25255B95%25255D.png?imgmax=800" width="585" height="336"></a> <li>次に、[レポート マネージャー URL]ページで、レポート マネージャーの URL を設定します(ここでも[適用]ボタンをクリックします)。<br><a href="http://lh6.ggpht.com/-M6nCIJ6Fhpg/UUggpYDUpEI/AAAAAAAAN8Q/RrOVoXWiqkg/s1600-h/image%25255B88%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-_zsa4dB7qSc/UUggp2uhAoI/AAAAAAAAN8Y/Sz-I4MUd71U/image_thumb%25255B46%25255D.png?imgmax=800" width="427" height="253"></a><br>数十秒後に、次のように結果が表示されれば、成功です。<br><a href="http://lh4.ggpht.com/-5yfOLJ2-psw/UUggqTSan9I/AAAAAAAAN8g/ejKQZnovuQs/s1600-h/image%25255B92%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-0QUjtR2sjUQ/UUggrCOA9_I/AAAAAAAAN8o/fqbWcSw5qqU/image_thumb%25255B48%25255D.png?imgmax=800" width="429" height="195"></a><br>以上でレポート サーバーの構成が完了です。</li></ol> <h5 id="id003">レポート マネージャーでのレポートの管理</h5> <h6>レポート マネージャーでのレポートの管理</h6> <p>レポート マネージャーは、レポートを管理するためのツールです。このツールでは、レポートを参照したり、アップロードしたりすることも可能です。</p> <p><a href="http://lh3.ggpht.com/-d9UKd-9eDgw/UUggrX15UeI/AAAAAAAAN8w/p_RVn8Vvcns/s1600-h/image%25255B96%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-ul-qJF2bpzo/UUggr5LIrAI/AAAAAAAAN84/CBD5OEvuu-Q/image_thumb%25255B50%25255D.png?imgmax=800" width="566" height="189"></a></p> <p>レポートの参照<br><a href="http://lh3.ggpht.com/-D5BDf20gItc/UUggsDtm-aI/AAAAAAAAN9A/_Ku18qKAVUU/s1600-h/image%25255B100%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-6Qa7OJqgStI/UUggsp0847I/AAAAAAAAN9I/LV5mGr_J_b4/image_thumb%25255B52%25255D.png?imgmax=800" width="353" height="242"></a></p> <p>レポートを管理するには、次のようにレポートの[▼]ボタンをクリックして、[管理]をクリックします。</p> <p><a href="http://lh6.ggpht.com/-83JNwwFG_Do/UUggtLTNpRI/AAAAAAAAN9Q/eJs7aYjvWLg/s1600-h/image%25255B109%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-vRAm7BRRPug/UUgguM20bII/AAAAAAAAN9Y/P9Rgphn0Gi0/image_thumb%25255B57%25255D.png?imgmax=800" width="339" height="232"></a><a href="http://lh5.ggpht.com/-G3z9TN3Cki8/UUggugxWbWI/AAAAAAAAN9g/jKSuplUVzCo/s1600-h/image%25255B108%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-8BD7ZyWNlao/UUggvevd3BI/AAAAAAAAN9o/mQFim72yP-8/image_thumb%25255B56%25255D.png?imgmax=800" width="307" height="235"></a></p> <h6>フォルダーの作成</h6> <p>レポート マネージャーでは、次のようにフォルダーを作成することもできます。</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrJLZbUUmYbURNPeovCVNXcWpkio5PFRAtaOHZxGUWTBXOUz-Heq8EUwsPDppHQG9r-_nTFTURiz6MBKHSBArbTXcwAQ85BUa5vYEmtVUhyphenhyphenTkHD0ysJbBcmu_D25R_pJsKaOT34sJgEQig/s1600-h/image%25255B113%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-xsDhARA8Njc/UUggwQYb5sI/AAAAAAAAN94/8iLxQEjgiEc/image_thumb%25255B59%25255D.png?imgmax=800" width="429" height="183"></a></p> <h6>セキュリティの設定</h6> <p>フォルダーやレポートに対しては、次のようにセキュリティを設定することができます。</p> <p><a href="http://lh4.ggpht.com/-xEieJFNX9ko/UUggxJL_GzI/AAAAAAAAN-A/jtowOz6MvFg/s1600-h/image%25255B117%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-WFGUR_PUgvI/UUggxSvfKeI/AAAAAAAAN-I/6S-U_b4j6rI/image_thumb%25255B61%25255D.png?imgmax=800" width="415" height="108"></a><br><a href="http://lh4.ggpht.com/-UK-j2gxYjDI/UUggxyucn3I/AAAAAAAAN-Q/JgVF3OYi_xM/s1600-h/image%25255B121%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-N5kAVJuLGMU/UUggybhVhTI/AAAAAAAAN-Y/hNfulljd564/image_thumb%25255B63%25255D.png?imgmax=800" width="417" height="267"></a></p> <p>「コンテンツ マネージャー」は管理者、「ブラウザー」は参照のみが可能なユーザーといった具合に、フォルダー全体またはレポート単位でセキュリティを設定することができます。</p> <h5 id="id004">URL アクセス構文とは</h5> <h6>URL アクセス構文とは</h6> <p>レポート サーバーに保存されたレポートは、「URL アクセス構文」を利用してアクセスすることができます。URL アクセス構文は、レポートを参照するカスタム アプリケーションを作成する際に最も基本となるものなので、構文を覚えておくことは非常に重要です。</p> <h6>一覧を表示する URL アクセス構文: rs:Command=ListChildren</h6> <p>URL アクセス構文では、レポートの一覧を表示する場合に、次のように記述します。</p> <p><a href="http://lh5.ggpht.com/-99Jrg-ZpTr4/UUggyhgWQ0I/AAAAAAAAN-g/4jzcIoBdqM0/s1600-h/image%25255B125%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-WKTzlAPOBo0/UUggzU7JjKI/AAAAAAAAN-o/MH5hVdhZjIA/image_thumb%25255B65%25255D.png?imgmax=800" width="544" height="78"></a></p> <p>ReportServer の後に「?」マークを付けて、「/」区切りでフォルダー名を指定します。<br>それでは、これを試してみましょう。今回は、フォルダーを作成していないので、ルートにあるレポートの一覧を表示してみます。</p> <p><a href="http://lh5.ggpht.com/-RJ4Y7dU12rM/UUggz5cRVYI/AAAAAAAAN-w/SyfNP76ztLk/s1600-h/image%25255B129%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Xd06-A6AwBI/UUgg0ft8sdI/AAAAAAAAN-4/vPwjXIh3K7M/image_thumb%25255B67%25255D.png?imgmax=800" width="381" height="196"></a></p> <p>前の Step で配置したレポート(SalesRepo)が一覧されることを確認できます。このように「&rs:Command=ListChildren」を指定することで、レポートの一覧を取得できるようになります。</p> <h5 id="id005">URL アクセス構文でのレポートの表示: rs:Command=Render</h5> <h6>URL アクセス構文でのレポートの表示: rs:Command=Render</h6> <p>レポートを表示する URL アクセス構文は、次のとおりです。</p> <p><a href="http://lh6.ggpht.com/-oNKWTWnM_S8/UUgg07yCXmI/AAAAAAAAN_A/BIcmWMJn7g4/s1600-h/image%25255B133%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ZoXlR-yKzjE/UUgg1fTEylI/AAAAAAAAN_I/KlVcAxbxens/image_thumb%25255B69%25255D.png?imgmax=800" width="516" height="64"></a></p> <ol> <li>SalesRepo レポートを表示するには、Internet Explorer のアドレスへ、次のように入力します。<br> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">http://サーバー名/ReportServer?/SalesRepo</td></tr></tbody></table><a href="http://lh6.ggpht.com/-W11_wZNBbF4/UUgg11-e5tI/AAAAAAAAN_Q/Opq0MXLNp88/s1600-h/image%25255B137%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipDANGJyaEo2A8xuvYLlVF94n0EpJ4qfEeFhiG3FcbQFm47xXw46aQZNDt7yH1ilMivVPhAf49y3yfRU4LR9XuM4kSd8L77RtI51EZVbu196JOg-O3zIJyH01bTdMST6hafigsGfWRjMHf/?imgmax=800" width="403" height="285"></a><br>アドレスが「http://localhost/ReportServer/Pages/ReportViewer.aspx?/Sales Repo」へ自動変換されて、SalesRepo レポートが表示されていることを確認できます。 <li>また、次のように「rs:CommandRender」と指定しても、同じようにレポートを表示することが可能です。<br> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">http://サーバー名/ReportServer?/SalesRepo&rs:Command=Render</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-BtvegMBsrKk/UUgg2vfUJzI/AAAAAAAAN_g/WWG88fxphLA/s1600-h/image%25255B141%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-3cLnF3zF2u8/UUgg3Qd0jfI/AAAAAAAAN_o/koCp9mIQWC8/image_thumb%25255B73%25255D.png?imgmax=800" width="400" height="225"></a></li></ol> <p><font color="#9b00d3">Note: レポート名が日本語の場合は URLEncode が必要</font><br>レポート名が日本語の場合は、URLEncode が必要です。たとえば、レポート名が「区分ごとの受注金額」の場合、次のようにアドレスを入力すると、エラーが発生してレポートを表示することができません。<br>http://サーバー名/ReportServer?/区分ごとの受注金額</p> <p>正しく接続するには、次のように、URLEncode が必要です。<br>http://サーバー名/ReportServer?/%e5%8c%ba%e5%88%86%e3%81%94%e3%81%a8%e3%81%ae %e5%8f%97%e6%b3%a8%e9%87%91%e9%a1%8d</p> <p>このリンクを取得するには、一覧画面のリンクをコピーして使えば簡単。<br><a href="http://lh5.ggpht.com/-tCxj4-QiRJc/UUgg3iCi7yI/AAAAAAAAN_w/xl0L2k4HdHI/s1600-h/image%25255B144%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-LrB65cE2unc/UUgg4PI6ksI/AAAAAAAAN_4/mZl3ZncvVqM/image_thumb%25255B74%25255D.png?imgmax=800" width="244" height="223"></a></p> <h5 id="id006">URL アクセス構文でのパラメーターの指定</h5> <h6>URL アクセス構文でのパラメーターの指定</h6> <p>URL アクセス構文では、次のようにパラメーターを指定することも可能です。<br><a href="http://lh5.ggpht.com/-DdbcspW3bZo/UUgg4jEmbKI/AAAAAAAAOAA/FqJyGNHZ5-Y/s1600-h/image%25255B148%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-gyoKykweNFo/UUgg4-oUPTI/AAAAAAAAOAI/Eha9whq493c/image_thumb%25255B76%25255D.png?imgmax=800" width="643" height="44"></a></p> <p>SalesRepo レポートのパラメーターを指定して、表示するには、Internet Explorer のアドレスへ、次のように入力して接続します。<br><a href="http://lh3.ggpht.com/-ULrbGg8Xdx0/UUgg5rmUQlI/AAAAAAAAOAQ/DdIf9G_q-68/s1600-h/image%25255B153%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-fCvSZQCCtDI/UUgg5yTSpnI/AAAAAAAAOAY/5TjSnMgYZMI/image_thumb%25255B79%25255D.png?imgmax=800" width="365" height="34"></a></p> <p><a href="http://lh6.ggpht.com/-U8BOtZ7MORg/UUgg6Q60GEI/AAAAAAAAOAg/YdG3heKgvKs/s1600-h/image%25255B157%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-0wpsNTHjhek/UUgg8S8XVJI/AAAAAAAAOAo/yc4r9TocG7M/image_thumb%25255B81%25255D.png?imgmax=800" width="368" height="250"></a></p> <p>このアドレスにより、p1(商品区分コード)が「3」の菓子類のデータのみが表示されることを確認できます。</p> <p><font color="#9b00d3">Note: 複数のパラメーターを指定するには<br></font>2006年と2007年のように、複数のパラメーターを指定したい場合には、次のように入力します。<br>http://サーバー名/ReportServer?/SalesRepo&p1=3&p2=2006&p2=2007</p> <p><font color="#9b00d3">Note: パラメーター値が日本語の場合は URLEncode が必要</font><br>レポート名と同様、パラメーター値が日本語の場合は、URLEncode が必要です。たとえば、次のように商品名のパラメーター(@p3)があり、「抹茶バー」の値を指定する場合には、次のとおりです。<br>http://サーバー名/ReportServer?/SalesRepo2&p1=3&p2=2006&p2=2007&p3=%e6%8a%b9%e8 %8c%b6%e3%83%90%e3%83%bc</p> <p>PowerShellを使えば簡単にエンコードした値を求められます。<br><a href="http://lh4.ggpht.com/-GhfrV35pq-Y/UUgg8_eIbZI/AAAAAAAAOAw/BcUzyWQAeII/s1600-h/image%25255B163%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-YXBfNRRdaPQ/UUgg-I5ZxsI/AAAAAAAAOA4/hBzj1HQmEPA/image_thumb%25255B85%25255D.png?imgmax=800" width="415" height="71"></a></p> <h5 id="id007">そのほかの URL アクセス構文</h5> <h6>そのほかの URL アクセス構文</h6> <p>そのほかの URL アクセス構文には、次のようなものがあります。</p> <p><a href="http://lh6.ggpht.com/-N5y60oJhj8k/UUgg-QCAF_I/AAAAAAAAOBA/juO4Qw2W5-Q/s1600-h/image%25255B167%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-RdFfnvRN-wA/UUgg-38PekI/AAAAAAAAOBI/YlXFE3yZn6w/image_thumb%25255B87%25255D.png?imgmax=800" width="418" height="92"></a></p> <p>これは、次のように利用できます。<br><a href="http://lh4.ggpht.com/-uDphim_XL2M/UUgg_2YYJ0I/AAAAAAAAOBQ/9yESBeIiLrc/s1600-h/image%25255B172%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Rv6G4n-W-vg/UUghBBe07UI/AAAAAAAAOBY/QWST8RYRbEg/image_thumb%25255B90%25255D.png?imgmax=800" width="552" height="105"></a></p> <h5 id="id008">URL アクセス構文でのレポートの出力形式の指定</h5> <p>URL アクセス構文では、次のようにレポートの出力形式を指定することも可能です。</p> <p><a href="http://lh3.ggpht.com/-_Xds5fVi4Tk/UUghB1WXkKI/AAAAAAAAOBg/YnEtlYmMP9A/s1600-h/image%25255B176%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-iXFIht2MAfk/UUghCTV-yBI/AAAAAAAAOBo/atvQ3GW38yg/image_thumb%25255B92%25255D.png?imgmax=800" width="549" height="38"></a></p> <p>「rs:Format=IMAGE」とすることで、画像形式を指定し、「rc:OutputFormat=JPEG」で JPEG 形式を指定しています。画像形式を指定した場合は、JPEG のほかには、BMP や EMP、GIF、PNG、TIFF を指定することもできます。</p> <p><font color="#9b00d3">Note: 画像(IMAGE)以外の形式への出力を指定する</font><br>「rs:Format=」へは、IMAGE のほかに、EXCEL や PDF、WORD などを指定することも可能です。</p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-47807266986316483832013-03-19T15:04:00.001+09:002013-03-20T23:29:43.305+09:00◆パラメータの利用<ul> <li><a href="#id001">作成するレポート(商品区分ごとの売上レポート)</a> <li><a href="#id002">区分コードのパラメーター化</a> <li><a href="#id003">パラメーター値をレポートへ表示</a> <li><a href="#id004">ドロップダウン リストによるパラメーターの選択</a> <li><a href="#id005">ラベル フィールドをレポートへ表示</a> <li><a href="#id006">パラメーターの複数選択</a> <li><a href="#id007">レポート サーバーへレポートの保存</a> <li><a href="#id008">ジャンプ レポートの作成</a> </li></ul> <h5 id="id001">作成するレポート(商品区分ごとの売上レポート)</h5> <p>この Step では、次のようなパラメーターを含んだレポートを作成する手順を説明します。</p> <p><a href="http://lh3.ggpht.com/-41FEhcTQhgQ/UUf-93cgW6I/AAAAAAAANmo/wauj6ZOBCdo/s1600-h/image3.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/--cglhZBWwEs/UUf--5nNK5I/AAAAAAAANmw/YJRG2UBOlpU/image_thumb1.png?imgmax=800" width="480" height="357"></a></p> <h5 id="id002">区分コードのパラメーター化</h5> <h6>区分コードのパラメーター化</h6> <p>まずは、商品の「区分コード」をパラメーター化して、選択した区分コードの売上データだけを表示するようにしてみましょう。</p> <p><a href="http://lh3.ggpht.com/-el3y2SDSoGg/UUf-_S96vYI/AAAAAAAANm4/PrNL6x_Cfy8/s1600-h/image8.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Xsw0ILxi9Qc/UUf_AHkDoyI/AAAAAAAANnA/jjSJ38b_WJY/image_thumb4.png?imgmax=800" width="641" height="267"></a></p> <h6>共有データセットの変更</h6> <ol> <li>最初に、レポート ビルダーを起動します。 <li>パラメーター化を行うには、データセットを変更する必要があるので、[作業の開始]ページでは、[開く]を選択します。<br><a href="http://lh5.ggpht.com/-vQPa9DzTaZw/UUf_Ap9vEJI/AAAAAAAANnI/WFrXx64yYhc/s1600-h/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-vD5hTnZpXLI/UUf_BFhob5I/AAAAAAAANnQ/6eQlvKta3ag/image_thumb5.png?imgmax=800" width="197" height="143"></a> <li>[レポートを開く]ダイアログが表示されたら、[アイテムの種類]で[データセット(.rsd)]を選択して、Step2 で作成した共有データセット「ds_受注クエリ」を選択し、[開く]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-BF2VXeZFdiw/UUf_BQa5EsI/AAAAAAAANnY/_QGldqFx6wU/s1600-h/image15.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-wdkGKxKp_9Y/UUf_CdIt1jI/AAAAAAAANng/ZTwIFpFpI6s/image_thumb7.png?imgmax=800" width="380" height="280"></a> <li>[データ ソースの資格情報の入力]ダイアログが表示されたら、データ ソースに接続するための適切な資格情報を入力して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-wQvgyn98KiA/UUf_DEQdNvI/AAAAAAAANno/lzA4chx9YMQ/s1600-h/image18.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-n4eyus5TiZM/UUf_DoXVFzI/AAAAAAAANnw/W1qpwdiqLF0/image_thumb8.png?imgmax=800" width="244" height="173"></a> <li>これにより、[クエリ デザイナー]ダイアログが表示されて、データセットの変更が行えるようになります。<br><a href="http://lh4.ggpht.com/-gNZcM8YdTsU/UUf_D89dVFI/AAAAAAAANn4/jxtF45eZjss/s1600-h/image23.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Ae6Om0uears/UUf_EVArN-I/AAAAAAAANoA/WMpe0OI5UEs/image_thumb11.png?imgmax=800" width="593" height="325"></a><br>クエリ デザイナーでは、クエリの一番最後へ「WHERE 商品.区分コード = @p1」を追加して、[!](実行)ボタンをクリックします。<br>[クエリ パラメーターの定義]ダイアログが表示されたら、[パラメーター名]の「@p1」の[パラメーター値]へ「1」と入力して、「OK」ボタンをクリックします。これで、次のように商品区分コードが 1(飲料)のデータのみを取得することができます。<br><a href="http://lh6.ggpht.com/-cK1Fwg6F-WQ/UUf_E4w78tI/AAAAAAAANoI/EevtYqX0XpY/s1600-h/image28.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-YPtw-4P_0rI/UUf_FYHbHgI/AAAAAAAANoQ/VdDMcQs91NQ/image_thumb14.png?imgmax=800" width="584" height="473"></a> <li>次に、左上のレポート ビルダー メニューから[名前を付けて保存]をクリックして、違う名前で共有データセットを保存します。<br><a href="http://lh4.ggpht.com/-KvtEeAkC0rQ/UUf_FiIeZvI/AAAAAAAANoY/krDWnLYEIrQ/s1600-h/image33.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-8cA2NxUr-zU/UUf_GLkJA6I/AAAAAAAANog/6gyDXDZf3oQ/image_thumb17.png?imgmax=800" width="580" height="253"></a><br>[データセットとして保存]ダイアログでは、[名前]に任意の名前(ds_受注クエリpara など)を入力して、[OK]ボタンをクリックします。<br></li></ol> <h6>レポートの作成</h6> <p>次に、新しいレポートを作成します。</p> <ol> <li>レポートを作成するために、別途レポート ビルダーを起動します(共有データセットを編集しているレポート ビルダーは開いたままにしておいてください)。 <li>[作業の開始]ページでは、[新しいレポート]の[空のレポート]を選択します。 <li>レポートのデザイン画面が表示されたら、デザイン上の任意の場所を右クリックして、[挿入]から[マトリックス]をクリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp-kMzVw6w_TacUp7H3hQ1LO6Kyy5pXUPVBhEfz9ab0z7faks4FEA3cYSgRlL3Rzy-uZz2tLAllIy_1fEIewazwXSczTZ74j_cwNvjMdcA2F9w-j1C_NpiRid8MfzJvqhq9pqZDNSLp96l/s1600-h/image36.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-QSDlSjsOAFQ/UUf_HJ5KfPI/AAAAAAAANow/scCafMyU_DY/image_thumb18.png?imgmax=800" width="220" height="170"></a> <li>[データセットのプロパティ]ダイアログが表示されたら、[参照]ボタンをクリックして、前の手順で作成した共有データセット(ds_受注クエリpara)を選択し、[開く]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-SddhX7m5TKM/UUf_HllkksI/AAAAAAAANo4/0ekmJjsfTNc/s1600-h/image40.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-RSVrg1-wSQI/UUf_ICXlQ_I/AAAAAAAANpA/vr63VVKijaM/image_thumb20.png?imgmax=800" width="593" height="303"></a> <li>[データセットのプロパティ]ダイアログへ戻ったら、[OK]ボタンをクリックしてダイアログを閉じます。<br><a href="http://lh4.ggpht.com/-_wU7kIuNXes/UUf_Iu0CreI/AAAAAAAANpI/2DUyrNIVLjM/s1600-h/image43.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-9X8pfCfu69U/UUf_I3z76OI/AAAAAAAANpQ/Uc4EO9zmsgQ/image_thumb21.png?imgmax=800" width="242" height="244"></a> <li>次に、配置したマトリックスの[行グループ]へ「区分名」、[列グループ]へ「年」、[データ]へ「受注金額」フィールドを配置します。<br><a href="http://lh6.ggpht.com/-17RusD7LxLc/UUf_JYlNl5I/AAAAAAAANpY/nYcVTo6WMfE/s1600-h/image47.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-4VAFj1stk90/UUf_J3FVfLI/AAAAAAAANpg/UN-ZNVYWVXY/image_thumb23.png?imgmax=800" width="350" height="298"></a> <li>次に、受注金額の書式を「通貨」へ変更します。<br><a href="http://lh3.ggpht.com/-ybL6qGm7Fpc/UUf_KEyj5xI/AAAAAAAANpo/BOIsJzS_t3Q/s1600-h/image50.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-6dnX4uk0sPY/UUf_KmSAGBI/AAAAAAAANpw/g0NjoNm6HCw/image_thumb24.png?imgmax=800" width="244" height="168"></a> <li>次に、「商品名」フィールドを「区分名」グループの下へ追加して、子グループを作成します。<br><a href="http://lh3.ggpht.com/-tXT6bSdj0RQ/UUf_LEYLiMI/AAAAAAAANp4/ZFxQRZtlmW4/s1600-h/image55.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-uiYjswHJAOI/UUf_Lf2mj5I/AAAAAAAANqA/JSvLzpTDG4Y/image_thumb27.png?imgmax=800" width="549" height="362"></a> <li>次に、各セルの書式(フォントや背景色など)を任意に設定して見栄えを調整します。<br><a href="http://lh4.ggpht.com/-pFm8TRUcc1A/UUf_L2EpBmI/AAAAAAAANqI/Ec6n1YhyOno/s1600-h/image58.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-tiHxpDoQ_wY/UUf_MUpsIHI/AAAAAAAANqQ/HJ_3KDICZcs/image_thumb28.png?imgmax=800" width="244" height="83"></a></li></ol> <h6>パラメーターの設定</h6> <p>次に、パラメーターを設定します。データセットのクエリの中で、「@」マークを付けたもの(前の手順では @p1 を作成)は、自動的にパラメーターへ設定されているので、これを確認/変更します。</p> <ol> <li>パラメーターを確認するには、[レポート データ]ペインの[パラメーター]フォルダーを展開します。<br><a href="http://lh3.ggpht.com/-9ue-F7BJ4aI/UUf_MpixddI/AAAAAAAANqY/sETTmvzvyyk/s1600-h/image62.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-v4fODyMDZKE/UUf_NTwjH6I/AAAAAAAANqg/fhq-n5IejyU/image_thumb30.png?imgmax=800" width="391" height="80"></a><br>p1 という名前のパラメーターが自動的に作成されていることを確認できます。 <li>次に、「p1」パラメーターを右クリックして、[パラメーターのプロパティ]をクリックします。<br><a href="http://lh4.ggpht.com/-Zev3FK-NCeQ/UUf_NjNiS3I/AAAAAAAANqo/u5dJA2sXgVQ/s1600-h/image66.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-A7IBdJF37aU/UUf_OBA0BvI/AAAAAAAANqw/AE5og1Lbioo/image_thumb32.png?imgmax=800" width="386" height="109"></a> <li>[レポート パラメーターのプロパティ]ダイアログが表示されたら、[プロンプト]で「商品区分」など、パラメーターの内容を表す任意の文字を入力し、[データ型]で「整数」を選択します。<br><a href="http://lh3.ggpht.com/-zMDMH_LbYNc/UUf_OvlVrTI/AAAAAAAANq4/dncKAlsKCNU/s1600-h/image69.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-0iWSViTgGpI/UUf_PFUwigI/AAAAAAAANrA/J2zT9BiDTxY/image_thumb33.png?imgmax=800" width="244" height="160"></a> <li>続いて、次のように[既定値]ページをクリックして開き、[値の指定]を選択して、[追加]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-pSpEv2gv9bs/UUf_PfDgkHI/AAAAAAAANrI/UasY2Eg4iD8/s1600-h/image73.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-HIfJxRVbSQE/UUf_P8JtHbI/AAAAAAAANrQ/B6sDsw_1gLE/image_thumb35.png?imgmax=800" width="376" height="314"></a><br>[値]へ「1」(飲料)など、任意の商品区分コードを入力して、パラメーターの既定値を設定し、[OK]ボタンをクリックします。 <li>[実行]ボタンをクリックしてプレビューを表示し、ここまでの結果を確認してみます。<br><a href="http://lh6.ggpht.com/-NiWCFJyyTts/UUf_QQb5BuI/AAAAAAAANrY/mal7UPNz8BE/s1600-h/image77.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XY3m2U-IWcA/UUf_Qoe4gdI/AAAAAAAANrg/yqxDU9XNbrw/image_thumb37.png?imgmax=800" width="374" height="164"></a><br>レポートの上部へ「パラメーター ボックス」が表示されて、既定値として設定した値(ここでは 1)が格納され、商品区分コード「1」の飲料区分の商品のみが表示されていることを確認できます。 <li>次に、パラメーター ボックスの値を「2」(調味料)へ変更して、[レポートの表示]をクリックしてみましょう。<br><a href="http://lh4.ggpht.com/-CZXAI9Kczns/UUf_RMgI3jI/AAAAAAAANro/XRFS0xTASGo/s1600-h/image82.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-GQmykc0Rq18/UUf_RpI7bvI/AAAAAAAANrw/wf6oWDSWTIE/image_thumb40.png?imgmax=800" width="593" height="165"></a><br>今度は、商品区分コードが「2」(調味料)の商品のみが表示されることを確認できます。 <p>このようにパラメーターを利用すると、レポートの結果を動的に変更できるようになります。<br>なお、「区分コード」と「区分名」の対応は、次のように Management Studio で「商品区分」テーブルの中身を参照することで確認することができます。</p></li></ol> <h5 id="id003">パラメーター値をレポートへ表示</h5> <p>次に、パラメーター値をレポート内へ表示するようにしてみましょう。</p> <ol> <li>まずは、次のようにレポート上のタイトルのテキスト ボックス内へ「区分」と入力します。<br><a href="http://lh3.ggpht.com/-zPN-DBVincM/UUf_R8RIFDI/AAAAAAAANr4/Suj9-vwezhA/s1600-h/image86.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-luGtv_F2bK4/UUf_SfY_9CI/AAAAAAAANsA/sGP4f8T08bg/image_thumb42.png?imgmax=800" width="521" height="209"></a><br>続いて、[パラメーター]フォルダーから「p1」パラメーターを、タイトルのテキスト ボックス内へドラッグ&ドロップします。さらに、テキスト ボックス内へ「の受注金額」と入力します。 <li>ここまでの設定をプレビュー表示して、結果を確認してみましょう。<br><a href="http://lh5.ggpht.com/-NQdskUz95k4/UUf_SqB3gEI/AAAAAAAANsI/kHUvNiom6tA/s1600-h/image90.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-E3aCw1CjVdw/UUf_TaFUcvI/AAAAAAAANsQ/9zbBw9sOM7A/image_thumb44.png?imgmax=800" width="514" height="168"></a><br>パラメーター値が、レポート内へ表示されていることを確認できます。 <li>続いて、パラメーター ボックスの値を「2」へ変更して、[レポートの表示]をクリックします。<br><a href="http://lh4.ggpht.com/--ck8NmcwefA/UUf_TsaqamI/AAAAAAAANsY/JQcr5rRk80o/s1600-h/image94.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-SwWXoecfW5o/UUf_UfLNe9I/AAAAAAAANsg/9XMqUmFHol0/image_thumb46.png?imgmax=800" width="515" height="166"></a><br>今度は、レポート内の表示が「2」へ変わったことを確認できます。 <li>確認後、タイトルを任意の書式(フォントなど)へ変更しておきましょう。<br><a href="http://lh5.ggpht.com/-s0--KfzS7WE/UUf_UqtHqRI/AAAAAAAANso/OoZs6ACCAyE/s1600-h/image98.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-TZLTAnUNXyc/UUf_VPGdgoI/AAAAAAAANsw/WZFLmJmFQpw/image_thumb48.png?imgmax=800" width="398" height="171"></a></li></ol> <h5 id="id004">ドロップダウン リストによるパラメーターの選択</h5> <p>次に、パラメーターの値を、ドロップダウン リストで選択できるようにしてみましょう。また、このときに表示するのは区分コートではなく、区分名が表示されるようにしましょう。</p> <p><a href="http://lh5.ggpht.com/-gfJTHW3Ioh0/UUf_VUu07eI/AAAAAAAANs4/WL0cQbflpLo/s1600-h/image102.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-350XlXLdI9k/UUf_WMim-dI/AAAAAAAANtA/CxlqWHJgBkM/image_thumb50.png?imgmax=800" width="390" height="158"></a></p> <ol> <li>まずは、ドロップダウン リストへ表示する「区分名」を取得するために、新しくデータセットを追加します。次のように[データセット]フォルダーを右クリックして、[データセットの追加]をクリックします。<br><a href="http://lh4.ggpht.com/-aVlvj4m0ECQ/UUf_WUJzJzI/AAAAAAAANtI/R_hNBvV-m9Y/s1600-h/image106.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hj8LG577Pvc/UUf_Ww1S9RI/AAAAAAAANtQ/aO4ipL8R_oo/image_thumb52.png?imgmax=800" width="455" height="177"></a><br>[データセットのプロパティ]ダイアログが表示されたら、[レポートに埋め込まれたデータセットを使用します]を選択して、[新規]ボタンをクリックします。 <li>[データ ソースのプロパティ]ダイアログが表示されたら、Step2 で作成した共有データ ソース(DS_NorthwindJ1)を選択して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-Qulh5snUAo0/UUf_XTEzYII/AAAAAAAANtY/UV01Jw8IvBY/s1600-h/image109.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-tMhyJ5tx0X0/UUf_XxPNZgI/AAAAAAAANtg/NJb8TeGx3X0/image_thumb53.png?imgmax=800" width="244" height="176"></a> <li>[データセットのプロパティ]ダイアログへ戻ったら、[クエリ デザイナー]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-ZH2zDYUBl6o/UUf_YBKzeMI/AAAAAAAANto/x8gAonw71BY/s1600-h/image112.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-B29hSEQkhOM/UUf_YmqWe6I/AAAAAAAANtw/X55Eun85C2Y/image_thumb54.png?imgmax=800" width="244" height="86"></a> <li>[データ ソースの資格情報の入力]ダイアログが表示されたら、データ ソースに接続するための適切な資格情報を入力して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-Jvjn8mIMQLQ/UUf_Y2WOPcI/AAAAAAAANt4/rYJnAATud4U/s1600-h/image115.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-W_d8tCU8EQc/UUf_ZCb8_VI/AAAAAAAANuA/dFtI6NQ-o1A/image_thumb55.png?imgmax=800" width="237" height="171"></a> <li>[クエリ デザイナー]ダイアログが表示されたら、次のように[テーブル]フォルダーを展開して、「商品区分」テーブルの「区分コード」と「区分名」列を選択して、[クエリの実行]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-V0VgGQvXa4U/UUf_ZjlhO-I/AAAAAAAANuI/PeT-eiOaLYI/s1600-h/image120.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-xXYNEAp9Vtc/UUf_aVMeoNI/AAAAAAAANuQ/3NYa7W2dYj0/image_thumb58.png?imgmax=800" width="597" height="446"></a><br>商品区分コードと区分名が取得できていることを確認します。確認後、[OK]ボタンをクリックして、[クエリ デザイナー]を閉じます。 <li>[データセットのプロパティ]ダイアログへ戻ったら、[OK]ボタンをクリックしてダイアログを閉じます。 <li>[レポート データ]ペインの[データセット]フォルダーに、新しく「DataSet2」が追加されていることを確認できます。<br><a href="http://lh3.ggpht.com/-vCqs-AsWnRU/UUf_awFkdRI/AAAAAAAANuY/4h7EDt34aRc/s1600-h/image123.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-QBqYGrKWIS4/UUf_bVklGPI/AAAAAAAANug/aAJwX6BllY0/image_thumb59.png?imgmax=800" width="180" height="210"></a> <li>次に、[パラメーター]フォルダーの「p1」を右クリックして、[パラメーターのプロパティ]をクリックします。<br><a href="http://lh4.ggpht.com/-jMHYX2Xr4qw/UUf_b5QtGnI/AAAAAAAANuo/yGbxHVJWpIQ/s1600-h/image128.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-lBEPO7dgqRk/UUf_cYvXWoI/AAAAAAAANuw/zG5Tqiwr8gk/image_thumb62.png?imgmax=800" width="590" height="349"></a><br>[レポート パラメーターのプロパティ]ダイアログが表示されたら、[使用できる値]ページを開いて、「クエリから値を取得」を選択します。[データセット]には「DataSet2」、[値フィールド]で「区分コード」、[ラベル フィールド]で「区分名」を選択して、[OK]ボタンをクリックします。これで、パラメーターをドロップダウン形式に変更して、区分名フィールドを表示できるようになります。 <li>ここまでの設定をプレビュー表示して、結果を確認してみましょう。<br><a href="http://lh5.ggpht.com/-qyNTwIJ-RPw/UUf_c--SB7I/AAAAAAAANu4/zFfT5zf223U/s1600-h/image131.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/--3DXn2rtwtY/UUf_dPmUbrI/AAAAAAAANvA/djE7d9pDFJ0/image_thumb63.png?imgmax=800" width="244" height="113"></a><br>パラメーター ボックスがドロップダウン リストへ変更されて、区分名が選択できるようになっていることを確認できます。 <li>次に、パラメーターのドロップダウン リストから任意の区分名(菓子類など)を選択して、[レポートの表示]をクリックしてみましょう。<br><a href="http://lh4.ggpht.com/-jizBUwXOI3E/UUf_dym9ORI/AAAAAAAANvI/p9TjubKRkjU/s1600-h/image135.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-jTRF7ypuqPY/UUf_eBkxATI/AAAAAAAANvQ/GyzlBZ_57eM/image_thumb65.png?imgmax=800" width="577" height="164"></a><br>選択した商品区分のデータが表示されることを確認できます。</li></ol> <h5 id="id005">ラベル フィールドをレポートへ表示</h5> <p>次に、パラメーターで選択した区分名を、レポートへ表示するように変更してみましょう。</p> <p><a href="http://lh3.ggpht.com/-2k5hplHL--8/UUf_erq1aKI/AAAAAAAANvY/XYmfXTeWF-M/s1600-h/image138.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-PRmpXhucIgk/UUf_fHK3g3I/AAAAAAAANvg/s9jvrsHMRtU/image_thumb66.png?imgmax=800" width="244" height="110"></a></p> <ol> <li>まずは、タイトルのテキスト ボックス内の [@p1] を選択して右クリックし、[式]をクリックします。<br><a href="http://lh3.ggpht.com/-x_wZh21XLmg/UUf_fgMRjYI/AAAAAAAANvo/vdYWFNjSvs8/s1600-h/image141.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSe3DNyhbGtqgZVoNxarquTfV351Egm5RqUgRpsBJ5GfAVNnEesENVzHoHMzdJjNSZI8xzOkqhGBYGTMyBLE-GJX5Ro1iCyOiffL1UdKBzAfa7fKgnEy6PPnBAHpio8VeOsSaw4g6mKV2j/?imgmax=800" width="230" height="147"></a> <li>[式]ダイアログが表示されたら、式を次のように変更します(Value を Label へ変更)。<br> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">=Parameters!p1.Label</td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-qO3XiVmxc70/UUf_gsg_oeI/AAAAAAAANv4/XGEXZUR1V_U/s1600-h/image146.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-C-013gYd_eQ/UUf_hDppjMI/AAAAAAAANwA/o0_--wUL6SU/image_thumb70.png?imgmax=800" width="349" height="342"></a><br>「p1.Value」を「p1.Label」へ変更することで、パラメーターのラベル フィールドの値を表示できるようになります(ラベル フィールドには、前の手順で区分名を割り当てるように設定しています)。 <li>ここまでの設定をプレビュー表示して、結果を確認してみます。<br><a href="http://lh6.ggpht.com/-bVJkkDxj3bk/UUf_hkQoiRI/AAAAAAAANwI/_ao9QjlU-58/s1600-h/image150.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-S8Z0OqXb0ok/UUf_iD1mXEI/AAAAAAAANwQ/Qvq1JJ1nMAk/image_thumb72.png?imgmax=800" width="526" height="178"></a><br>任意の商品区分を選択し、[レポートの表示]をクリックすると、今度は、区分コードではなく、区分名が表示されることを確認できます。</li></ol> <h5 id="id006">パラメーターの複数選択</h5> <p>次に、「年度」を選択するパラメーターを追加して、複数の値を選択できるようにしてみましょう。</p> <p><a href="http://lh5.ggpht.com/-tE229xzqprk/UUf_irhnd1I/AAAAAAAANwY/n8bMuAkOqB0/s1600-h/image154.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ybvm_U5_9ns/UUf_jA2BGEI/AAAAAAAANwg/FXblpi1UntE/image_thumb74.png?imgmax=800" width="537" height="144"></a></p> <ol> <li>まず、共有データセット(ds_受注クエリpara)を編集している側のレポート ビルダーを開きます。レポート ビルダーを閉じてしまっている場合は、新しくレポート ビルダーを起動して、[開く]から共有データセットを開いておいてください。 <li>クエリ デザイナーで、WHERE 句の最後へ「 AND YEAR(受注.受注日) IN (@p2)」を追加して、[!](実行)ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-6RsnXzfsx_A/UUf_jsCSu0I/AAAAAAAANwo/oBQTpenZsgY/s1600-h/image158.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-cvCd5Fm46MU/UUf_j2ALPQI/AAAAAAAANww/jsJx6ntyJJc/image_thumb76.png?imgmax=800" width="493" height="279"></a><br>[クエリ パラメーターの定義]ダイアログが表示されたら、「@p1」には「1」、「@p2」には「2005」と入力して、「OK」ボタンをクリックします。これで、次のように 2005年度の商品区分コードが「1」(飲料)のデータのみを取得することができます。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx7OgJinhaMYmY_ZUj0XK8HjG54yizIdVy9l_EI42bTJ-ze250Sn91IKKfor5uUTT3WLtZa98qZGfDA34aTbdIT08_gBowXznbMoPOEvrbHBuxg2iEA6z68PZWbWHqDXnua2ul9ECQMrzQ/s1600-h/image161.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyKIKt1UfFwzQomxRTt1eYWbwYaZvQ2u2bSvQIc9QZ26Lt1ORNQ5xzN8BiJqzHHqZEmPL2TSStCTi3nu8noAwawECjr9c0pbNsw-9owvZio7sWNSTl8NVLeK1TcWDyyXzkmwkzI0XHNXuD/?imgmax=800" width="241" height="169"></a><br>確認後、画面上部の[保存]ボタン(フロッピーの形をしたアイコン)をクリックして、データセットを保存します。 <li>次に、レポートを作成している側のレポート ビルダーへ戻って、[レポート データ]ペインで共有データセット(DataSet1)を右クリックして、[データセットのプロパティ]をクリックします。<br><a href="http://lh3.ggpht.com/--RHICXaYqTk/UUf_lSsyLfI/AAAAAAAANxI/XP21h8_VPVE/s1600-h/image166.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-s9JLJcnSLRM/UUf_l4fxi8I/AAAAAAAANxQ/zIhlhy_SqFw/image_thumb80.png?imgmax=800" width="485" height="233"></a><br>[データセットのプロパティ]ダイアログが表示されたら、[フィールドの更新]ボタンをクリックします。これで共有データセット側で更新された情報をレポート側に反映させることができます。<br>実行後、[OK]ボタンをクリックしてダイアログを閉じます。 <li>[パラメーター]フォルダーを展開すると、「p2」パラメーターが自動的に作成されていることを確認できます。<br><a href="http://lh3.ggpht.com/-pU2EKM4diJY/UUf_mn9ZBuI/AAAAAAAANxY/CpdtBwVsKxY/s1600-h/image169.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-D6zABHFRbVM/UUf_nIWuSaI/AAAAAAAANxg/I2lDP6hi18E/image_thumb81.png?imgmax=800" width="150" height="121"></a> <li>次に、p2 パラメーターを右クリックして、[パラメーターのプロパティ]をクリックします。<br><a href="http://lh3.ggpht.com/-MuM0O9qVjzU/UUf_nkF5YtI/AAAAAAAANxo/z3AH1U7ThcE/s1600-h/image173.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-z4PwmOK5XK0/UUf_oP-bWRI/AAAAAAAANxw/js93B-xFZLg/image_thumb83.png?imgmax=800" width="488" height="226"></a><br>[レポート パラメーターのプロパティ]ダイアログが表示されたら、[プロンプト]へ「年度」など、パラメーターの内容を表す任意の文字を入力し、[複数の値を許可]をチェックします。 <li>続いて、[使用できる値]ページをクリックして開き、[値の指定]を選択します。[追加]ボタンをクリックして、[値]へ「2005」と入力します。<br><a href="http://lh5.ggpht.com/-SdXzHvyZXJA/UUf_oroDoQI/AAAAAAAANx4/bY48cgFDa-Q/s1600-h/image177.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-u4cymb3wuzc/UUf_pAWG-zI/AAAAAAAANyA/1JjlI_W9fNs/image_thumb85.png?imgmax=800" width="482" height="307"></a><br>同様の操作で、「2006」と「2007」の[値]を追加していきます。 <li>次に、[既定値]ページを開いて、既定値を指定します。<br><a href="http://lh6.ggpht.com/-g1-9o-oYv6E/UUf_pj-4ObI/AAAAAAAANyI/MO2YzKK4WlI/s1600-h/image181.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-FYzP1KHF2G0/UUf_qObbroI/AAAAAAAANyQ/fjKwBDwNaXI/image_thumb87.png?imgmax=800" width="340" height="301"></a><br>[値の指定]を選択して、[追加]ボタンをクリックし、[値]へ「2005」と入力します。同様の操作で、「2006」と「2007」の[値]を追加していきます。 <li>設定後、プレビューを表示して、結果を確認してみましょう。<br><a href="http://lh4.ggpht.com/-U2-cwGKEAco/UUf_qc9LLQI/AAAAAAAANyY/YBm9ucU0yyk/s1600-h/image185.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-m2atMf7szxg/UUf_qwWWMDI/AAAAAAAANyg/RpK85HOnxnY/image_thumb89.png?imgmax=800" width="484" height="166"></a><br>レポートの上部に[年度]のパラメーター ボックスが表示されることを確認できます。 <li>パラメーター ボックスで任意の年度を複数選択して(2006と2007など)、[レポートの表示]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-AFIVXd5BLkI/UUf_raAfcNI/AAAAAAAANyo/OixCXDc67PA/s1600-h/image189.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-lTYkpbVIxDA/UUf_rzVLuSI/AAAAAAAANyw/5qXTaCHKit4/image_thumb91.png?imgmax=800" width="363" height="191"></a><br>2006年と2007年のデータのみが表示されることを確認できます。<br>このように、クエリで IN 演算子でパラメーターを定義して、[複数値を許可]するように設定すれば、複数の値を選択できるようになります。</li></ol> <h5 id="id007">レポート サーバーへレポートの保存</h5> <h6>レポート サーバーへレポートの保存</h6> <p>以上で、この Step のレポート作成が完了ですが、作成したレポートは、次の Step でも使用するので、名前を付けてレポート サーバー上へ保存しておきます。</p> <ol> <li>レポート ビルダー メニューで[名前を付けて保存]をクリックして、「SalesRepo」という名前でレポート サーバー上へ保存します。<br><a href="http://lh4.ggpht.com/-zCGrphYiAX0/UUf_seVvZnI/AAAAAAAANy4/mwBTB3DohbQ/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-vcOTrUZ0oQE/UUf_swjYUII/AAAAAAAANzA/1gcIVfoDImg/image_thumb%25255B1%25255D.png?imgmax=800" width="350" height="177"></a></li></ol> <h6>レポート マネージャーからパラメーターの確認</h6> <p>レポート サーバー上に保存したパラメーター付きのレポートは、レポート マネージャーからパラメーターを確認/変更することもできます。これも確認しておきましょう。</p> <ol> <li>まずは、Internet Explorer を起動して、次のように URL を入力して、レポート マネージャーへアクセスします。<br> <table cellspacing="0" cellpadding="7" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="620">http://サーバー名/Reports</td></tr></tbody></table><br><a href="http://lh5.ggpht.com/-VlpOjmFrSJI/UUf_tHNN8cI/AAAAAAAANzI/IVkB2paM2mQ/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-xj-g3JPcBsw/UUf_tlGwqxI/AAAAAAAANzQ/Jw8YfYIZ-sc/image_thumb%25255B3%25255D.png?imgmax=800" width="372" height="266"></a><br>レポートの一覧が表示されたら、「SalesRepo」の[▼]ボタンをクリックして、[管理]をクリックします。 <li>次に、[パラメーター]ページをクリックして開くと、パラメーターの既定値やプロンプト(文字列の表示)などを変更することができます。<br><a href="http://lh4.ggpht.com/-57qYF4Mf4r8/UUf_uX4fmAI/AAAAAAAANzY/CEtQgM2pRwc/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-BX2EVv2G0HI/UUf_u4K6vYI/AAAAAAAANzg/rt7QEOtvSS0/image_thumb%25255B5%25255D.png?imgmax=800" width="610" height="156"></a></li></ol> <p><a href="http://lh6.ggpht.com/-_MKvYocItwc/UUf_vVKhlhI/AAAAAAAANzo/sfFK8AdUMHQ/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwbz6IYMqPud8JfB0ZrvgDh-noii7OWbf4wdRi0nzLOzrqeRef02Xc6_6u_EbVJaFAWMvtO3j8w8h9XrSCVpiIEeR4ytOQNuCJ2NWriKxTt_82oKyBKmXUZbF28ojMJ94ePtg1XIOrFz45/?imgmax=800" width="649" height="429"></a></p> <p><a href="http://lh6.ggpht.com/-v3N8XGh4U0U/UUf_wYddfLI/AAAAAAAANz4/zPAYH6zGXvc/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYCQxKZ6IRP-4OcoNw8SjRM0sEjElegKmLIlBiI-3OBmhOSlfWsgBR5gYE_RAOcmVmO6ExcEdj1q-z4eSDAiHtnQiB6Pq8Hc65rSj21N_84ksbst1S8hvjKt9mXnNKU3gs4MZp51fiODiK/?imgmax=800" width="648" height="479"></a></p> <p><a href="http://lh4.ggpht.com/-BrLifDCobSc/UUf_xVJDzdI/AAAAAAAAN0I/WCMFjSQ2yfg/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hvKHiQQukQM/UUf_yGGpBiI/AAAAAAAAN0Q/Sj_P0_HzJ3c/image_thumb%25255B13%25255D.png?imgmax=800" width="510" height="274"></a></p> <p><a href="http://lh5.ggpht.com/-I3RiW2PCrFs/UUf_yn1znEI/AAAAAAAAN0Y/yiJlynZbsQk/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-x-GSUcOfd-c/UUf_zG_qbYI/AAAAAAAAN0g/G0hpD9fTYiw/image_thumb%25255B15%25255D.png?imgmax=800" width="512" height="387"></a></p> <h5 id="id008">ジャンプ レポートの作成</h5> <h6>ジャンプ レポートの作成</h6> <p>Reporting Services のレポートは、次のように別のレポートへジャンプさせることも簡単に行うことができます。</p> <p><a href="http://lh3.ggpht.com/-cKH27E2xc4A/UUf_ziEFUAI/AAAAAAAAN0o/_GEpPC0cG9k/s1600-h/image%25255B34%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3F49bLmpzLAlr4Y2et3cRTEXIwQUKUHeOtkDilmgqvJun-F_4cMMTeGsQmm_Ki9PC05Wj7ANbz4qDb3BmxYnX8-c63UfxZNv_u_nrAkWur7ih9y4tr30Gb4E1WUk388MbkIlkg1hU3O8s/?imgmax=800" width="501" height="232"></a></p> <p>グラフやレポート内の任意の値をクリックしたときに、その値をジャンプ先となるレポートのパラメーターへ代入する値として渡すことができます。</p> <ol> <li>ジャンプ先のレポートには、この Step で作成した SalesRepo レポートを利用します。<br><a href="http://lh5.ggpht.com/-oGZ7aw6lKoc/UUf_0hD_2VI/AAAAAAAAN04/8hCXEpKT47A/s1600-h/image%25255B39%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-khrHPk-5yrU/UUf_1HelU4I/AAAAAAAAN1A/mXYj9XzVX_8/image_thumb%25255B20%25255D.png?imgmax=800" width="593" height="164"></a> <li>ジャンプ元のレポートには、次のように区分ごとの受注金額を「円グラフ」で表示するレポートを利用します。<br><a href="http://lh6.ggpht.com/-ExdV8vUR1AY/UUf_1sEMckI/AAAAAAAAN1I/u5JqIP6KE-4/s1600-h/image%25255B43%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-wTAHfA4iopY/UUf_2DGcE7I/AAAAAAAAN1Q/40sDtjBdHdc/image_thumb%25255B22%25255D.png?imgmax=800" width="587" height="257"></a><br>作成方法は、Step 3 で作成したグラフとほとんど同じで、[Σ値]へ「受注金額」、[カテゴリ グループ]へ「区分名」を設定して、区分ごとの受注金額を円グラフで表示するようにします。<br><a href="http://lh6.ggpht.com/-TM6dAPQ_kEQ/UUf_2pu8UKI/AAAAAAAAN1Y/QA9yIz7Jejo/s1600-h/image%25255B47%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-NDLqzdgCgPk/UUf_3BHtgaI/AAAAAAAAN1g/m2VwGn7VD-Q/image_thumb%25255B24%25255D.png?imgmax=800" width="587" height="300"></a><br>上画面のレポートは、データ ラベルを設定していますが、これを設定する方法については、Step 11 で説明します。 <li>ジャンプ先となるレポートを設定するには、次のようにグラフを選択して右クリックし、[系列のプロパティ]をクリックします。<br><a href="http://lh4.ggpht.com/-pIVANZXbjUs/UUf_3lISa8I/AAAAAAAAN1o/NjXDgbCGi3w/s1600-h/image%25255B51%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-TVV-j2jMi9k/UUf_4F056HI/AAAAAAAAN1w/OBO6If5NL4Q/image_thumb%25255B26%25255D.png?imgmax=800" width="431" height="248"></a> <li>[系列のプロパティ]ダイアログが開いたら、[アクション]ページを開いて、[レポートに移動する]を選択し、[レポートの指定]で[参照]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-j1h4mNiitHM/UUf_4i5gXMI/AAAAAAAAN14/oPNyCG---1o/s1600-h/image%25255B55%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-8U_p-DZJukA/UUf_5CE2OFI/AAAAAAAAN2A/C8-OIDjFkPw/image_thumb%25255B28%25255D.png?imgmax=800" width="423" height="207"></a> <li>[レポートの選択]ダイアログが表示されたら、レポート サーバー上の「SalesRepo」レポートを選択して、[開く]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-GGCu3_jvmrk/UUf_5q5WmoI/AAAAAAAAN2I/bzCgKWSR8xM/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Dhhh3mfKXtM/UUf_6DRPftI/AAAAAAAAN2Q/fZkyyckPJIs/image_thumb%25255B30%25255D.png?imgmax=800" width="413" height="229"></a> <li>[系列のプロパティ]ダイアログへ戻ったら、[レポートの実行に使用するパラメーター]では、[追加]ボタンをクリックして、[名前]で「p1」、[値]で「[区分コード]」を選択し、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-MjVNxPcaHUs/UUf_6n9ax5I/AAAAAAAAN2Y/EBuNe3Jl5jY/s1600-h/image%25255B63%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdBUJLgCt_SZv6ivVqg4VOEGd8PU6illmDjd1QyF_YbdyZQAY7MJ5TMA6ww832LlBL7igGPGowXkOtmfFL_QP37eQ38BVIWiKAyTUGHpG0iXOXhok3IZUIuLSIGeZJBclffZXS8mm5KS-G/?imgmax=800" width="413" height="265"></a><br>以上で、ジャンプ レポートの設定が完了です。<br>なお、上記ダイアログで、パラメーターへ渡す値として「区分コード」を選択するには、データセットのクエリとして、次のように「区分コード」を取得しておく必要があります(Step 2 で作成した共有データセットは、区分コードを取得していないため、新しいデータセットを作成する必要があります)。<br><a href="http://lh3.ggpht.com/-5EQGDoXRoDA/UUf_7Vo6VBI/AAAAAAAAN2o/7RViRX3t0ks/s1600-h/image%25255B67%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-1ILsSdjARug/UUf_75NYhDI/AAAAAAAAN2w/3z1nAwrX9Kk/image_thumb%25255B34%25255D.png?imgmax=800" width="417" height="363"></a> <li>設定後、レポートをレポート サーバー上へ保存します。 <li>Internet Explorer でレポートへアクセスし、ジャンプ レポートが正しく動作することを確認しておきましょう。<br><a href="http://lh4.ggpht.com/-UwlQXaezhZ8/UUf_8WcLbfI/AAAAAAAAN24/9CWO_4zizJE/s1600-h/image%25255B71%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-veFHUP_DZLk/UUf_9KSK2DI/AAAAAAAAN3A/OXjN0EwL0Mc/image_thumb%25255B36%25255D.png?imgmax=800" width="415" height="232"></a><br></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-10472365225658188682013-03-18T16:01:00.001+09:002013-03-18T16:01:14.733+09:00◆データバー、インジケーター、スパークラインの利用<ul> <li><a href="#id001">データバー、インジケーター、スパークライン</a> <li><a href="#id002">データバーの追加</a> <li><a href="#id003">インジケーターの追加</a> <li><a href="#id004">スパークラインの追加</a> </li></ul> <h5 id="id001">データバー、インジケーター、スパークライン</h5> <h6>データバー、インジケーター、スパークライン</h6> <p>次に、SQL Server 2008 R2 から提供された機能である「データバー」や「インジケーター」、「スパークライン」を含む商品区分別受注レポートを作成してみましょう。</p> <p><a href="http://lh4.ggpht.com/-EBavrwu9SRo/UUa7OvyPM_I/AAAAAAAANco/DaipF9dMODU/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-4G6k15nScM4/UUa7PN8U9UI/AAAAAAAANcw/7gsbccrlVXY/image_thumb%25255B1%25255D.png?imgmax=800" width="649" height="222"></a></p> <p>それでは、これを試してみましょう。</p> <ol> <li>まずは、レポート ビルダーを起動します。 <li>[作業の開始]ページが表示されたら、[新しいレポート]の[空のレポート]をクリックします。<br><a href="http://lh4.ggpht.com/-G3ZQ3Y8sPGE/UUa7PpSfFqI/AAAAAAAANc4/R5WGqk0s-4c/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ATNEnJzX-C4/UUa7QG-S_TI/AAAAAAAANdA/aVTgDPcGJ08/image_thumb%25255B3%25255D.png?imgmax=800" width="336" height="253"></a> <li>レポートのデザイン画面が表示されたら、次のようにデザイン上の任意の場所を右クリックして、[挿入]から[マトリックス]をクリックします。<br><a href="http://lh4.ggpht.com/-IhMp3UUwX8Y/UUa7QfVcMCI/AAAAAAAANdI/ptCUZI5BC3E/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAhVxUoXpiB1pLitRrvNUBlcbAfUKZAWHLliMEdEMoC9gGfvQYqRLh47luJCsVpzA37gUIe1J9nYCD0NM7bdFwarbNz8rsxfMp9lJMNRfLLZ2GXnjltncgyNeZ1EaNsiv3g1Zugfectbgy/?imgmax=800" width="334" height="270"></a> <li>[データセットのプロパティ]ダイアログが表示されたら、Step2 で作成した共有データセット(ds_受注クエリ)を選択して、[OK]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-Gxon7YEwJTA/UUa7RWKbsGI/AAAAAAAANdY/dYUrQ-lPw8E/s1600-h/image%25255B16%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-MNHBlsMS0v4/UUa7R7OQyNI/AAAAAAAANdg/NpHJOhc1MN0/image_thumb%25255B8%25255D.png?imgmax=800" width="333" height="204"></a> <li>次に、配置したマトリックスの[行グループ]へ「区分名」、[列グループ]へ「年」、[データ]へ「受注金額」フィールドを配置します。<br><a href="http://lh5.ggpht.com/-nu0RRMed5Ng/UUa7STL9-kI/AAAAAAAANdo/PVX9-8P9iKk/s1600-h/image%25255B20%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-CWDQ4gEIeUQ/UUa7S5s5FTI/AAAAAAAANdw/abn9odawzcg/image_thumb%25255B10%25255D.png?imgmax=800" width="330" height="285"></a> <li>次に、区分と年の合計値を追加します。<br><a href="http://lh3.ggpht.com/-rPgEvBJKWDc/UUa7THvEtRI/AAAAAAAANd4/uix7tyzZVYc/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-QDGaY0rA2xY/UUa7TvzEicI/AAAAAAAANeA/EwlAV_m-oac/image_thumb%25255B14%25255D.png?imgmax=800" width="547" height="712"></a> <li>次に、「Sum(受注金額)」のセルを選択してリボンの[$](通貨記号)ボタンをクリックし、受注金額を通貨形式(¥マーク付き)で表示するように変更します。<br><a href="http://lh6.ggpht.com/-UiPcq9aWslU/UUa7UDFPZQI/AAAAAAAANeI/td4O4mjjOY0/s1600-h/image%25255B30%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-g3o3IXa2jSc/UUa7UlMP1BI/AAAAAAAANeQ/fv9yrN74KcM/image_thumb%25255B16%25255D.png?imgmax=800" width="342" height="236"></a> <li>リボンの[実行]ボタンをクリックして、ここまでの状態を確認してみましょう。<br><a href="http://lh3.ggpht.com/-3ZHEPV8P8oI/UUa7VLHUGzI/AAAAAAAANeY/LNl4i5Nz8L4/s1600-h/image%25255B34%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7cp67-HxJYRHeNB2W1OK8KthCKRLQPkYMq0eTuG10yeq5tHDT1tE2SQ0i1JFHd2BPuWY9xFuemnWYRAuz2fn8oxZqDzy39VvsVH9w930td3p1ESU1Wz-YpmC3TgdVhv0OkXSdICgbIVeY/?imgmax=800" width="338" height="264"></a></li></ol> <h6>2005 年のみのデータに絞り込む</h6> <p>次に、作成したマトリックスが 2005 年のデータのみを表示するように変更します。</p> <ol> <li>マトリックスのデータを 2005 年のデータのみに絞り込むには、次のように、マトリックスの左上にあるコーナー ハンドルを右クリックして、[Tablix のプロパティ]をクリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlfWRU0H7OVfoNKYJPUi80lfuAXJen4KToCxQoTzSwhF6cA41CwvBwuj3DhY1Smgh41snTGjKPYVAmEUI2WfQ-P9rNhyphenhyphenesl78DYAIuFzLjzkB568RBgZsE-vKVUv7tTE3dlb-vLyFoIECh/s1600-h/image%25255B38%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-yg42bJd1lII/UUa7WdOohdI/AAAAAAAANew/owpiwZMQpng/image_thumb%25255B20%25255D.png?imgmax=800" width="408" height="309"></a><br>[Tablix のプロパティ]ダイアログが表示されたら、[フィルター]ページで[追加]ボタンをクリックします。[式]で「[年]」を選択して、[値]へ「2005」と入力して、[OK]ボタンをクリックします。 <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみましょう。<br><a href="http://lh4.ggpht.com/-qoVvy9Yz-3M/UUa7W3amylI/AAAAAAAANe4/6ZjKEEK661U/s1600-h/image%25255B42%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-_oA9c0R5uno/UUa7XJOIUBI/AAAAAAAANfA/Hfmjjad8sqI/image_thumb%25255B22%25255D.png?imgmax=800" width="396" height="136"></a></li></ol> <h6>四半期ごとのデータを表示</h6> <p>次に、マトリックスに四半期ごとの受注金額を表示するように変更しましょう。</p> <ol> <li>四半期ごとの受注金額の合計を表示するには、次のように「四半期」フィールドを[列グループ]ペインの「年」の下にドラッグ&ドロップします。<br><a href="http://lh4.ggpht.com/-HqjwfeEFUwI/UUa7Xn8n2EI/AAAAAAAANfI/NCqBDRFiqfI/s1600-h/image%25255B46%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Qw9XWctqz_I/UUa7YG79jzI/AAAAAAAANfQ/hGnH8pDoFZQ/image_thumb%25255B24%25255D.png?imgmax=800" width="393" height="247"></a> <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみましょう。<br><a href="http://lh5.ggpht.com/-PqJrr_JHboA/UUa7YffZBWI/AAAAAAAANfY/b71-7z1OlF4/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-CJVYyIiaZ1s/UUa7Y2QyZaI/AAAAAAAANfg/95Qx5xRXFJ4/image_thumb%25255B26%25255D.png?imgmax=800" width="399" height="223"></a><br>確認後、デザイン画面へもどります。 <li>次に、レポートのスタイル(フォントや背景色、文字揃えなど)を任意に変更します。</li></ol> <h5 id="id002">データバーの追加</h5> <h6>データバーの追加</h6> <p>次に、データバーを追加します。</p> <ol> <li>データバーを追加するために、次のようにマトリックスの「年」列を右クリックして[列を挿入]から[右揃え]をクリックし、列を追加します。<br><a href="http://lh3.ggpht.com/-g8xxgSQFOq8/UUa7ZMxA7kI/AAAAAAAANfo/HTDMKnJdarA/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPuG1U0A0rw5UEzRmiOzViE8Tob7_fmGLeBNccQqu_PzqcaTxMxEGwOYECmr8cQ9w1xQAU3aXGsyupAiqcALnLMadYrd6uDbXlWrnxdLP4ji1bg30eQfAzBl1D3nzR4OaRuctx23Rot6FJ/?imgmax=800" width="409" height="147"></a> <li>次に、挿入した列の「区分名」行のセル上で右クリックして、[挿入]から[データバー]をクリックします。<br><a href="http://lh5.ggpht.com/-ls_lX3YVrRY/UUa7Zx7nhwI/AAAAAAAANf4/mFcRDcaOnfY/s1600-h/image%25255B58%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-oG0AWkTOz8Q/UUa7afsL_YI/AAAAAAAANgA/P3vy2meCpH8/image_thumb%25255B30%25255D.png?imgmax=800" width="416" height="383"></a> <li>次のように[データバーの種類を選択]ダイアログが表示されたら、任意のデータバーを選択して、[OK]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-N4buiZEcab0/UUa7a3252_I/AAAAAAAANgI/rn-W_UuR6WI/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-a2C5_Bz5bkk/UUa7bX9O_4I/AAAAAAAANgQ/Ukw0RxwQtrM/image_thumb%25255B31%25255D.png?imgmax=800" width="244" height="238"></a> <li>続いて、次のようにセルのデータバーをクリックして[グラフ データ]ペインを表示します。<br><a href="http://lh5.ggpht.com/-ZlWTYN9_EFk/UUa7bgKA6SI/AAAAAAAANgY/c8_-jr9cWpc/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-9yMqNlkwbaQ/UUa7b-Kx19I/AAAAAAAANgg/o1P67D2aPDg/image_thumb%25255B33%25255D.png?imgmax=800" width="393" height="221"></a><br>[グラフ データ]ペインの[Σ 値]で[+]ボタンをクリックして、[受注金額]フィールドをクリックします。これで、[Σ 値]へ[受注金額]フィールドが追加されます。 <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみます。<br><a href="http://lh3.ggpht.com/-BmKLjLgArwk/UUa7cWF_OzI/AAAAAAAANgo/tTNVutRkZ_Q/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-0cfV9ErhI3U/UUa7ctzbk-I/AAAAAAAANgw/5NMq_Jx23qg/image_thumb%25255B35%25255D.png?imgmax=800" width="388" height="119"></a><br>受注金額に応じたデータバーが表示されることを確認できます。<br>確認後、デザイン画面へもどります。</li></ol> <h6>データバーの色の設定</h6> <p>データバーは、任意の色へ変更することもできます。</p> <ol> <li>データバーの色を変更するには、セル内のデータバーを右クリックして、[系列のプロパティ]をクリックします。<br><a href="http://lh5.ggpht.com/-Tz5Gq39WFlo/UUa7dPnSh-I/AAAAAAAANg4/YeupdU_P9jY/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-jc1oyqjIOXQ/UUa7dnMM-mI/AAAAAAAANhA/jyuKJJvynd0/image_thumb%25255B37%25255D.png?imgmax=800" width="385" height="253"></a> <li>[系列のプロパティ]ダイアログが表示されたら、[塗りつぶし]ページで、[グラデーション]など任意の塗りつぶしのスタイルを選択して色を指定します(今回の例では、一番目の[色]で「オレンジ」、[2番目の色]で「白」、[グラデーションのスタイル]で「左から右」を選択)。<br><a href="http://lh6.ggpht.com/-AY2J-wJAixY/UUa7eA0jYbI/AAAAAAAANhI/3WcNDn0EnlE/s1600-h/image%25255B77%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-VVUC0arViHM/UUa7ej177EI/AAAAAAAANhQ/m7IAGUecZRU/image_thumb%25255B39%25255D.png?imgmax=800" width="381" height="246"></a> <li>リボンの[実行]ボタンをクリックして、実行結果を確認すると、次のようになります。<br><a href="http://lh4.ggpht.com/-VVAxgkF_CE0/UUa7fPXV44I/AAAAAAAANhY/B8M8DEEra3M/s1600-h/image%25255B81%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-3EiFrjHKXXg/UUa7f-HYpeI/AAAAAAAANhg/uZVylh9k1hE/image_thumb%25255B41%25255D.png?imgmax=800" width="381" height="120"></a><br>確認後、デザイン画面へもどります。</li></ol> <h5 id="id003">インジケーターの追加</h5> <h6>インジケーターの追加</h6> <p>次に、インジケーターを追加してみましょう。</p> <ol> <li>インジケーターを追加するために、まずは、次のようにマトリックスのデータバーを挿入した列を右クリックして[列を挿入]から[右揃え]をクリックし、列を追加します。<br><a href="http://lh5.ggpht.com/-i1VlVE2TLfg/UUa7gQgZ-vI/AAAAAAAANho/0CE9IbZixWs/s1600-h/image%25255B85%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Y7AWy5nTNIc/UUa7g15qNRI/AAAAAAAANhw/eoX0iG0ms3g/image_thumb%25255B43%25255D.png?imgmax=800" width="371" height="120"></a> <li>次に、追加した列の「区分名」行のセル上で右クリックして、[挿入]から[インジケーター]をクリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZTMqAjg2Tk5i8_hBcfJRTcEBuqa9l1rm-U8f-t02pALdyRFxzm6U_6barF3wXx6n7Bln91LOSnizzXL974YvY1ZvFPZOLLK44t9IeH9E63Jr_HfQRJDE2Njj8oJJr3rHgSPKcNprfUgA5/s1600-h/image%25255B90%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi08EB1P93_KBa3_iagqFeHFORl9BhC4cxQvUeZudUyIMkNAW6GW5t7RtSqk4qrhjc7jOmdogDvaRZXgh2p_dfMhY6T2cmSRpk4M2vPjjIXY5l-efV75aSPFatCyqQpOxpD5yK0xESrsZxa/?imgmax=800" width="589" height="394"></a> <li>[インジケーターの種類の選択]ダイアログが表示されたら、任意のインジケーターを選択して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-edDiMRFNGcA/UUa7h0VbH6I/AAAAAAAANiI/a1yoKS44s-w/s1600-h/image%25255B93%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-A0HyQV4pFGQ/UUa7iTY-R-I/AAAAAAAANiQ/Gi7f94zwdqQ/image_thumb%25255B47%25255D.png?imgmax=800" width="237" height="146"></a><br>これで、マトリックスのセル内にインジケーターを追加できます。 <li>次に、追加したインジケーターをクリックして、[ゲージ データ]ペインを表示します。<br><a href="http://lh5.ggpht.com/-TXDAyw4WIT4/UUa7ihK8ZJI/AAAAAAAANiY/yK_fWp4QCGY/s1600-h/image%25255B97%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-34P-iBnsXD0/UUa7jUZMvEI/AAAAAAAANig/Bo0CXbhHr4Y/image_thumb%25255B49%25255D.png?imgmax=800" width="585" height="280"></a><br>[ゲージ データ]ペインの[Σ 値]で[▼]ボタンをクリックして、[受注金額]フィールドをクリックします。これで、[Σ 値]を[受注金額]フィールドへ設定することができます。 <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみます。<br><a href="http://lh5.ggpht.com/-XnRMQy_bl8g/UUa7jqBYIzI/AAAAAAAANio/D5-qeMPDzlA/s1600-h/image%25255B101%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-EM04DKds55k/UUa7kQ8lhoI/AAAAAAAANiw/KJTSBueiQgg/image_thumb%25255B51%25255D.png?imgmax=800" width="581" height="240"></a><br>受注金額の大きさに応じて異なるインジケーターが表示されることを確認できます。<br>確認後、デザイン画面へもどります。<br>なお、インジケーターの形状は、既定では、Excel 2007/2010 でのアイコンセット機能と同様、全体のパーセンテージをもとに 0~33、33~66、66~100% の3段階で区別されます。これを変更したい場合は、次のように[インジケーターのプロパティ]ダイアログの[値と状態]ページから行います。<br><a href="http://lh5.ggpht.com/-lT4rWWnZ0Mg/UUa7klo0XbI/AAAAAAAANi4/jtp89tPy8IE/s1600-h/image%25255B104%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-rKcmQwhl-yk/UUa7lH6y38I/AAAAAAAANjA/zpmwGOiZrwk/image_thumb%25255B52%25255D.png?imgmax=800" width="244" height="182"></a></li></ol> <h5 id="id004">スパークラインの追加</h5> <h6>スパークラインの追加</h6> <p>次に、スパークラインを追加してみましょう。</p> <ol> <li>スパークラインを追加するために、まずは、次のようにマトリックスの一番右の「合計」列を右クリックして、[列を挿入]から[右揃え]をクリックし、列を追加します。<br><a href="http://lh6.ggpht.com/-VVXyiNuczw4/UUa7lT36_HI/AAAAAAAANjI/IYGN_FOmovo/s1600-h/image%25255B108%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-M2rCUftuJ6E/UUa7l8ZlrjI/AAAAAAAANjQ/0g31O-wm5DU/image_thumb%25255B54%25255D.png?imgmax=800" width="582" height="124"></a> <li>次に、追加した列のセル上で右クリックして、[挿入]から[スパークライン]をクリックします。<br><a href="http://lh3.ggpht.com/-RpYjy-TroHw/UUa7mNYYePI/AAAAAAAANjY/lpI6G1RXwOU/s1600-h/image%25255B113%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-6rYoMS7HyMA/UUa7mtyn4BI/AAAAAAAANjg/roiSlasmoF0/image_thumb%25255B57%25255D.png?imgmax=800" width="508" height="359"></a> <li>[スパークラインの種類の選択]ダイアログが表示されたら、縦棒のスパークラインを選択して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-lSNsKXpANck/UUa7m446n9I/AAAAAAAANjo/zphH-4UGKN4/s1600-h/image%25255B116%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-bb9Ue74SmQA/UUa7nRmIcEI/AAAAAAAANjw/HDY25EnJWnI/image_thumb%25255B58%25255D.png?imgmax=800" width="161" height="107"></a><br>これで、マトリックスのセル内にスパークラインを追加できます。 <li>次に、追加したスパークラインをクリックして、[グラフ データ]ペインを表示します。<br><a href="http://lh4.ggpht.com/-GZu3sdFMElc/UUa7nnX3gQI/AAAAAAAANj4/2r0_nPRhXh8/s1600-h/image%25255B120%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Eu18z-KI5bo/UUa7oHU6GkI/AAAAAAAANkA/z3nnV-QzcxE/image_thumb%25255B60%25255D.png?imgmax=800" width="385" height="212"></a><br>[グラフ データ]ペインの[Σ 値]で[+]ボタンをクリックして、[受注金額]フィールドをクリックします。これで、[Σ 値]へ[受注金額]フィールドを設定できます。 <li>次に、[カテゴリ グループ]の(詳細)の「▼」ボタンをクリックして、[四半期]フィールドを選択します。<br><a href="http://lh4.ggpht.com/-RO77WEARYeY/UUa7oasUaXI/AAAAAAAANkI/bWo-1kUemlA/s1600-h/image%25255B124%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-zvKiyDiARes/UUa7o51OM5I/AAAAAAAANkQ/rriPjcV0SL8/image_thumb%25255B62%25255D.png?imgmax=800" width="386" height="252"></a><br>これにより、[カテゴリ グループ]に配置されていた(詳細)を「四半期」へ変更することができます。 <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみます。<br><a href="http://lh3.ggpht.com/-7VZBgpgxeu8/UUa7pVi7ZiI/AAAAAAAANkY/f-08z_1uqqQ/s1600-h/image%25255B128%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-QqPREQ18_ts/UUa7pzFeUDI/AAAAAAAANkg/IgyC04NURro/image_thumb%25255B64%25255D.png?imgmax=800" width="380" height="335"></a><br>四半期ごとの受注金額がスパークラインとして表示されていることを確認できます。<br>確認後、デザイン画面へもどります。</li></ol> <h6>スパークラインのカテゴリ グループを変更する</h6> <p>次に、スパークラインのカテゴリ グループを変更して、月ごとの受注合計金額の傾向が確認できるようにしてみましょう。</p> <ol> <li>スパークラインのカテゴリ グループを変更するには、次のように、スパークラインをクリックして、[グラフ データ]ペインを表示します。<br>(この例だと四半期に月を追加しているが、月だけと結果は同じになるようだ。複数指定しても一番細かいレンジが表示されるのかな?)<br><a href="http://lh4.ggpht.com/-WNerBm57G68/UUa7qbK4sdI/AAAAAAAANko/kpZNTLigu8I/s1600-h/image%25255B132%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-6Cl7kYgIAA0/UUa7q7KOeoI/AAAAAAAANkw/lSQfDCT02EA/image_thumb%25255B66%25255D.png?imgmax=800" width="386" height="313"></a> <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみます。<br><a href="http://lh4.ggpht.com/-dZrzv7g_JVU/UUa7rIF5i4I/AAAAAAAANk4/N7yvZDP_sVM/s1600-h/image%25255B136%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGqKFnFkb22og9EgWeK6atIvINP7Iqr0GPp4YW3-GAEUZ_V1fZ8Bv3nXLOAXsn_k8UaAWdPIcTjyR1J9SdDCC1eARzI1LM3HfXvXYQihXSXo_iKtOFKDM5m85g_714V6n-mVLflUM-uYbD/?imgmax=800" width="595" height="98"></a><br>月ごと(12ヶ月分)の受注金額がスパークラインとして表示されていることを確認できます。<br>確認後、デザイン画面へもどります。</li></ol> <h6>スパークラインの種類を変更する</h6> <p>次に、スパークラインの種類を変更して、月ごとの受注金額の傾向を折れ線グラフで確認できるようにしてみましょう。</p> <ol> <li>スパークラインの種類を変更するには、次のようにセル内のスパークラインを右クリックして、[スパークラインの種類の変更]をクリックします。<br><a href="http://lh5.ggpht.com/-tvoFuuG2pHA/UUa7r0Afo8I/AAAAAAAANlI/2jtCPgQc9Qs/s1600-h/image%25255B140%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-_aAkXKSZbNU/UUa7sZ754VI/AAAAAAAANlQ/OenJZdXp-tQ/image_thumb%25255B70%25255D.png?imgmax=800" width="498" height="128"></a> <li>[スパークラインの種類を選択]ダイアログが表示されたら、折れ線のスパークラインを選択して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-jMU8b9m3trg/UUa7slD_UvI/AAAAAAAANlY/1DvyuwdSISM/s1600-h/image%25255B143%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-s3YdYIWBTls/UUa7tFjm8YI/AAAAAAAANlg/TLncduhZWg4/image_thumb%25255B71%25255D.png?imgmax=800" width="244" height="130"></a> <li>リボンの[実行]ボタンをクリックして、実行結果を確認してみます。<br><a href="http://lh6.ggpht.com/-DY80PF9xk5g/UUa7tlqpRiI/AAAAAAAANlo/kCLgXHzBMa8/s1600-h/image%25255B147%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-XpoKjKlTiHw/UUa7uNdffgI/AAAAAAAANlw/hl4SiTIaTl0/image_thumb%25255B73%25255D.png?imgmax=800" width="490" height="93"></a><br>スパークラインが折れ線グラフで表示されていることを確認できます。<br>確認後、デザイン画面へもどります。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com1tag:blogger.com,1999:blog-7901251401713078028.post-3321177767405534352013-03-17T16:40:00.001+09:002013-03-17T16:40:38.916+09:00◆イベント ハンドラー<h5>イベント ハンドラーの利用</h5> <p>イベント ハンドラーを利用するとパッケージ実行時のイベント(エラーや警告など)の内容に応じて、タスクを実行できるようになります。この Step では、パッケージのエラー発生時に、スクリプト タスクを実行するようにしてみましょう。</p> <h6>イベント ハンドラーの設定</h6> <p>ここでは、前の Step で作成した SSISoyo2 プロジェクトを引き続き利用します。</p> <ol> <li>イベント ハンドラーを設定するには、次のように[イベント ハンドラー]タブをクリックします。<br><a href="http://lh3.ggpht.com/-_UcttPRygZQ/UUVxZRk4P8I/AAAAAAAANZY/NfKaY50wSUY/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-We9ZOWH39DQ/UUVxg4VlaCI/AAAAAAAANZg/yMSR6GSX1K4/image_thumb%25255B1%25255D.png?imgmax=800" width="453" height="109"></a></li> <li>今回は、エラー発生時に実行するタスクを設定するので、[実行可能ファイル]で[Package]、[イベント ハンドラー]で「OnError」が選択されていることを確認して、[実行可能ファイル 'Package' の 'OnError' イベント ハンドラーを作成するにはここをクリックします]をクリックします。<br><a href="http://lh6.ggpht.com/--qVLvkjcD34/UUVxlhcKRmI/AAAAAAAANZo/lpgi4ib5ydE/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Zf2NnuXAENM/UUVxs0-9MFI/AAAAAAAANZw/zXhTGwT6UdY/image_thumb%25255B3%25255D.png?imgmax=800" width="433" height="244"></a></li> <li>これにより、次のようにイベント ハンドラー デザイナーが表示されるので、ここで、エラー発生時に実行するタスクを設定していきます。今回は、SSIS ツール ボックスの[共通]カテゴリから[スクリプト タスク]をドラッグ&ドロップして、配置します。<br><a href="http://lh6.ggpht.com/-vAJERh_bbUg/UUVxxoSOrdI/AAAAAAAANZ4/u1DjgMOfIO4/s1600-h/image%25255B11%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-7I_MCTK7sUg/UUVx4x8bDgI/AAAAAAAANaA/wt0R_YZqaYU/image_thumb%25255B5%25255D.png?imgmax=800" width="432" height="159"></a></li> <li>配置した[スクリプト タスク]をダブル クリックします。<br><a href="http://lh5.ggpht.com/-WpbTOBIoRZ0/UUVx99kMd2I/AAAAAAAANaI/oeKArUHlz6M/s1600-h/image%25255B14%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-7aaozJ01CD4/UUVyE36qPPI/AAAAAAAANaQ/fyc9_Kua8lk/image_thumb%25255B6%25255D.png?imgmax=800" width="244" height="80"></a></li> <li>[スクリプト タスク エディター]ダイアログが表示されたら、今回も Visual Basic でスクリプトを記述するので、[ScriptLanguage]で「Microsoft Visual Basic 2010」を選択します。<br><a href="http://lh5.ggpht.com/-UF0SJv9dr1c/UUVyJwG4-sI/AAAAAAAANaY/Lq_RTKXeMBg/s1600-h/image%25255B18%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-HECVtx-1axg/UUVyQ85_NyI/AAAAAAAANag/LSQLO9ADEVs/image_thumb%25255B8%25255D.png?imgmax=800" width="422" height="136"></a></li> <li>続いて、[ReadOnlyVariables]で[...]ボタンをクリックして、[変数の選択]ダイアログを表示し、システム変数の一覧から「System::ErrorCode」(エラー番号)と「System::ErrorDescription」(エラーの説明)をチェックします。<br><a href="http://lh6.ggpht.com/-8cPjBkVxyVE/UUVyV85dRpI/AAAAAAAANao/HJfjP_bLoeU/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Xk7OhOB3x00/UUVydCzcBDI/AAAAAAAANaw/1QgTDz9zAGE/image_thumb%25255B11%25255D.png?imgmax=800" width="602" height="420"></a></li> <li>[スクリプト タスク エディター]へ戻ったら、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-LS6dqQifLe4/UUVyiBEl89I/AAAAAAAANa4/rbe_yYF7ouk/s1600-h/image%25255B26%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-N2vedGcSqtM/UUVypcxpblI/AAAAAAAANbA/ifgKHlWbT1Y/image_thumb%25255B12%25255D.png?imgmax=800" width="244" height="112"></a></li> <li>[スクリプト エディター]が表示されたら、「public Sub Main( )」内のコードを、次のように入力します(変数名の大文字と小文字に注意してください)。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">MessageBox.Show(Dts.Variables("System::ErrorCode").Value.ToString() _ & ":" & Dts.Variables("ErrorDescription").Value.ToString())</td></tr></tbody></table><a href="http://lh5.ggpht.com/-a6fRIzFA6Xc/UUVyuCaTcNI/AAAAAAAANbI/DZpLqxYyto4/s1600-h/image%25255B32%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpQbtbnG7JfPuZjbhavGGJsyn3B4hdxTFh0FEuc74_zlOlCcJVosgGw-ZSNsTKwnWvZy5XkJX5gUlqlEG7zmU18ydaOLf7fs09o1fgvyfTnve7TfNWmix1pYQjzWcSpn8uEj9OQx_b0mMJ/?imgmax=800" width="633" height="172"></a>このコードにより、エラー番号とエラーの説明をメッセージ ボックスで表示できるようになります。<br>入力後、保存してスクリプト エディターを閉じます。</li> <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。<br><a href="http://lh6.ggpht.com/-rgupfvPDHEU/UUVy6aePaMI/AAAAAAAANbY/htgtwMyQbK8/s1600-h/image%25255B35%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hlWB_-KDwwY/UUVzBawWOPI/AAAAAAAANbg/7OZwn9XKOG4/image_thumb%25255B17%25255D.png?imgmax=800" width="244" height="239"></a></li> <li>パッケージをデバッグ実行して確認します。<br>配置されている[スクリプト タスク]が実行中に変わり、エラー番号とエラーの説明がメッセージ ボックスで表示されることを確認できます。<br><a href="http://lh4.ggpht.com/-on0viLN4KZg/UUVzGfdRdsI/AAAAAAAANbo/IKoKDU3ECvw/s1600-h/image%25255B39%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-X09tekRO054/UUVzNwfuQhI/AAAAAAAANbw/3jFgDr95fuk/image_thumb%25255B19%25255D.png?imgmax=800" width="425" height="350"></a><br>このメッセージ ボックスは、エラー(OnError イベント)が発生した数の分だけ表示されるので、その分[OK]ボタンをクリックします。</li> <li>すべてのメッセージ ボックスが表示され、[スクリプト タスク]に緑のチェックマークが付いたら、デバッグを終了します。</li> <li>最後に、[ファイル]メニューの[すべてを保存]をクリックして、SSISoyo2 プロジェクトを保存しておきます。このプロジェクトは、次の Step でも引き続き使用します。<br>このように、イベント ハンドラーを利用すれば、パッケージのエラー発生時などに応じて、タスクを実行できるようになるので、大変便利です。</li></ol> <p>Note: エラー発生時にメール送信(メール送信タスク)<br>OnError イベント ハンドラーや OnTaskFailed(タスクの失敗)イベント ハンドラーでは、メール送信タスクを利用すると、エラー発生時にメールを送信できるようになるので便利です。<br>メール送信タスクでは、次のように「SMTP サーバー」の名前と「From」(送信元のメール アドレス)、「To」(送信先のメール アドレス)、「件名」を設定するだけでメールを送信することができます。</p> <p><a href="http://lh4.ggpht.com/-ZEKoJmLmnQQ/UUVzTN22cdI/AAAAAAAANb4/dVcWvXCkHbc/s1600-h/image%25255B47%25255D.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-9jOaR402wMk/UUVzbEVwEyI/AAAAAAAANcA/e1gID2AOx3k/image_thumb%25255B25%25255D.png?imgmax=800" width="617" height="941"></a></p> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-12995596469468477112013-03-17T00:13:00.001+09:002013-03-17T00:13:19.741+09:00◆ログ記録<ul> <li><a href="#id001">実習を始める前に</a> <li><a href="#id002">ログ記録の設定</a> <li><a href="#id003">ログ記録の詳細設定</a> <li><a href="#id004">エラー発生時のログ記録の確認</a> </li></ul> <h5 id="id001">実習を始める前に</h5> <h6>転送先となるデータベースの作成</h6> <p>この Step では、SSIS デザイナーを使用して、テキスト データを SQL Server 内へ取り込んでいくので、取り込み先(転送先)となる空のデータベースを作成しておきます。次のようにManagement Studio で[データベース]フォルダーを右クリックして、[新しいデータベース]をクリックし、「ssisdb3」という名前のデータベースを作成します。</p> <p><a href="http://lh5.ggpht.com/-2GpeubL7YMg/UUSFAanrHzI/AAAAAAAANQA/rq3M1AZ56OI/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-Shdz_CnmKOg/UUSFHtGTW2I/AAAAAAAANQI/3wSXyT-WFGA/image_thumb%25255B1%25255D.png?imgmax=800" width="630" height="155"></a></p> <h6>インポートするファイル</h6> <p>インポートするファイル(ABC.txt と MISS.txt)は、次のように、タブ区切りのファイルで、サンプル スクリプトの import フォルダー内へ格納してあります。</p> <p><a href="http://lh5.ggpht.com/-NRm0aY_px54/UUSFMtUyJ0I/AAAAAAAANQQ/rKW_3HoZhn8/s1600-h/image%25255B7%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-1duOo-mZ8jo/UUSFT91tVSI/AAAAAAAANQY/7D6C2v79yEA/image_thumb%25255B3%25255D.png?imgmax=800" width="479" height="218"></a></p> <h5 id="id002">ログ記録の設定</h5> <h6>ログ記録の設定</h6> <p>まずは、テキスト ファイル(ABC.txt)を SQL Server へインポートするパッケージを作成して、実行時にログが記録される設定にしてみましょう。</p> <ol> <li>SQL Server Data Tools を起動して、[スタート ページ]の[新しいプロジェクト]をクリックし、新しいプロジェクトを作成します。<br><a href="http://lh6.ggpht.com/--QNPQ9PS8PU/UUSFY1jS0cI/AAAAAAAANQg/eF81Lq4l1dg/s1600-h/image%25255B10%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-uTYxkwKvCe4/UUSFgc94wqI/AAAAAAAANQo/cJVGpu5FIJM/image_thumb%25255B4%25255D.png?imgmax=800" width="189" height="150"></a> <li>[新しいプロジェクト]ダイアログでは、[インストールされたテンプレート]で「ビジネス インテリジェンス プロジェクト」の[Integration Services]をクリックし、「Integration Services プロジェクト」を選択します。<br><a href="http://lh5.ggpht.com/-MwBL-xGC34E/UUSFlNhIAXI/AAAAAAAANQw/A1oFwcvRRgw/s1600-h/image%25255B15%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-LWdOmQ3O_KU/UUSFsQyJwnI/AAAAAAAANQ4/OdOH5MMiU2w/image_thumb%25255B7%25255D.png?imgmax=800" width="553" height="125"></a><br>[名前]へ任意のプロジェクト名(画面は SSISoyo2)、[場所]へ任意の保存場所(画面は C:\)を入力して、[OK]ボタンをクリックします。 <li>次に、データ転送を設定するために、SSIS ツールボックスの[お気に入り]カテゴリから[データ フロー タスク]を SSIS デザイナー上へドラッグ&ドロップします。<br><a href="http://lh4.ggpht.com/-feeIShgJn4o/UUSFxX-2YuI/AAAAAAAANRA/x9N2Tviu_SM/s1600-h/image%25255B28%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-pskEHhjPIDw/UUSF4qmIiQI/AAAAAAAANRI/PA_WcUDHXog/image_thumb%25255B14%25255D.png?imgmax=800" width="458" height="165"></a> <li>配置した[データ フロー タスク]をダブル クリックして、[データ フロー]タブを表示します。<br><a href="http://lh4.ggpht.com/-sbiUrE7lqK8/UUSF9Uqa47I/AAAAAAAANRQ/Neu9neRndSA/s1600-h/image%25255B23%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-nyL97s3KFxg/UUSGE_h-6wI/AAAAAAAANRY/84HU-jx7dYY/image_thumb%25255B11%25255D.png?imgmax=800" width="454" height="184"></a> <li>次に、転送元データとしてテキスト ファイルを指定するため、SSIS ツールボックスの[その他の変換元]カテゴリから、[フラット ファイル ソース]を SSIS デザイナー上へドラッグ&ドロップします。<br><a href="http://lh6.ggpht.com/-CQmhjBCy2QU/UUSGJkPL5PI/AAAAAAAANRg/Odxn_ETKcoQ/s1600-h/image%25255B27%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-oyhhHblaSdI/UUSGQzoBxMI/AAAAAAAANRo/7WxlLiVjhf0/image_thumb%25255B13%25255D.png?imgmax=800" width="450" height="201"></a> <li>配置した[フラット ファイル ソース]をダブル クリックして、[フラット ファイル ソースエディター]ダイアログを表示します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1W5dwW4JNOAu9D4nj9PX9XlbbJ3SdRCqVcIkAxCy8pIvBrJsJjfcMsLXzV6ALVIcU_Tfb5sbEZOnZcFl5ohADnc8KrGfENc9INk5biBMGNlK56GYLKraIxAUCjL9v4hejCN7_Z40O2oxu/s1600-h/image%25255B32%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-wTwFVmylUNg/UUSGc5sUVCI/AAAAAAAANR4/Hlh_D7zB23Y/image_thumb%25255B16%25255D.png?imgmax=800" width="440" height="100"></a><br>このダイアログでは、転送元のテキスト ファイルを指定するために[新規作成]ボタンをクリックします。 <li>これにより、[フラット ファイル接続マネージャー エディター]ダイアログが表示されるので、[接続マネージャー名]へ任意の名前(テキストファイルへの接続 など)と入力し、[参照]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-45eoC8ZwJ6c/UUSGh-rME5I/AAAAAAAANSA/M9EkDQqI2Io/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-E5lZCHlqulY/UUSGpSbxG9I/AAAAAAAANSI/LpQkvV-_jhw/image_thumb%25255B18%25255D.png?imgmax=800" width="444" height="349"></a><br>[開く]ダイアログが表示されたら、サンプル スクリプト内の「import」フォルダー内にある「ABC.txt」ファイルを選択して、[開く]ボタンをクリックします。 <li>[フラット ファイル接続マネージャー エディター]ダイアログへ戻ったら、[先頭データ行を列名として使用する]のチェックを外します。<br><a href="http://lh5.ggpht.com/-4Upr884te4k/UUSGueM_YmI/AAAAAAAANSQ/hUDbLtMV1ms/s1600-h/image%25255B40%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-mqQr1CcUBCE/UUSG18m-_iI/AAAAAAAANSY/MKiytkN1dFc/image_thumb%25255B20%25255D.png?imgmax=800" width="441" height="359"></a> <li>続いて、「列」ページをクリックして、[列区切り記号]で「タブ {t}」が選択されていることを確認します(ABC.txt ファイルはタブ区切りのファイルです)。<br><a href="http://lh5.ggpht.com/-_QPvTVPAaIw/UUSG6obSf9I/AAAAAAAANSg/zxJNns1nHsY/s1600-h/image%25255B44%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-2BoFfvL4_aU/UUSHB2l1atI/AAAAAAAANSo/ChHoZnVZ5og/image_thumb%25255B22%25255D.png?imgmax=800" width="387" height="275"></a><br>[1~3行のプレビュー]で転送元のデータを確認します。このファイルには、列見出しの行がないので、列名が自動的に「列0」、「列1」と設定されています。 <li>次に、[詳細設定]ページをクリックして開きます。<br><a href="http://lh4.ggpht.com/-G7v8mJ8N4ls/UUSHHB2bc5I/AAAAAAAANSw/pGX_W2PEpT4/s1600-h/image%25255B49%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-i-_Bjy8Vh6Q/UUSHOUx9A3I/AAAAAAAANS4/hxPOKMYrpuo/image_thumb%25255B25%25255D.png?imgmax=800" width="511" height="186"></a><br>テキスト ファイルの場合、デフォルトでは「文字列」(DT_STR:string)データ型としてデータが転送されますが、今回は、「列0」列(1 番左の列)のデータを数値として転送するように、「列0」列の[DataType]を「4 バイト符号付き整数」(SQL Server での int 型に相当するデータ型)へ変更します。変更後、[OK]ボタンをクリックします。<br>[フラット ファイル ソース エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、転送先のデータベースを指定するために、SSIS ツールボックスの[その他の変換先]カテゴリから、[SQL Server 変換先]を SSIS デザイナー上へドラッグ&ドロップします。<br><a href="http://lh6.ggpht.com/-teHhq1Ql4XI/UUSHTFUFmnI/AAAAAAAANTA/h5yQ0GX-fEc/s1600-h/image%25255B54%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-KNV9NQqWn4Y/UUSHac0mwiI/AAAAAAAANTI/vXl0kiNU1Co/image_thumb%25255B28%25255D.png?imgmax=800" width="496" height="172"></a><br>続いて、転送元と転送先を関連付けるために、[フラット ファイル ソース]の青色の矢印を[SQL Server 変換先]まで、ドラッグ&ドロップして伸ばします。 <li>次に、転送先の詳細設定を行うために、[SQL Server 変換先]をダブル クリックします。<br><a href="http://lh5.ggpht.com/-_9SkK7AwP40/UUSHfearEVI/AAAAAAAANTQ/KEtCdYr65IQ/s1600-h/image%25255B57%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-RQxR9B2Sx7Q/UUSHmUX_SbI/AAAAAAAANTY/WX3-xkQLzsE/image_thumb%25255B29%25255D.png?imgmax=800" width="244" height="128"></a> <li>[SQL 変換先エディター]ダイアログが表示されたら、[接続マネージャー]で[新規作成]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-sbUtt7znWfc/UUSHrSPsDRI/AAAAAAAANTg/0Uemj9xh2qo/s1600-h/image%25255B61%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-n-YCiauleQc/UUSHy5P0mrI/AAAAAAAANTo/ykyuOQz6lu4/image_thumb%25255B31%25255D.png?imgmax=800" width="350" height="253"></a><br>[OLE DB 接続マネージャーの構成]ダイアログが表示されたら、[新規作成]ボタンをクリックします。 <li>[接続マネージャー]ダイアログでは、[サーバー名]へ SQL Server の名前、[データベース名の選択または入力]で「ssisdb3」データベースを選択して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-ErGWv9i4elA/UUSH3osVnOI/AAAAAAAANTw/SQ8yX8P_FGs/s1600-h/image%25255B65%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-tQbCryR9lyg/UUSH-xBpk1I/AAAAAAAANT4/BP6fdNd3DNs/image_thumb%25255B33%25255D.png?imgmax=800" width="251" height="325"></a><br>[OLE DB 接続マネージャーの構成]ダイアログへ戻ったら、[OK]ボタンをクリックして、閉じます。これで、ssisdb3 データベースへ接続できるようになります。 <li>次に、データの転送先となるテーブルを作成するために、次のように[SQL 変換先エディター]の[テーブルまたはビューを使用]で[新規作成]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-WEMN6aCB9CY/UUSIDt8QxAI/AAAAAAAANUA/oktUOjHAOIg/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-XhYzw-xs1_c/UUSIKwe9HQI/AAAAAAAANUI/cgemIbm6vBg/image_thumb%25255B35%25255D.png?imgmax=800" width="579" height="136"></a><br>これにより、[テーブルの作成]ダイアログが表示されて、転送元のデータをもとにテーブルを作成するための CREATE TABLE ステートメントが表示されます。今回は、テーブル名を「ABC」へ変更して、[OK]ボタンをクリックします。<br>[OK]ボタンをクリックしたタイミングで、このステートメントが実行されて、ssisdb3 データベース内へ「ABC」テーブルが作成されます。 <li>[SQL 変換先エディター]ダイアログへ戻ったら、[マッピング]ページをクリックし、転送元のデータと転送先のデータのマッピングを設定します。<br><a href="http://lh6.ggpht.com/-Y3qfG_Cemeo/UUSIP-DiCBI/AAAAAAAANUQ/3wv-CJyeaNA/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Fc0gXahaktk/UUSIWxBfRoI/AAAAAAAANUY/hdC4FTabG0E/image_thumb%25255B37%25255D.png?imgmax=800" width="406" height="138"></a><br>同じ名前の列同士がマッピングされていることを確認して、[OK]ボタンをクリックします。<br>これで、ABC.txt ファイルのデータが ssisdb3 データベースへインポートすることができるようになりました。</li></ol> <h6>ログ記録の設定</h6> <p>続いて、パッケージの実行時に、ログがテキスト ファイルに記録されるようにしてみましょう。</p> <ol> <li>次のように、[SSIS]メニューから[ログ記録]をクリックします。<br><a href="http://lh6.ggpht.com/-RGZ2N73PUbY/UUSIbydWA7I/AAAAAAAANUg/iUBh8f5UfcY/s1600-h/image%25255B76%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-vloduI8s59A/UUSIi86KeNI/AAAAAAAANUo/d236qtJw-nY/image_thumb%25255B38%25255D.png?imgmax=800" width="241" height="70"></a> <li>これにより、[SSIS ログの構成]ダイアログが表示されるので、[プロバイダーの種類]で「テキスト ファイルの SSIS ログ プロバイダー」を選択して、[追加]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-t9DA-llkWn4/UUSIn12wqdI/AAAAAAAANUw/dPHPauJuvo8/s1600-h/image%25255B81%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-J1FMEUJFqnU/UUSIu8oIQAI/AAAAAAAANU4/17JrB1HCAac/image_thumb%25255B41%25255D.png?imgmax=800" width="585" height="171"></a><br>次に、追加したログの[構成]で、「新しい接続」をクリックします。 <li>[ファイル接続マネージャー エディター]が表示されたら、[使用法の種類]で、ログ記録用のファイルを新しく作成するために「ファイルの作成」を選択し、[ファイル]で任意のファイル名(ここでは、C:\logging1.txt)と入力して、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/--q_QxB4LQCo/UUSIz2z0lqI/AAAAAAAANVA/Bre5WojnfHc/s1600-h/image%25255B85%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-2N5jpxtF454/UUSI7NCNOjI/AAAAAAAANVI/Gc-_1DYbMpg/image_thumb%25255B43%25255D.png?imgmax=800" width="488" height="181"></a> <li>[SSIS ログの構成]ダイアログへ戻ったら、[コンテナー]で「Package」をチェックして、[コンテナーに対して使用するログを選択します]で追加したログをチェックし、[OK]ボタンをクリックします<br><a href="http://lh3.ggpht.com/-3U8aRCA7Al8/UUSJACrS7cI/AAAAAAAANVQ/hELiGsAsq4Y/s1600-h/image%25255B89%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmW17YxPBiNV6KGeXrEJH7_TkjIYRYPpANWYloTljthXhMi3UTyytiTYYcYALuNJk-WCUBuYkFXHxjEB1ciACZ9y2gTH0W1az4BxnLiq7WjolsiO9qh7Lpgwmjm5IMbMSgdEMgcGcoHl8x/?imgmax=800" width="488" height="222"></a> <li>ツールバーの[デバッグ開始]ボタンをクリックしてパッケージを実行し、ここまでの設定を確認します。<br><a href="http://lh6.ggpht.com/-yVqNzZ3-bk4/UUSJMRZ0h6I/AAAAAAAANVg/ID-gKwwvJMY/s1600-h/image%25255B92%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-SC6qeypc3ek/UUSJTqeYNPI/AAAAAAAANVo/q7q7CiY4C-s/image_thumb%25255B46%25255D.png?imgmax=800" width="172" height="166"></a><br>配置されているオブジェクトに緑のチェックマークが付いて、データの転送が成功したことを確認したら、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。 <li>もう 1度、デバッグを開始して、パッケージを合計で 2回実行します。 <li>データが正常に転送されたことを確認するには、Management Studio から、ssisdb3 データベースの[ABC]テーブルを右クリックして、[上位 1000 行の選択]をクリックし、データを確認します。<br><a href="http://lh6.ggpht.com/-ez3EJgzjyvA/UUSJYRnAuqI/AAAAAAAANVw/igH1X8PZk4o/s1600-h/image%25255B96%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-fb8NKCK9m-8/UUSJfnNNwFI/AAAAAAAANV4/q-g02VDqacg/image_thumb%25255B48%25255D.png?imgmax=800" width="389" height="164"></a><br>パッケージを 2回実行しているので、2回分のデータがインポートできていることを確認できます。 <li>次に、Windows エクスプローラーを起動して、C:\logging1.txt ファイルをダブル クリックして開き、記録されたログを確認します。<br><a href="http://lh5.ggpht.com/-8BNGCZWQZG8/UUSJkriazsI/AAAAAAAANWA/E1VDYtVEb80/s1600-h/image%25255B102%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-B59adrcvm-4/UUSJr35jacI/AAAAAAAANWI/i-Rr7GzwwJQ/image_thumb%25255B52%25255D.png?imgmax=800" width="590" height="296"></a><br>パッケージを実行した開始時刻(PackageStart)と終了時刻(PackageEnd)が 2回分記録されていることを確認することができます。このように、ログ記録を設定すると、パッケージの実行に関するログを簡単に取得できるようになります。</li></ol> <h5 id="id003">ログ記録の詳細設定</h5> <h6>ログ記録の詳細設定</h6> <p>前の手順のログ記録では、パッケージの開始時刻と終了時刻にみの記録でしたが、ログ記録では、それ以外の情報(エラー情報など)も記録することができます。それはこれを試してみましょう。</p> <ol> <li>ログ記録の設定を変更するには、[SSIS]メニューから[ログ記録]をクリックします。<br><a href="http://lh4.ggpht.com/-A31okPryfy4/UUSJw2xYzlI/AAAAAAAANWQ/YtnOsZusBHs/s1600-h/image%25255B107%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Ev1Pjt5YYeQ/UUSJ35xDYeI/AAAAAAAANWY/a6g2_RQBAQE/image_thumb%25255B55%25255D.png?imgmax=800" width="314" height="98"></a> <li>[SSIS ログの構成]ダイアログが表示されたら、[詳細]タブをクリックしてページを開き、ログを記録するイベントを選択します。<br><a href="http://lh5.ggpht.com/-JMo2wAbf7a8/UUSJ83QdqWI/AAAAAAAANWg/PdrgIIoJM7s/s1600-h/image%25255B110%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnIO5uRVG-jduRJgDCMLOy-gqUcEaGvwC4wOjMae-yA8aiJYIUtEN7oLkrHNVws5KVFmRp1jpngYDGaEdmST6PSGvvdszDEDpadlDiFkCmqu8xDen1s7Fz7-hY_RBjFjBWAiQT1tPo8H7L/?imgmax=800" width="244" height="155"></a><br>ここでは、「OnError」(エラーの発生)、「OnInformation」(情報メッセージ)、「OnTaskFailed」(タスクの失敗)、「OnWarning」(警告)をチェックして、[OK]ボタンをクリックします。 <li>パッケージを実行して、ここまでの設定を確認します。<br><a href="http://lh4.ggpht.com/-RU0eXwfU_eM/UUSKJGH701I/AAAAAAAANWw/2HzPp-MxaMc/s1600-h/image%25255B113%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-8eBNJSx8luA/UUSKQeD0BiI/AAAAAAAANW4/K3glIpdU2YU/image_thumb%25255B57%25255D.png?imgmax=800" width="164" height="161"></a><br>配置されているオブジェクトに緑のチェックマークが付いて、データの転送が成功したことを確認したら、デバッグを終了します。 <li>次に、Windows エクスプローラーから、C:\logging1.txt ファイルを開いて、記録されたログを確認します。<br><a href="http://lh3.ggpht.com/-FaROBLOe0FA/UUSKVLWWARI/AAAAAAAANXA/WPNNLXA31l0/s1600-h/image%25255B118%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-SAFVvRUAvVQ/UUSKcmekq-I/AAAAAAAANXI/-CNA1qnWgEY/image_thumb%25255B60%25255D.png?imgmax=800" width="606" height="202"></a><br>パッケージを実行した開始時刻(PackageStart)と終了時刻(PackageEnd)に加えて、タスクの情報(OnInformation)が記録されていることを確認できます。今回は、正常にデータが転送されているので、OnError や OnTaskFailed、OnWarning については、記録されていません。<br><a href="http://lh6.ggpht.com/-b_BHcR-G7F8/UUSKhUPsLZI/AAAAAAAANXQ/v0fDgY7RCYQ/s1600-h/image%25255B122%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-EaiD0xOg2E8/UUSKoiWxWxI/AAAAAAAANXY/3o-OpeQa3EE/image_thumb%25255B62%25255D.png?imgmax=800" width="461" height="223"></a></li></ol> <h5 id="id004">エラー発生時のログ記録の確認</h5> <h6>エラー発生時のログ記録の確認</h6> <p>次に、転送元のテキスト ファイルを「MISS.txt」ファイルへ変更して、転送時にデータ型に関するエラーが発生するようにして、エラー発生時にどういったログが記録されるのかを確認してみましょう。</p> <ol> <li>転送元のテキスト ファイルを変更するには、次のように[データ フロー]タブの画面下部の[接続マネージャー]に一覧されている「テキストファイルへの接続」(前の Step で作成したもの)をダブル クリックします。<br><a href="http://lh5.ggpht.com/-mHEx_Jl65m0/UUSKtQhHwHI/AAAAAAAANXg/S0b1JWPfRp0/s1600-h/image%25255B126%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-8-3tXMoVajE/UUSK1CTdd6I/AAAAAAAANXo/xMJZavP-TK8/image_thumb%25255B64%25255D.png?imgmax=800" width="364" height="304"></a> <li>これにより、[フラット ファイル接続マネージャー エディター]ダイアログが表示されるので、[ファイル名]の[参照]ボタンをクリックして、サンプル スクリプト内の「import」フォルダー内にある「MISS.txt」ファイルへ変更します。<br><a href="http://lh5.ggpht.com/-ny5pSRTp_3Q/UUSK5wflx5I/AAAAAAAANXw/2zyEnyiuBoY/s1600-h/image%25255B131%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-kgbFkx4wiJU/UUSLBUCw4VI/AAAAAAAANX4/lxEqy5BUrfo/image_thumb%25255B67%25255D.png?imgmax=800" width="601" height="390"></a> <li>続いて、次のように「列」ページをクリックして、[1~3行のプレビュー]で転送元となるデータを確認します。<br><a href="http://lh6.ggpht.com/-Ff0OooRRK-s/UUSLGPd9B0I/AAAAAAAANYA/-WLRk8fiCUo/s1600-h/image%25255B134%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-o3gBJhbf3Uw/UUSLNSF4YbI/AAAAAAAANYI/tQnruXCAbGU/image_thumb%25255B68%25255D.png?imgmax=800" width="244" height="223"></a><br>1 列目の「列0」列には、文字データが含まれていることに注目します。 <li>続いて、[詳細設定]ページをクリックして開き、前の Step で設定したように「列0」列の[DataType]が「4バイト符号付き整数」(SQL Server の int 型に相当)になっていることを確認します。確認後、[OK]ボタンをクリックして、[フラット ファイル接続マネージャー エディター]ダイアログを閉じます。<br><a href="http://lh4.ggpht.com/-3qhscaPFBQU/UUSLSbtiwFI/AAAAAAAANYQ/AffN7MXJpvA/s1600-h/image%25255B138%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-tix773615KQ/UUSLZlZAihI/AAAAAAAANYY/jUZpeOJpx0w/image_thumb%25255B70%25255D.png?imgmax=800" width="483" height="279"></a> <li>パッケージを実行して、結果を確認します。<br><a href="http://lh5.ggpht.com/-jv2eODla6MQ/UUSLeRenhiI/AAAAAAAANYg/SYNTgdUbA4c/s1600-h/image%25255B141%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-egfHmHYMW_0/UUSLl3LPlHI/AAAAAAAANYo/bt7SbVPufWU/image_thumb%25255B71%25255D.png?imgmax=800" width="227" height="203"></a><br>今度は、[フラット ファイル ソース]がエラーになって、データ転送が失敗していることを確認できます。 <li>確認後、デバッグを終了します。 <li>続いて、[実行結果]タブをクリックして、実行結果を確認します。<br><a href="http://lh3.ggpht.com/-8kHBlSWXp08/UUSLq1fHteI/AAAAAAAANYw/hZgv4QxZ0k4/s1600-h/image%25255B146%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-n0gj9CwjWqI/UUSLzGU9huI/AAAAAAAANY4/3hbAXJV6pPw/image_thumb%25255B74%25255D.png?imgmax=800" width="604" height="574"></a><br>「データ変換に失敗しました。列 "列0" の~」というエラーが発生して、MISS.txt の「列0」でのデータ型の変換エラーによって、パッケージ実行が失敗したことを確認できます。これは、列0(1 列目)のデータ型を int 型に設定しているにも関わらず、文字データが含まれているために発生しています。 <li>次に、Windows エクスプローラーから、C:\logging1.txt ファイルを開いて、記録されたログを確認します。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieMdb_el_v_VbYLZieLsIXiQ9y9FJrcGXZU7nr7pKFky5luIVYXsx9n0XLtl0rXRJau5GYMVmsyc5HXsrAv0ZZ5nL2FTs6wGd4a5N3FtjTUVTkxV6tteitQf9a0Zq74djg4y5eolY9nqrj/s1600-h/image%25255B150%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/--XPNOaJDZxg/UUSMBF4ODlI/AAAAAAAANZI/a22PMwQedgw/image_thumb%25255B76%25255D.png?imgmax=800" width="526" height="382"></a><br>今回はデータ転送が失敗したので、エラーの発生(OnError)とタスクの失敗(OnTaskFailed)が記録されることを確認できます。<br>このように、ログ記録を設定しておくと、パッケージの実行エラーを記録することができるので、大変便利です。 <li>最後に、[ファイル]メニューの[すべてを保存]をクリックして、SSISoyo2 プロジェクトを保存しておきます。このプロジェクトは、次の Step でも引き続き使用します。</li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0tag:blogger.com,1999:blog-7901251401713078028.post-26609392117628129102013-03-15T23:45:00.001+09:002013-03-15T23:45:31.811+09:00◆スクリプト タスクと変数の利用<ul> <li><a href="#id001">スクリプト タスクの実行</a> <li><a href="#id002">スクリプト タスクからファイルへの書き込み</a> <li><a href="#id003">ユーザー定義の変数の利用</a> <li><a href="#id004">SQL 実行タスクの結果を変数へ格納</a> <li><a href="#id005">SQL ステートメントのパラメーター化と変数の引き渡し</a> </li></ul> <h5 id="id001">スクリプト タスクの実行</h5> <h6>スクリプト タスクの実行</h6> <p>スクリプト タスクを利用すると、Visual C# 2010 や Visual Basic 2010 で記述したスクリプトを実行できるようになります。ここでは、Visual Basic 2010 を利用して簡単なスクリプトを記述し、実行できることを確認してみましょう。</p> <ol> <li>まずは、[スタート]メニューの[すべてのプログラム]から、[Microsoft SQL Server 2012]の[SQL Server Data Tools]をクリックして、SQL Server Data Tools を起動します。<br><a href="http://lh6.ggpht.com/-y2Bvc2R1ORQ/UUMmXFjaAyI/AAAAAAAAM94/pjG9Bju_mrs/s1600-h/image10.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-G5mOUVV6mcU/UUMmeTLXjDI/AAAAAAAAM-A/emnUlHmbFVQ/image_thumb4.png?imgmax=800" width="294" height="319"></a> <li>SQL Server Data Tools(Visual Studio 2010)が起動したら、[スタート ページ]の[新しいプロジェクト]をクリックして、新しいプロジェクトを作成します。<br><a href="http://lh5.ggpht.com/-oagqYj3KH5A/UUMmjfBEv6I/AAAAAAAAM-I/iCdgeNycV4s/s1600-h/image13.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-L9Pk4CfPbIg/UUMmqqeEAjI/AAAAAAAAM-Q/PFMOf7ZTHJU/image_thumb5.png?imgmax=800" width="222" height="160"></a> <li>[新しいプロジェクト]ダイアログが表示されたら、次のように[インストールされたテンプレート]で[ビジネス インテリジェンス]の[Integration Services]をクリックして、「Integration Services プロジェクト」を選択します。<br><a href="http://lh5.ggpht.com/-pSxCCtoH0eY/UUMmvo5zAgI/AAAAAAAAM-Y/Xlk9JhAvD_g/s1600-h/image18.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-QV7UCVx2GWk/UUMm2xYaMcI/AAAAAAAAM-g/QF0cHbxoKrU/image_thumb8.png?imgmax=800" width="583" height="390"></a><br>[名前]へ任意のプロジェクト名(画面は SSISoyo1)、[場所]へ任意の保存場所(画面は C:\)を入力して、[OK]ボタンをクリックします。これにより、Integration Services プロジェクトが作成されます。 <li>次に、SSIS ツールボックスの[共通]カテゴリから[<font color="#ff80ff">スクリプト タスク</font>]を SSIS デザイナー上へドラッグ&ドロップして配置します。<br><a href="http://lh4.ggpht.com/-K7wbfuQyXA0/UUMm79W5jBI/AAAAAAAAM-o/O_nUUzkQLJE/s1600-h/image23.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-g3XMv8si7IU/UUMnDI8IgqI/AAAAAAAAM-w/G4v0U-JdkK0/image_thumb11.png?imgmax=800" width="580" height="179"></a> <li>続いて、配置した[スクリプト タスク]をダブル クリックして、[スクリプト タスク エディター]ダイアログを表示します。<br><a href="http://lh5.ggpht.com/-d_mapKU2z_8/UUMnIM-BRcI/AAAAAAAAM-4/VC0e9rKvc_U/s1600-h/image27.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-GRjnt2s16Ho/UUMnPSz8jRI/AAAAAAAAM_A/liAI4h8DfzU/image_thumb13.png?imgmax=800" width="574" height="344"></a> <li>このダイアログでは、次のように[ScriptLanguage]で「Microsoft Visual Basic 2010」を選択して、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-ET0noIyOHoQ/UUMnUWLhrMI/AAAAAAAAM_I/UwW4uhkIav4/s1600-h/image32.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Vk4JG1PJOWs/UUMnbsohfwI/AAAAAAAAM_Q/pSzgm6HlMnE/image_thumb16.png?imgmax=800" width="443" height="366"></a><br>これで、Visual Basic 2010 でスクリプトを記述できるようになります。 <li>数秒後、次のように「スクリプト エディター」が起動されるので、「public Sub Main( )」内へ、以下のコードを記述します(コードが開かれていない場合は、プロジェクト エクスプローラーで[ScriptMain.vb]ファイルをダブル クリックして開きます)。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip1uDiEsq7ZfDEddZl_CgFztDw9zQJ5QSC1HMaXgqLNEeSXXM5hcdt73ah4CM5JS4ZYCn99HsNExpAkcIu9eijqmQ4-fW4pvqt0fNQQlnjo5JCQxOJm97uTXEGITz_sLmnU37X9CpUxeWZ/s1600-h/image35.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Tu3riehVUFo/UUMnnjBj0PI/AAAAAAAAM_g/EwHjLkCw-j4/image_thumb17.png?imgmax=800" width="244" height="34"></a><br><br><a href="http://lh4.ggpht.com/-hT1V6eakV7A/UUMnslLi8JI/AAAAAAAAM_o/rPbmKNgLzSQ/s1600-h/image39.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-DSK_JesqjYk/UUMn0e-oJII/AAAAAAAAM_w/rxLWYpXgF5E/image_thumb19.png?imgmax=800" width="436" height="453"></a><br>このコードにより、「Hello」という文字をメッセージ ボックスで表示できるようになります。<br>コードを記述後、ツールバーの[保存]ボタンをクリックしてスクリプトを保存し、スクリプト エディターを閉じます([×]ボタンで終了します)。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。<br><a href="http://lh5.ggpht.com/-gDVFtjJabMc/UUMn5U6sIeI/AAAAAAAAM_4/nID8nWRaozk/s1600-h/image%25255B3%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6fmfc3KhHRBtxDsNSRFhBOmDL5hs1i41RQtk5i-hC1p0DyATxhYCBUCiTNdRsrFazxcWeDOTPs9AXoeql0ZO0SFNXpnX_4oDhqra4YycG4eYMXxK1edCRvbEHEHxS6qvVid8Skzql1Mlf/?imgmax=800" width="439" height="365"></a> <li>次に、スクリプトを実行するために、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。<br><a href="http://lh4.ggpht.com/-fK3iJGLudig/UUMoFnYSSmI/AAAAAAAANAI/naQ1uVexXfE/s1600-h/image%25255B8%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-CDaDwW7fndo/UUMoMzSeJWI/AAAAAAAANAQ/wCNOiLZhvBA/image_thumb%25255B4%25255D.png?imgmax=800" width="602" height="258"></a><br>配置されている[スクリプト タスク]が実行中に変わって、メッセージ ボックスに「Hello」と表示されることを確認できます。<br><a href="http://lh3.ggpht.com/-xahRPLh4s7A/UUMoR3w8T6I/AAAAAAAANAY/40-VXQfTck8/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ECxQOgIlH8Y/UUMoZEwneXI/AAAAAAAANAg/gYVHdHVrgtg/image_thumb%25255B6%25255D.png?imgmax=800" width="603" height="221"></a><br>[OK]ボタンをクリックすると、[スクリプト タスク]に緑のチェックマークが付いて、スクリプトの実行が成功したことを確認できます。 <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。<br></li></ol> <h6>システム変数の利用</h6> <p>次に、スクリプト内で Integration Services のシステム変数を利用してみましょう。</p> <ol> <li>配置した[スクリプト タスク]をダブル クリックします。<br><a href="http://lh6.ggpht.com/-tJApIMKrBV8/UUMoeFeGd-I/AAAAAAAANAo/RTTKZY8gQ1o/s1600-h/image%25255B17%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhukEs2rinkM63Xpa5fOGNFZLtX0mtesB1Oc-N_qW9DozljiRkpcTfOMhkSGQOybZ_7ZeFJLzQM4WeDiAD-N0imQJfwg2ky4jpnCuuVxYfDDCV8QwzamNco8_A8_VdSOeRZldGrw2c6Xb7s/?imgmax=800" width="596" height="320"></a><br>[スクリプト タスク エディター]ダイアログが表示されたら、[ReadOnlyVariables]の[...]ボタンをクリックします。 <li>これにより、次のように[変数の選択]ダイアログが表示されて、「System::」で始まるシステム変数が一覧されます。<br><a href="http://lh6.ggpht.com/-hddXtGKbIx8/UUMoqIyGiFI/AAAAAAAANA4/pA5K033hxrM/s1600-h/image%25255B22%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-SHKJvHS0VmA/UUMoxaRuumI/AAAAAAAANBA/0TNnkPTvkVc/image_thumb%25255B12%25255D.png?imgmax=800" width="405" height="281"></a><br>今回は、この中から「System::UserName」をチェックして、[OK]ボタンをクリックします。UserName システム変数は、パッケージを実行しているユーザーの名前を取得することができます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-DxoE4mYRK04/UUMo2W6SUVI/AAAAAAAANBI/JF0mUCvPloI/s1600-h/image%25255B25%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-uG_AtL6a0KQ/UUMo9iBobKI/AAAAAAAANBQ/Agn0wVkDEJg/image_thumb%25255B13%25255D.png?imgmax=800" width="244" height="94"></a> <li>[スクリプト エディター]が開いたら、「public Sub Main( )」内のコードを、次のように変更します。<br><a href="http://lh6.ggpht.com/---JTlUQhG7Q/UUMpCc7JcQI/AAAAAAAANBY/mPRCXLeV368/s1600-h/image%25255B29%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-AmNOMPh_LhI/UUMpJlrDK9I/AAAAAAAANBg/ArJLXsEWYtI/image_thumb%25255B15%25255D.png?imgmax=800" width="612" height="39"></a><br><a href="http://lh5.ggpht.com/-jXYWV6CpFPY/UUMpOXg4idI/AAAAAAAANBo/28IoDvNEDiU/s1600-h/image%25255B33%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-YNLOUB7AOOk/UUMpVziZyWI/AAAAAAAANBw/xdlGRsThJRQ/image_thumb%25255B17%25255D.png?imgmax=800" width="616" height="236"></a>スクリプト タスク内では、「Dts.Variables("変数名")」と指定することで、変数を利用することができます。変数名の「System::UserName」は、大文字と小文字を区別するので、注意してください。このコードにより、メッセージ ボックスでユーザー名を表示できるようになります。<br>入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。<br><a href="http://lh5.ggpht.com/--9t5QsAJTTg/UUMpanIrO7I/AAAAAAAANB4/2J8ykxtX5ME/s1600-h/image%25255B36%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-RclxHFjRTxI/UUMphhIsq2I/AAAAAAAANCA/DaIfdroZEsc/image_thumb%25255B18%25255D.png?imgmax=800" width="244" height="83"></a> <li>次に、スクリプトを実行するために、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。(とあるが、実行前に保存するという昔は当たり前だった習慣を復活させたほうがよさそう。ここで異常終了してコーディングが消えてしまった。) <li>配置されている[スクリプト タスク]が実行中に変わって、実行しているユーザー名(画面は MOON\Adminitrator)がメッセージ ボックスで表示されることを確認できます。 <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。<br>このように、スクリプト タスクを利用すると、変数を扱うことができます。スクリプト タスクでは、ユーザー定義の変数を扱うこともできますが、これについては Step2.3 で説明します。</li></ol> <h5 id="id002">スクリプト タスクからファイルへの書き込み</h5> <h6>スクリプト タスクからファイルへの書き込み</h6> <p>次に、ファイルへの書き込みを行うスクリプトを作成してみましょう。</p> <ol> <li>まずは、配置した[スクリプト タスク]をダブル クリックして、[スクリプト タスク エディター]ダイアログを表示し、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-zxDS1R3tVl0/UUMpmr2TSfI/AAAAAAAANCI/93pEniMohbQ/s1600-h/image%25255B41%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-sMAPBRFmwHo/UUMpt_diLFI/AAAAAAAANCQ/yNvknKJlJHI/image_thumb%25255B21%25255D.png?imgmax=800" width="550" height="295"></a> <li>[スクリプト エディター]が表示されたら、「public Sub Main( )」内のコードを、次のように変更します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>Dim sw As New System.IO.StreamWriter("C:\test.txt", False)<br>sw.Write("XXXXX")<br>sw.Close()</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-dx48ChMGdp4/UUMpy9YEamI/AAAAAAAANCY/-0tLAaP_Ui8/s1600-h/image%25255B50%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-wOg5dV6yFJI/UUMp6Xs92FI/AAAAAAAANCg/haFaLIX6uzY/image_thumb%25255B26%25255D.png?imgmax=800" width="629" height="311"></a>このコードにより、「C:\text.txt」というファイルを作成して、「XXXXX」という文字を書き込むことができます。<br>入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、スクリプトを実行するために、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。<br><a href="http://lh3.ggpht.com/-r3nNMwZm0BY/UUMp_AqXX8I/AAAAAAAANCo/3Ew2R4ieZy0/s1600-h/image%25255B53%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-likpt-gG2SI/UUMqGdM7DHI/AAAAAAAANCw/_IcQwuexi-0/image_thumb%25255B27%25255D.png?imgmax=800" width="205" height="71"></a><br>配置されている[スクリプト タスク]に緑のチェックマークが付いて、スクリプトの実行が成功したことを確認できます。 <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。 <li>次に、Windows エクスプローラーを起動して、C:\text.txt ファイルをダブル クリックして開きます。 <li>「XXXXX」という文字が書き込まれていることを確認できます。<br>このように、スクリプト タスクでは、Visual Basic 2010 または Visual C# 2010 を利用して、任意の .NET コードを記述することができます。<br><a href="http://lh5.ggpht.com/-v6Tqr3y38RQ/UUMqLyzWA1I/AAAAAAAANC4/BzdcMrlACHA/s1600-h/image%25255B56%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-1IXEo_2jWKI/UUMqS7wp31I/AAAAAAAANDA/hHn3iMhMRPo/image_thumb%25255B28%25255D.png?imgmax=800" width="244" height="179"></a></li></ol> <h5 id="id003">ユーザー定義の変数の利用</h5> <h6>ユーザー定義の変数の利用</h6> <p>次に、ユーザー定義の変数を利用してみましょう。</p> <ol> <li>まずは、変数を定義します。次のように、[SSIS]メニューから[変数]をクリックして、[変数]ウィンドウを表示します。<br><a href="http://lh3.ggpht.com/-RFhi5yhu81U/UUMqX-D48KI/AAAAAAAANDI/eaiVPl_zb7A/s1600-h/image%25255B59%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-68FMn3oe2Eg/UUMqe-re--I/AAAAAAAANDQ/W1uqk5k3Qkg/image_thumb%25255B29%25255D.png?imgmax=800" width="244" height="83"></a><br><a href="http://lh6.ggpht.com/-S6Qi1no-8dw/UUMqj1K79EI/AAAAAAAANDY/hVknx9Ri5hI/s1600-h/image%25255B63%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-eHCJi8wXw5s/UUMqrDP7bhI/AAAAAAAANDg/Cni7NEnNzE4/image_thumb%25255B31%25255D.png?imgmax=800" width="382" height="165"></a> <li>続いて、ツールバーの[変数の追加]ボタンをクリックします。<br><a href="http://lh6.ggpht.com/-x2jPjskAMaw/UUMqwDZihsI/AAAAAAAANDo/yAki2AQfqvY/s1600-h/image%25255B66%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-jauCM13aNdw/UUMq3IztKlI/AAAAAAAANDw/RlXJlj-47so/image_thumb%25255B32%25255D.png?imgmax=800" width="244" height="102"></a> <li>これにより、変数が追加できるようになるので、今回は、次のように[名前]を「var1」、[データ型]を「String」(文字列)へ指定した変数を追加します。<br><a href="http://lh3.ggpht.com/-g9tI_06fd-g/UUMq8PlfvLI/AAAAAAAAND4/UDLZKkZJ5Fc/s1600-h/image%25255B69%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/--wAAd-HWKjA/UUMrDI--PJI/AAAAAAAANEA/3g7CJEFaT5o/image_thumb%25255B33%25255D.png?imgmax=800" width="244" height="71"></a> <li>続いて、制御フローに配置している[スクリプト タスク]をダブル クリックします。<br><a href="http://lh5.ggpht.com/-epBCapa4od8/UUMrILRJtdI/AAAAAAAANEI/bOzJALxo7i0/s1600-h/image%25255B73%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Eh-oMCWQxRg/UUMrPIm90qI/AAAAAAAANEQ/ew4n78rVxG0/image_thumb%25255B35%25255D.png?imgmax=800" width="446" height="146"></a><br>[スクリプト タスク エディター]ダイアログが表示されたら、[ReadWriteVariables]の[...]ボタンをクリックします(スクリプト内で変数へ値を代入するには、ReadOnly ではなく、ReadWrite~ へ変数を指定する必要があります)。 <li>これにより、[変数の選択]ダイアログで、変数の一覧が表示されるので、「User::var1」をチェックして、[OK]ボタンをクリックします(ユーザー定義の変数には、「User::」が付きます)。<br><a href="http://lh5.ggpht.com/-VCJHYXB6N5E/UUMrUAcBJfI/AAAAAAAANEY/wR8cltbAvC0/s1600-h/image%25255B76%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-vj-Bry7JHQE/UUMrbaHavNI/AAAAAAAANEg/rzbv93SNowI/image_thumb%25255B36%25255D.png?imgmax=800" width="244" height="197"></a> <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-llCvz6QuM3w/UUMrges3rfI/AAAAAAAANEo/wfhGZyy0zIw/s1600-h/image%25255B79%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ISzoxZrmHOw/UUMrnWV4syI/AAAAAAAANEw/Er3RJFNJfaY/image_thumb%25255B37%25255D.png?imgmax=800" width="197" height="82"></a> <li>[スクリプト エディター]が表示されたら、「public Sub Main( )」内のコードを、次のように変更します。変数名「User::var1」は、大文字と小文字を区別するので、注意してください。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">Dts.Variables("User::var1").Value = System.DateTime.Now.ToString()</td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-UiMEDF5hhCY/UUMrsZbFmKI/AAAAAAAANE4/jXwUbwnL6Tc/s1600-h/image%25255B84%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-L5vjBSRgHfI/UUMrzdSfKiI/AAAAAAAANFA/YUwW6oOygQs/image_thumb%25255B40%25255D.png?imgmax=800" width="572" height="132"></a><br>このコードにより、スクリプト実行時の時刻(Now プロパティ)を変数「var1」へ格納できるようになります。<br>入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、SSIS ツールボックスを開いて、[スクリプト タスク]をもうひとつ、SSIS デザイナー上へドラッグ&ドロップして配置します。<br><a href="http://lh4.ggpht.com/-bAQwv5Iu4JQ/UUMr4aRPzbI/AAAAAAAANFI/VtI6871K7EE/s1600-h/image%25255B88%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-sbckFSqXfPw/UUMr_hElrqI/AAAAAAAANFQ/bWakmYwVnog/image_thumb%25255B42%25255D.png?imgmax=800" width="343" height="231"></a><br>最初の[スクリプト タスク]の緑色の矢印を、新たに追加した[スクリプト タスク1]まで、ドラッグ&ドロップして伸ばします。 <li>追加した[スクリプト タスク1]をダブル クリックして、[スクリプト タスク エディター]ダイアログを表示します。<br><a href="http://lh5.ggpht.com/-OvFiiBOg4ys/UUMsEdswo5I/AAAAAAAANFY/vTInhhJEKEA/s1600-h/image%25255B91%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-YOSEjLDzKxY/UUMsLn1ohAI/AAAAAAAANFg/q3V-iGLB2qo/image_thumb%25255B43%25255D.png?imgmax=800" width="244" height="167"></a> <li>[スクリプト タスク エディター]ダイアログが表示されたら、[ScritLanguage]で「Microsoft Visual Basic 2010」を選択します。<br><a href="http://lh5.ggpht.com/-5X0t9X7bH1g/UUMsQddc4dI/AAAAAAAANFo/AWovpFW2y0g/s1600-h/image%25255B95%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-l8DFZeZa9mw/UUMsXjpDGeI/AAAAAAAANFw/T4RCmfszrnc/image_thumb%25255B45%25255D.png?imgmax=800" width="476" height="118"></a> <li>次に、[ReadOnlyVariables]の[...]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-S6qibLN6OBc/UUMscWcEPKI/AAAAAAAANF4/Tcb-Pi1G1gk/s1600-h/image%25255B99%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-M_X9Cw9Dc1c/UUMsjodQFbI/AAAAAAAANGA/DoJzcvEktPI/image_thumb%25255B47%25255D.png?imgmax=800" width="476" height="153"></a> <li>[変数の選択]ダイアログが表示されたら、変数の一覧から、「User::var1」をチェックして、[OK]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-4l44zJAwgUk/UUMsosf5r8I/AAAAAAAANGI/dzm4JrmI3SA/s1600-h/image%25255B103%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-jgBc8O9QRoI/UUMsvzFIr_I/AAAAAAAANGQ/MNkNY8DWuKU/image_thumb%25255B49%25255D.png?imgmax=800" width="305" height="249"></a> <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-p1ZFBkI09Vw/UUMs0nN1LRI/AAAAAAAANGY/EpyFu7-kQ-I/s1600-h/image%25255B106%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-sgGszJpKM24/UUMs72DU2fI/AAAAAAAANGg/bLWfhAcsa-c/image_thumb%25255B50%25255D.png?imgmax=800" width="182" height="82"></a> <li>[スクリプト エディター]では、「public Sub Main( )」内のコードを、次のように変更します(変数名の大文字と小文字に注意してください)。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">MessageBox.Show(Dts.Variables("User::var1").Value.ToString())</td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-AQ_oyHqRZkU/UUMtApP9_eI/AAAAAAAANGo/CpX9CMUnj_0/s1600-h/image%25255B110%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-6aoGSYhLHJw/UUMtH2DCkNI/AAAAAAAANGw/2jp3HdA0n80/image_thumb%25255B52%25255D.png?imgmax=800" width="508" height="130"></a><br><br>このコードにより、変数「var1」の値をメッセージ ボックスで表示できるようになります。<br>入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。<br>1つ目の[スクリプト タスク]が緑色、2つ目の[スクリプト タスク1]が実行中に変わって、スクリプト タスクを実行した時の日時がメッセージ ボックスで表示されることを確認できます。<br><a href="http://lh5.ggpht.com/-52nqzg4JzkE/UUMtM3HqICI/AAAAAAAANG4/YR7-YyfSvOM/s1600-h/image%25255B114%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-8c7DaJZuS28/UUMtUDTDMlI/AAAAAAAANHA/5-Irck5Vsp8/image_thumb%25255B54%25255D.png?imgmax=800" width="579" height="193"></a><br>[OK]ボタンをクリックすると、[スクリプト タスク1]に緑のチェックマークが付いて、スクリプトの実行が成功したことを確認できます。 <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。<br></li></ol> <h5 id="id004">SQL 実行タスクの結果を変数へ格納</h5> <h6>SQL 実行タスクの結果を変数へ格納</h6> <p>次に、「SQL 実行タスク」を利用して SQL を実行し、その結果を変数へ格納したり、その変数をデータ フロー タスクのパラメーターへ受け渡してデータ転送を実行してみましょう。</p> <ol> <li>まずは、SSIS ツールボックスの[お気に入り]カテゴリから[SQL 実行タスク]を SSIS デザイナー上へドラッグ&ドロップして配置します。<br><a href="http://lh6.ggpht.com/-WHQTHuMgiMc/UUMtZMeSj0I/AAAAAAAANHI/WptqmHJ-fJk/s1600-h/image%25255B118%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-G0k9EZSSwww/UUMtgs3YhBI/AAAAAAAANHQ/7pnh1FmqQlo/image_thumb%25255B56%25255D.png?imgmax=800" width="576" height="271"></a> <li>次に、[SSIS]メニューから[変数]をクリックします。<br><a href="http://lh3.ggpht.com/-tBY5kZHfj5Y/UUMtln1xw7I/AAAAAAAANHY/80P-J0xw-4Q/s1600-h/image%25255B122%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-U097l9S8zpU/UUMtspk_tGI/AAAAAAAANHg/E_qevjPNHfA/image_thumb%25255B58%25255D.png?imgmax=800" width="346" height="104"></a> <li>[変数]ウィンドウが表示されたら、前のステップで作成した変数「var1」を選択して、[変数の削除]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-4Aqfs9ZYsJk/UUMtxnYyUVI/AAAAAAAANHo/fefxkVOE28c/s1600-h/image%25255B126%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-oNQzbOpC_dY/UUMt4miUNyI/AAAAAAAANHw/Anz5oazMTPU/image_thumb%25255B60%25255D.png?imgmax=800" width="351" height="94"></a> <li>続いて、[変数の追加]ボタンをクリックし、新しく変数を追加します。<br><a href="http://lh3.ggpht.com/-xHpIgOk4sZw/UUMt9t6FxPI/AAAAAAAANH4/V1dxYM1DdLA/s1600-h/image%25255B130%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-SpbMUeIpKRs/UUMuEte0FPI/AAAAAAAANIA/wTWJTklkUTs/image_thumb%25255B62%25255D.png?imgmax=800" width="351" height="104"></a><br>[名前]には「v1」と入力して、[データ型]は既定値の「Int32」を選択します。Int32 は、SQL Server での int 型に相当するデータ型です。 <li>次に、もう一度[変数の追加]ボタンをクリックして、もうひとつ変数を追加します。<br><a href="http://lh5.ggpht.com/-yi0ZTlDyIUE/UUMuJiuXwKI/AAAAAAAANII/GCA1aSiRwM4/s1600-h/image%25255B134%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-ejqe0IJCxas/UUMuQhJpEXI/AAAAAAAANIQ/1GwWqDTrDhw/image_thumb%25255B64%25255D.png?imgmax=800" width="354" height="115"></a><br>[名前]は「v2」とし、[データ型]は既定値の「Int32」を選択します。 <li>次に、SSIS デザイナーへ配置した[SQL 実行タスク]をダブル クリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlh2Zd4mTL8Bog6RdJbMXDbJAZFD322jaVhQT6l6mA0jfzDlXfyzfkyrkkxHjhQnvbHjkcn8E1NjHoa8P9vNAnSpaMimfgruwIqC3bAZTgvOoee2xG3h-d4Xj9O_VaQKF8Zirhbi2BO9et/s1600-h/image%25255B138%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-2nT8_eorGWc/UUMucsyZyjI/AAAAAAAANIg/bfhTveTgpns/image_thumb%25255B66%25255D.png?imgmax=800" width="351" height="94"></a> <li>[SQL 実行タスク エディター]ダイアログが表示されたら、[Connection]で「新しい接続」をクリックして、[OLE DB 接続マネージャーの構成]ダイアログを表示し、[新規作成]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-6-yupKlnwq0/UUMuhpJdrKI/AAAAAAAANIo/5-YlCDDlHLA/s1600-h/image%25255B142%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-xU6XiFWjQLA/UUMuo9vyJ4I/AAAAAAAANIw/7W1SayvnHro/image_thumb%25255B68%25255D.png?imgmax=800" width="354" height="291"></a> <li>[接続マネージャー]ダイアログが表示されたら、[サーバー名]へ SQL Server の名前(画面は moon)を入力し、[データベース名の選択または入力]で「sampleDB」データベースを選択して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-Mn5CoRReQwg/UUMutgVvPqI/AAAAAAAANI4/ZAp4n9SWJgM/s1600-h/image%25255B146%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-0hVU0mtystk/UUMu09kIOXI/AAAAAAAANJA/2nXJ6g6BxZM/image_thumb%25255B70%25255D.png?imgmax=800" width="356" height="341"></a><br>[OLE DB 接続マネージャーの構成]ダイアログへ戻ったら、[OK]ボタンをクリックします。 <li>次に、[SQLStatement]の[...]ボタンをクリックして、[SQL クエリの入力]ダイアログを表示し、次のように SQL を入力します。この SQL は、sampleDB データベース内にある「社員」テーブルから、「給与」の最大値と最小値を取得するものです。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT MAX(給与) AS maxsal, MIN(給与) AS minsal FROM 社員</td></tr></tbody></table><br><a href="http://lh6.ggpht.com/-MFuXDarUTf4/UUMu6Mp6rGI/AAAAAAAANJI/U7GAUNED64M/s1600-h/image%25255B151%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-Ot7tvM5ET9Y/UUMvBaDqR_I/AAAAAAAANJQ/7Of7E5rbqZI/image_thumb%25255B73%25255D.png?imgmax=800" width="516" height="414"></a> <li>続いて、[ResultSet]で、クエリ結果の形式を指定します。このクエリの結果は、1行のみを返すので、「単一行」を選択します。<br><a href="http://lh4.ggpht.com/-hsypE-JG_wk/UUMvGClgFsI/AAAAAAAANJY/Acjdp7sxyv0/s1600-h/image%25255B155%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-J7QzAX6095M/UUMvNXuzDII/AAAAAAAANJg/6FzKin71RbU/image_thumb%25255B75%25255D.png?imgmax=800" width="414" height="230"></a> <li>次に、[結果セット]ページをクリックして開きます。[追加]ボタンをクリックして、[結果名]へ給与の最大値の列名である「maxsal」を入力し、[変数名]で「User::v1」が選択されていることを確認します。<br><a href="http://lh6.ggpht.com/-hEgjZkMwY1g/UUMvSICCUFI/AAAAAAAANJo/UKFyenTeL_8/s1600-h/image%25255B159%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-I_VR3-KTU5A/UUMvZZAuhwI/AAAAAAAANJw/iUE2IM75qwU/image_thumb%25255B77%25255D.png?imgmax=800" width="404" height="356"></a><br>これにより、SELECT ステートメントで取得した maxsal(MAX(給与))の値を変数 v1 へ格納できるようになります。 <li>続いて、もう一度[追加]ボタンをクリックして、[結果名]へ給与の最小値の列名である「minsal」を入力し、[変数名]で「User::v2」を選択して、[OK]ボタンをクリックします。<br><a href="http://lh3.ggpht.com/-sPoaDlvRnzs/UUMveSt-bhI/AAAAAAAANJ4/kUoJgHu5nHE/s1600-h/image%25255B163%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-qBbZCZTFfCI/UUMvlhv9b-I/AAAAAAAANKA/1H3SLmtSxog/image_thumb%25255B79%25255D.png?imgmax=800" width="404" height="407"></a> <li>次に、1つ目の[スクリプト タスク]を右クリックして、[無効化]をクリックし、[スクリプト タスク]を無効化しておきます。<br><a href="http://lh5.ggpht.com/-GB67FZSG9Bo/UUMvqWBw3NI/AAAAAAAANKI/nA4FZdWKfVE/s1600-h/image%25255B167%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-e3GuL890wEk/UUMvxqviS8I/AAAAAAAANKQ/UruCKnv1HGs/image_thumb%25255B81%25255D.png?imgmax=800" width="408" height="203"></a> <li>続いて、[SQL 実行タスク]の緑色の矢印を[スクリプト タスク1]までドラッグ&ドロップして伸ばします。<br><a href="http://lh6.ggpht.com/-biauWm9djR4/UUMv2ZlBBjI/AAAAAAAANKY/ObXjiG5tDRo/s1600-h/image%25255B171%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-F71VSD43TVA/UUMv9rN-75I/AAAAAAAANKg/xCn8Nt0Mo3c/image_thumb%25255B83%25255D.png?imgmax=800" width="410" height="163"></a> <li>次に、[スクリプト タスク1]をダブルクリックします。<br><a href="http://lh6.ggpht.com/-4lytz52NGq4/UUMwCid4dwI/AAAAAAAANKo/55N-DxRYdlk/s1600-h/image%25255B175%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-aOy-8C0OQp4/UUMwJr0cQ6I/AAAAAAAANKw/CEjvwpc3TlU/image_thumb%25255B85%25255D.png?imgmax=800" width="295" height="102"></a> <li>[スクリプト タスク エディター]ダイアログが表示されたら、[ReadOnlyVariables]で[...]ボタンをクリックして、[変数の選択]ダイアログを表示し、追加した「User::v1」と「User::v2」の変数をチェックして、[OK]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-oV9thcqonkQ/UUMwOsAy2JI/AAAAAAAANK4/5FuHQJUjrm8/s1600-h/image%25255B179%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-6iD1-YAxe30/UUMwVwDTzbI/AAAAAAAANLA/O_sKjx9-Q7g/image_thumb%25255B87%25255D.png?imgmax=800" width="411" height="308"></a> <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[スクリプトの編集]ボタンをクリックします。<br><a href="http://lh4.ggpht.com/-noUm4gNlRoI/UUMwanHaR1I/AAAAAAAANLI/uBGk5nKYEHI/s1600-h/image%25255B183%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-4sS1bE42cM0/UUMwh1Vhn_I/AAAAAAAANLQ/Z5hxHjRSui4/image_thumb%25255B89%25255D.png?imgmax=800" width="409" height="264"></a> <li>[スクリプト エディター]では、「public Sub Main( )」内のコードを、次のように変更します(変数名の、大文字と小文字に注意してください)。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630"> <p>MessageBox.Show(Dts.Variables("User::v1").Value.ToString() _<br>& " : " & Dts.Variables("User::v2").Value.ToString())</p></td></tr></tbody></table><br><a href="http://lh3.ggpht.com/-D7pYgBE4Vyc/UUMwm4acZOI/AAAAAAAANLY/BdzvWSuCtdM/s1600-h/image%25255B188%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-m-NSevX-dx0/UUMwuAL_CQI/AAAAAAAANLg/4K6Oq23EfU4/image_thumb%25255B92%25255D.png?imgmax=800" width="614" height="246"></a><br>このコードにより、変数「v1」と「v2」の値をメッセージ ボックスで表示できるようになります。入力後、保存してスクリプト エディターを閉じます。 <li>[スクリプト タスク エディター]ダイアログへ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、スクリプト タスクを実行するために、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。<br>次のように、[SQL 実行タスク]に緑のチェックマーク、[スクリプト タスク1]が実行中へ変わって、社員テーブルの給与の最大値「700000」と最小値「300000」がメッセージ ボックスで表示されることを確認できます。<br><a href="http://lh4.ggpht.com/-Ldpi1FOtk_4/UUMwzLkQURI/AAAAAAAANLo/hf3wgNZmJ_k/s1600-h/image%25255B193%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-KhMTWXMvRvQ/UUMw6dbwwrI/AAAAAAAANLw/cc0BGSPDueg/image_thumb%25255B95%25255D.png?imgmax=800" width="503" height="232"></a><br>[OK]ボタンをクリックすると、[スクリプト タスク1]に緑のチェックマークが付いて、スクリプトの実行が成功したことを確認できます。<br><a href="http://lh6.ggpht.com/-i9eKwnMxwL0/UUMw_qLoRMI/AAAAAAAANL4/HevmqVEIsZk/s1600-h/image%25255B197%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-n-FVpIpQaas/UUMxHf01VcI/AAAAAAAANMA/TKj6YoDHuEw/image_thumb%25255B97%25255D.png?imgmax=800" width="504" height="214"></a><br>このように、SQL 実行タスクで実行した結果は、変数へ格納することができ、それはスクリプト タスクなどパッケージ内で再利用していくことができます。 <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。</li></ol> <h5 id="id005">SQL ステートメントのパラメーター化と変数の引き渡し</h5> <h6>SQL ステートメントのパラメーター化と変数の引き渡し</h6> <p>次に、SQL 実行タスクの結果を格納した変数を、データ フロー タスク内の SQL ステートメントの「パラメーター」へ引き渡して、実行できるようにしてみましょう。</p> <ol> <li>まずは、[スクリプト タスク1]を右クリックして、[無効化]をクリックし、[スクリプト タスク1]を無効にしておきます。<br><a href="http://lh4.ggpht.com/-_4anMDvdU4s/UUMxMEQQF-I/AAAAAAAANMI/JcsKkYblq7M/s1600-h/image%25255B201%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-UV4Q3XWgvzw/UUMxTWbNZAI/AAAAAAAANMQ/i-rOj4fUY4g/image_thumb%25255B99%25255D.png?imgmax=800" width="379" height="187"></a> <li>次に、SSIS ツールボックスを開いて、[お気に入り]カテゴリから[データ フロー タスク]をドラッグ&ドロップして SSIS デザイナー上へ配置します。<br><a href="http://lh5.ggpht.com/-Y7wHrbYQJds/UUMxYVX8MTI/AAAAAAAANMY/XJ8IonNeaZc/s1600-h/image%25255B206%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-1TjBy8r4UtY/UUMxf9NVLtI/AAAAAAAANMg/y1LRBoMz9U8/image_thumb%25255B102%25255D.png?imgmax=800" width="503" height="260"></a> <li>次に、[SQL 実行タスク]の緑色の矢印を、[データ フロー タスク]まで、ドラッグ&ドロップして伸ばします。<br><a href="http://lh3.ggpht.com/-ZTX_jhewGQo/UUMxk5UMklI/AAAAAAAANMo/P8q7mIU5NFo/s1600-h/image%25255B210%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-YqObxWijT2Y/UUMxsFRUKWI/AAAAAAAANMw/98bEbV-BZss/image_thumb%25255B104%25255D.png?imgmax=800" width="449" height="206"></a> <li>続いて、[データ フロー タスク]をダブル クリックして、[データ フロー]タブを表示します。<br><a href="http://lh4.ggpht.com/-DMHhntYQ04A/UUMxw9RNLiI/AAAAAAAANM4/tZVAGIFow1o/s1600-h/image%25255B213%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-bfdoGkybBMA/UUMx4JADEhI/AAAAAAAANNA/f_RaEaLT_uo/image_thumb%25255B105%25255D.png?imgmax=800" width="244" height="83"></a> <li>[データ フロー]タブが表示されたら、SSIS ツールボックスの[その他の変換元]カテゴリから[OLE DB ソース]を SSIS デザイナー上へドラッグ&ドロップして配置します。<br><a href="http://lh6.ggpht.com/-c93jw_ECShU/UUMx9P2CXzI/AAAAAAAANNI/_gXrcvD0nps/s1600-h/image%25255B226%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-4bk2U7Rx1Fw/UUMyE63tdVI/AAAAAAAANNQ/mjD84tlyDak/image_thumb%25255B110%25255D.png?imgmax=800" width="392" height="145"></a><br>配置した[OLE DB ソース]をダブル クリックして、[OLE DB ソース エディター]を開き、[OLE DB 接続マネージャー]で「サーバー名.sampleDB」が選択されていることを確認します。<br><a href="http://lh6.ggpht.com/-zE0yF9i5pbk/UUMyJjdM6pI/AAAAAAAANNY/brJTzdRQmJY/s1600-h/image%25255B227%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-eBmMD4b8qSs/UUMyRIONJpI/AAAAAAAANNg/llt5W2FW7B0/image_thumb%25255B111%25255D.png?imgmax=800" width="392" height="190"></a><br>[データ アクセス モード]では、「SQL コマンド」を選択します。これにより、[SQL コマンド テキスト]が表示され、SQL が入力できるようになるので、次のように SQL を記述します。<br> <table cellspacing="0" cellpadding="6" width="630" bgcolor="#000066" border="1"> <tbody> <tr> <td valign="top" width="630">SELECT * FROM 社員 WHERE 給与 = ?</td></tr></tbody></table><br> <p>「給与 = ?」と記述することで、パラメーターが 1 つ(パラメーター0)自動作成されます。<br>SQL を記述後、[パラメーター]ボタンをクリックします。</p> <li>[クエリ パラメーターの設定]ダイアログが表示されたら、自動作成されたパラメーター「パラメーター0」の[変数]で「User::v2」変数を選択して割り当てます。<br><a href="http://lh6.ggpht.com/-pgORQ2Bcbc0/UUMyV6FPpqI/AAAAAAAANNo/mCItPf5KG4M/s1600-h/image%25255B230%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-GPXeOhhD9nU/UUMydNgP8wI/AAAAAAAANNw/XPGbLfK7Hvw/image_thumb%25255B112%25255D.png?imgmax=800" width="222" height="244"></a><br>これで、「給与 = ?」の ? へ代入する値を変数 v2(最小給与が格納されている変数)にすることができます。<br>設定後、[OK]ボタンをクリックして、ダイアログを閉じます。[OLE DB ソース エディター]へ戻ったら、[OK]ボタンをクリックして閉じます。 <li>次に、SSIS ツール ボックスの[その他の変換先]カテゴリから[SQL Server 変換先]を SSIS デザイナー上へドラッグ&ドロップして配置します。<br><a href="http://lh4.ggpht.com/-rOCCdTl_gNs/UUMyiJKgHNI/AAAAAAAANN4/QLuasoHzTDU/s1600-h/image%25255B250%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-xwjCmy-C7xE/UUMypUD0thI/AAAAAAAANOA/s9UbyJ8Ceb8/image_thumb%25255B119%25255D.png?imgmax=800" width="417" height="216"></a><br>続いて、[OLE DB ソース]の青色の矢印を[SQL Server 変換先]まで、ドラッグ&ドロップして伸ばします。 <li>次に、[SQL Server 変換先]をダブル クリックして、[SQL 変換先エディター]を開き、[接続マネージャー]で「サーバー名.samplDB」が選択されていることを確認します。<br><a href="http://lh5.ggpht.com/-7uz67L6enkk/UUMyudwYouI/AAAAAAAANOI/nsbOBl-_nl0/s1600-h/image%25255B254%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-VmV3GQh3xe0/UUMy1uBvQZI/AAAAAAAANOQ/ovwsQzGG0U8/image_thumb%25255B121%25255D.png?imgmax=800" width="418" height="190"></a><br>[テーブルまたはビューを使用]で[新規作成]ボタンをクリックします。 <li>これにより、[テーブルの作成]ダイアログが表示されるので、テーブル名を「最小給与社員」へ変更して、[OK]ボタンをクリックします。<br><a href="http://lh5.ggpht.com/-92leKdyubBw/UUMy6v5LBsI/AAAAAAAANOY/3cWE3wsoAyU/s1600-h/image%25255B257%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-yk3NGAwCrRo/UUMzBxYUm4I/AAAAAAAANOg/NMj25wiUNaA/image_thumb%25255B122%25255D.png?imgmax=800" width="221" height="124"></a> <li>[SQL 変換先エディター]へ戻ったら、[マッピング]ページをクリックして開き、[OK]ボタンをクリックします。<br><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtpc4puwFLiWBFg5MxiU_uwGGkmekDHUfRZE1tWZk5QZs6jkmIJYKZlrK6mP-hVfb6BsLl6EW1m6AmhGljqtj98QkN9DWDQX1ebt-Y-lHiBrdMUYldtksPRkC2RwwTzyLT8TAAHialirQM/s1600-h/image%25255B261%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZZghRh-qyfqHyPVNKHp20-eZpKkKx0jAX1V4AVcWlse0RbmxtmdRjK4gcf1x4P1CBMfajrUkI3LOTydScNcabm5CBH5VgtE74H0clr_VE05o1ckjlvLTLepXev7jgSkpCpB3Ko4nis6b/?imgmax=800" width="334" height="324"></a> <li>次に、[制御フロー]タブをクリックして開いてから、ツールバーの[デバッグ開始]ボタンをクリックして、パッケージを実行します。<br><a href="http://lh6.ggpht.com/-aGyCOl60kR0/UUMzS9dTJkI/AAAAAAAANO4/njCNMW5V8UQ/s1600-h/image%25255B265%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-ZpFx_Pgbw4o/UUMzaI-VGnI/AAAAAAAANPA/P748ot4Kh8I/image_thumb%25255B126%25255D.png?imgmax=800" width="333" height="241"></a><br>[SQL 実行タスク]と[データ フロー タスク]に緑のチェックマークが付いて、パッケージの実行が成功したことを確認できます。<br><a href="http://lh4.ggpht.com/-SoGOvv1rrNQ/UUMzfMsshDI/AAAAAAAANPI/zvbVc3nE3qA/s1600-h/image%25255B269%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-Soo-wzlWj48/UUMzmnZidMI/AAAAAAAANPQ/cFVV3F9RWZs/image_thumb%25255B128%25255D.png?imgmax=800" width="336" height="200"></a> <li>確認後、ツールバーの[デバッグの停止]ボタンをクリックして、デバッグを終了します。 <li>転送されたデータを確認するには、Management Studio から、[最小給与社員]テーブルを右クリックして、[上位 1000 行の選択]をクリックし、データを確認します。<br><a href="http://lh6.ggpht.com/-2BE4kTJHtvU/UUMzruaUvJI/AAAAAAAANPY/B3s0hiTpsDQ/s1600-h/image%25255B274%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-_bHXTUHm_xY/UUMzzC6LykI/AAAAAAAANPg/E3PyNUZc25Y/image_thumb%25255B131%25255D.png?imgmax=800" width="497" height="284"></a><br>給与が最小の社員が格納されていることを確認できます。<br>このように、データ フロー タスク内の SQL ステートメントは、パラメーター化することができ、また、それに対して変数を代入して実行することができます。<br><a href="http://lh4.ggpht.com/-KM8tOcLisWI/UUMz4U_vlqI/AAAAAAAANPo/xPsIEToSSLg/s1600-h/image%25255B279%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-DMHGImbo-xM/UUM0AHOHTlI/AAAAAAAANPw/H10p1EHCdqw/image_thumb%25255B134%25255D.png?imgmax=800" width="611" height="555"></a></li></ol> minminnanahttp://www.blogger.com/profile/00281220352630588849noreply@blogger.com0