{"id":318,"date":"2014-10-30T22:03:20","date_gmt":"2014-10-30T13:03:20","guid":{"rendered":"http:\/\/www.dbsheetclient.jp\/blog\/?p=318"},"modified":"2018-06-27T16:28:13","modified_gmt":"2018-06-27T07:28:13","slug":"%e5%8b%95%e7%9a%84sql%ef%bc%883%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.dbsheetclient.jp\/blog\/?p=318","title":{"rendered":"\u52d5\u7684SQL\uff083\uff09"},"content":{"rendered":"<p>IT\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30bf\u306e\u30b7\u30e5\u30a6\u3067\u3059\u3002<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/003.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-319\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/003-300x225.jpg\" alt=\"003\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/003-300x225.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/003-1024x768.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/> <\/a><\/p>\n<p>\u30b3\u30b9\u30e2\u30b9\u304c\u3044\u3063\u3071\u3044\u54b2\u3044\u3066\u307e\u3057\u305f\u3002\u3061\u3087\u3063\u3068\u524d\u306e\u53f0\u98a8\u3067\u3001\u7d50\u69cb\u5012\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u4f55\u3068\u304b\u5fa9\u6d3b\u3057\u3066\u9811\u5f35\u3063\u3066\u54b2\u3044\u3066\u3044\u307e\u3059\u3002<br \/> <strong><br \/> \uff1c\u672c\u65e5\u306e\u984c\u6750\uff1e<br \/> \u52d5\u7684SQL(SQL Server)<\/strong><\/p>\n<p>\u4eca\u56de\u306f\u3001SQL Server\u306e\u5834\u5408\u306e\u52d5\u7684SQL\u306b\u3064\u3044\u3066\u306e\u5185\u5bb9\u3067\u3059\u3002SQL Server\u306e\u5834\u5408\u306f\u3001EXECUTE\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3001\u3082\u3057\u304f\u306f\u3001sp_executesql \u30b7\u30b9\u30c6\u30e0\u30b9\u30c8\u30a2\u30c9\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3092\u5229\u7528\u3057\u307e\u3059\u3002<br \/> <strong><br \/> \u4f8b\uff09<\/strong>\u30c6\u30fc\u30d6\u30eb\u540d\u3084\u9805\u76ee\u540d\u3092\u5909\u6570\u5316\u3057\u3066\u3001\u6307\u5b9a\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u5fc5\u8981\u306a\u9805\u76ee\u306b\u3064\u3044\u3066\u306e\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u52d5\u7684SQL\u306e\u4f8b\u3067\u3059\u3002EXECUTE\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3057\u305f\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u6307\u5b9a\u3057\u305f\u6761\u4ef6\u3067\u62bd\u51fa\u3057\u307e\u3059\u3002<br \/> \u305d\u306e\u524d\u306b\u3001\u30c7\u30fc\u30bf\u3092\u78ba\u8a8d<br \/> SELECT * FROM dbo.\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb<br \/> ORDER BY \u53d7\u6ce8NO;<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5168\u4ef6ms.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-320\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5168\u4ef6ms-300x226.jpg\" alt=\"\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5168\u4ef6ms\" width=\"317\" height=\"239\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5168\u4ef6ms-300x226.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5168\u4ef6ms.jpg 345w\" sizes=\"auto, (max-width: 317px) 100vw, 317px\" \/><\/a><\/p>\n<p>\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u306e\u5b9a\u7fa9\u306f\u3001Oracle\u306e\u3068\u304d\u3068\u540c\u3058<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9ms.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-321\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9ms-300x115.jpg\" alt=\"\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9ms\" width=\"326\" height=\"125\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9ms-300x115.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9ms.jpg 326w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/a><\/p>\n<p>SQL\u306f\u4ee5\u4e0b\uff1a<\/p>\n<p>DECLARE @tab_name VARCHAR(30)<br \/> DECLARE @Col_name VARCHAR(100)<br \/> DECLARE @Clause VARCHAR(200)<br \/> SELECT @tab_name = &#8216;dbo.\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb&#8217;<br \/> <span style=\"line-height: 1.5;\">SELECT @Col_name = &#8216;\u9867\u5ba2\u756a\u53f7, \u53d7\u6ce8\u65e5, \u6c7a\u6e08\u91d1\u984d&#8217;<br \/> <\/span><span style=\"line-height: 1.5;\">SELECT @Clause = &#8216;\u53d7\u6ce8\u65e5 BETWEEN &#8221;20141028&#8221; AND &#8221;20141029&#8221;&#8217;<\/span><\/p>\n<p><span style=\"line-height: 1.5;\">EXECUTE (&#8216;SELECT &#8216;+@Col_name+&#8217; FROM &#8216;+ @tab_name+&#8217; WHERE &#8216;+ @Clause)<br \/> <\/span><\/p>\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u3001@tab_name \u306b\u30c6\u30fc\u30d6\u30eb\u540d\u3001@Col_name\u306b\u30ab\u30e9\u30e0\u540d\u3001@Clause\u306b\u306f\u6761\u4ef6\u3092\u6e21\u3057\u3001\u305d\u308c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<br \/> \uff08SQL Server\u3067\u3082\u3001&#8217;\u3067\u56f2\u3080\u6587\u5b57\u5217\u306e\u4e2d\u306b&#8217;\uff08\u30b7\u30f3\u30b0\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\uff09\u3092\u542b\u3080\u5834\u5408\u306b\u306f\u3001&#8217;\u30922\u3064\u9023\u7d9a\u3057\u3066\u8a18\u8ff0\u3057\u307e\u3059\uff09<br \/> \u307e\u305f\u3001EXECUTE\u306f\u300cEXEC\u300d\u3068\u7701\u7565\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u7d50\u679c\u306f\u3001\u4ee5\u4e0b<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c1ms.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-322\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c1ms.jpg\" alt=\"\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c1ms\" width=\"257\" height=\"200\" \/><\/a><\/p>\n<p>\u53d7\u6ce8\u65e5\u304c20141028\uff5e20141029\u306e\u671f\u9593\u5185\u3067\u306e\u9867\u5ba2\u756a\u53f7\u3001\u53d7\u6ce8\u65e5\u3001\u6c7a\u6e08\u91d1\u984d\u3092\u62bd\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<br \/> \u3053\u306e\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308c\u3070\u3001\u52d5\u7684\u306b\u5bfe\u8c61\u306e\u30c6\u30fc\u30d6\u30eb\u3001\u9805\u76ee\u3001\u6761\u4ef6\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u5909\u66f4\u3057\u305f\u5834\u5408\u306e\u4f8b\u3002<\/p>\n<p>DECLARE @tab_name VARCHAR(30)<br \/> DECLARE @Col_name VARCHAR(100)<br \/> DECLARE @Clause VARCHAR(200)<br \/> DECLARE @sql_stmt VARCHAR(400)<br \/> SELECT @tab_name = &#8216;dbo.\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb&#8217;<br \/> SELECT @Col_name = &#8216;\u9867\u5ba2\u756a\u53f7, \u6c7a\u6e08\u91d1\u984d&#8217;<br \/> SELECT @Clause = &#8216;\u6c7a\u6e08\u91d1\u984d &gt; 300000&#8217;<br \/> SELECT @sql_stmt = &#8216;SELECT &#8216;+@Col_name+&#8217; FROM &#8216;+ @tab_name+&#8217; WHERE &#8216;+ @Clause<\/p>\n<p>EXEC (@sql_stmt)<\/p>\n<p>\u7d50\u679c\u306f\u3001\u4ee5\u4e0b<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c2ms.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-323\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c2ms.jpg\" alt=\"\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c2ms\" width=\"202\" height=\"158\" \/><\/a><\/p>\n<p>\u3053\u3053\u3067\u306f\u3001\u6700\u7d42\u7684\u306b @sql_stmt \u306b\u7d44\u307f\u5408\u308f\u305b\u305fSELECT\u6587\u30bb\u30c3\u30c8\u3057\u3066\u3001\u305d\u308c\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002\u6c7a\u6e08\u91d1\u984d\u304c 300000\u5186\u4ee5\u4e0a\u306e\u9867\u5ba2\u306b\u3064\u3044\u3066\u3001\u9867\u5ba2\u756a\u53f7\u3068\u6c7a\u6e08\u91d1\u984d\u3092\u8868\u793a\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u4eca\u65e5\u306f\u4ee5\u4e0a\u307e\u3067<a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/10\/\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u7d50\u679c2ms.jpg\"><br \/> <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IT\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30bf\u306e\u30b7\u30e5\u30a6\u3067\u3059\u3002 \u30b3\u30b9\u30e2\u30b9\u304c\u3044\u3063\u3071\u3044\u54b2\u3044\u3066\u307e\u3057\u305f\u3002\u3061\u3087\u3063\u3068\u524d\u306e\u53f0\u98a8\u3067\u3001\u7d50\u69cb\u5012\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u4f55\u3068\u304b\u5fa9\u6d3b\u3057\u3066\u9811\u5f35\u3063\u3066\u54b2\u3044\u3066\u3044\u307e\u3059\u3002 \uff1c\u672c\u65e5\u306e\u984c\u6750\uff1e \u52d5\u7684SQL(SQL Server) \u4eca\u56de\u306f\u3001SQ [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[10,6],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-10","category-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":11,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}