{"id":334,"date":"2014-11-05T11:34:58","date_gmt":"2014-11-05T02:34:58","guid":{"rendered":"http:\/\/www.dbsheetclient.jp\/blog\/?p=334"},"modified":"2018-06-27T16:27:44","modified_gmt":"2018-06-27T07:27:44","slug":"%e5%8b%95%e7%9a%84sql4","status":"publish","type":"post","link":"https:\/\/www.dbsheetclient.jp\/blog\/?p=334","title":{"rendered":"\u52d5\u7684SQL(4)"},"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\/11\/IMG_0277.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-335\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/IMG_0277-225x300.jpg\" alt=\"IMG_0277\" width=\"202\" height=\"269\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/IMG_0277-225x300.jpg 225w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/IMG_0277-768x1024.jpg 768w\" sizes=\"auto, (max-width: 202px) 100vw, 202px\" \/><\/p>\n<p><\/a>\u4f1a\u793e\u306e\u4eba\u304c\u5e83\u5cf6\u306b\u884c\u3063\u305f\u3068\u304d\u306b\u3001\u5915\u65b9\u8fd1\u304f\u306b\u5e73\u548c\u516c\u5712\u306b\u6563\u6b69\u304c\u3066\u3089\u7acb\u3061\u5bc4\u3063\u3066\u64ae\u3063\u305f\u3001\u539f\u7206\u30c9\u30fc\u30e0\u306e\u5199\u771f\u3067\u3059\u3002\u6614\u3001\u5c0f\u5b66\u6821\u306e\u4fee\u5b66\u65c5\u884c\u3067\u5e83\u5cf6\u306e\u5e73\u548c\u516c\u5712\u306b\u884c\u3063\u305f\u3053\u3068\u304c\u3042\u308a\u3001\u5343\u7fbd\u9db4\u304c\u305f\u304f\u3055\u3093\u3064\u308b\u3057\u3066\u3042\u3063\u305f\u306e\u3092\u898b\u305f\u8a18\u61b6\u304c\u3042\u308a\u307e\u3059\u306d\u3002\u5b50\u4f9b\u5fc3\u306b\u3001\u6226\u4e89\u3084\u539f\u5b50\u7206\u5f3e\u306e\u88ab\u5bb3\u304c\u4e8c\u5ea6\u3068\u3042\u3063\u3066\u306f\u306a\u3089\u306a\u3044\u3068\u5f37\u304f\u611f\u3058\u307e\u3057\u305f\u3002<br \/> <strong><br \/> \uff1c\u672c\u65e5\u306e\u984c\u6750\uff1e<br \/> <\/strong><strong>\u52d5\u7684SQL(SQL Server)<\/strong><\/p>\n<p>\u4eca\u56de\u306f\u3001SQL Server\u306e\u5834\u5408\u306e\u52d5\u7684SQL\u306e\u7d9a\u304d\u3067\u3001sp_executesql \u30b7\u30b9\u30c6\u30e0\u30b9\u30c8\u30a2\u30c9\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3092\u5229\u7528\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n<p><strong>\u69cb\u6587<\/strong><br \/> sp_executesql [ @stmt = ] statement<br \/> [<br \/> { , [ @params = ] N&#8217;@parameter_name data_type [ OUT | OUTPUT ][ ,&#8230;n ]&#8217; }<br \/> { , [ @param1 = ] &#8216;value1&#8217; [ ,&#8230;n ] }<br \/> ]<\/p>\n<p>@stmt\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u542b\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u5b9a\u7fa9\u30ea\u30b9\u30c8(@params=..)\u3068\u30d1\u30e9\u30e1\u30fc\u30bf\u5024\u30ea\u30b9\u30c8(@param1=..)\u306e\u4e21\u65b9\u306b\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4f8b\uff09\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u3001\u6307\u5b9a\u3057\u305f\u53d7\u6ce8\u65e5\u3068\u6c7a\u6e08\u91d1\u984d\u306e\u6761\u4ef6\u306b\u5408\u3063\u305f\u53d7\u6ce8\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u4f8b\u3067\u3001\u6761\u4ef6\u53e5(WHERE\u53e5)\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u5909\u6570\u3092\u7528\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>DECLARE @sql_stmt nvarchar(500)<br \/> DECLARE @tab_Name nvarchar(30) = &#8216;dbo.\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb&#8217;<br \/> DECLARE @param nvarchar(200)<br \/> SET @sql_stmt = N&#8217;SELECT * FROM &#8216; + @tab_Name<br \/> SET @sql_stmt = @sql_stmt + N&#8217; WHERE \u53d7\u6ce8\u65e5= @p1_date&#8217;<br \/> SET @sql_stmt = @sql_stmt + N&#8217; AND \u6c7a\u6e08\u91d1\u984d&gt;= @p2_kingaku&#8217;<br \/> SET @param = N&#8217;@p1_date varchar(8), @p2_kingaku int&#8217;<\/p>\n<p>EXEC sp_executesql@sql_stmt<br \/> , @param<br \/> , @p1_date = &#8216;20141029&#8217;<br \/> , @p2_kingaku = 300000<\/p>\n<p>\u7d50\u679c\u306f\u3001\u4ee5\u4e0b<br \/> <a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679c.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-337\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679c-300x107.jpg\" alt=\"executesql_2\u7d50\u679c\" width=\"353\" height=\"126\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679c-300x107.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679c.jpg 320w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/a><\/p>\n<p>\u6761\u4ef6\u3068\u3057\u3066\u3001\u53d7\u6ce8\u65e5(@p1_date)\u306b\u300c20141029\u300d\u3001\u6c7a\u6e08\u91d1\u984d(@p2_kingaku)\u306b\u300c300000\u300d\u4ee5\u4e0a\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4e0e\u3048\u305f\u5834\u5408\u306e\u3001\u53d7\u6ce8\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u304c\u62bd\u51fa\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u203b\uff09\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u6700\u7d42\u7684\u306b\u5909\u6570@sql_stmt\u306b\u8a2d\u5b9a\u3057\u305fSQL\u6587\u304c\u6b63\u3057\u3044SQL\u6587\u306b\u306a\u308b\u3088\u3046\u306b\u3001FROM\u53e5\u3084WHERE\u53e5\u3001AND\u53e5\u306a\u3069\u306e\u524d\u5f8c\u306b\u7a7a\u767d\u3092\u304d\u3061\u3093\u3068\u5165\u308c\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\uff08\u524d\u56de\u6295\u7a3f\u306e\u4f8b\u3067\u3082\u3053\u306e\u70b9\u306f\u540c\u3058\u3067\u3059\u304c\uff09<br \/> \u3082\u3057\u3001\u4e0a\u8a18\u306e\u4f8b\u3067WHERE\u306e\u524d\u306e\u7a7a\u767d\u3092\u5165\u308c\u3066\u3044\u306a\u3044\u3068\u3001\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\u4e0a\u8a18\u3092\u30b9\u30c8\u30a2\u30c9\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u306b\u3057\u305f\u5834\u5408\u306f\u3001\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u306f\u3067\u304d\u308b\u3051\u308c\u3069\u3082\u3001\u5b9f\u884c\u6642\u306b\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3068\u3044\u3046\u3053\u3068\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679cerror2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-340\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679cerror2-300x189.jpg\" alt=\"executesql_2\u7d50\u679cerror2\" width=\"414\" height=\"261\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679cerror2-300x189.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2014\/11\/executesql_2\u7d50\u679cerror2.jpg 593w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><br \/> <\/a><\/p>\n<p>\u307e\u305f\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u7e70\u308a\u8fd4\u3057\u5909\u66f4\u3057\u306a\u304c\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u3001sp_executesql \u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u304b\u306a\u308a\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u6539\u5584\u3067\u304d\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u308c\u306b\u3064\u3044\u3066\u306f\u6b21\u56de\u4ee5\u964d\u306b\u307e\u305f\u6a5f\u4f1a\u3092\u6539\u3081\u3066\u3001\u984c\u6750\u3068\u3057\u3066\u53d6\u308a\u4e0a\u3052\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u65e5\u306f\u4ee5\u4e0a\u307e\u3067<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IT\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30bf\u306e\u30b7\u30e5\u30a6\u3067\u3059\u3002 \u4f1a\u793e\u306e\u4eba\u304c\u5e83\u5cf6\u306b\u884c\u3063\u305f\u3068\u304d\u306b\u3001\u5915\u65b9\u8fd1\u304f\u306b\u5e73\u548c\u516c\u5712\u306b\u6563\u6b69\u304c\u3066\u3089\u7acb\u3061\u5bc4\u3063\u3066\u64ae\u3063\u305f\u3001\u539f\u7206\u30c9\u30fc\u30e0\u306e\u5199\u771f\u3067\u3059\u3002\u6614\u3001\u5c0f\u5b66\u6821\u306e\u4fee\u5b66\u65c5\u884c\u3067\u5e83\u5cf6\u306e\u5e73\u548c\u516c\u5712\u306b\u884c\u3063\u305f\u3053\u3068\u304c\u3042\u308a\u3001\u5343\u7fbd\u9db4\u304c\u305f\u304f\u3055\u3093\u3064\u308b\u3057\u3066\u3042\u3063\u305f [&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-334","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\/334","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=334"}],"version-history":[{"count":7,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":1975,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions\/1975"}],"wp:attachment":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}