{"id":2790,"date":"2019-12-18T19:50:29","date_gmt":"2019-12-18T11:50:29","guid":{"rendered":"http:\/\/jason695.why3s.tw\/wordpress\/?p=2790"},"modified":"2019-12-18T19:56:52","modified_gmt":"2019-12-18T11:56:52","slug":"%e5%8f%96%e5%be%97-execute-sql-%e8%a3%a1%e9%9d%a2%e8%b3%87%e6%96%99%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/jason695.why3s.tw\/wordpress\/2019\/12\/18\/%e5%8f%96%e5%be%97-execute-sql-%e8%a3%a1%e9%9d%a2%e8%b3%87%e6%96%99%e7%9a%84%e5%80%bc\/","title":{"rendered":"\u53d6\u5f97 Execute SQL \u88e1\u9762\u8cc7\u6599\u7684\u503c"},"content":{"rendered":"\n<p>\u539f\u6587: <a href=\"https:\/\/rainmakerho.github.io\/2017\/10\/03\/20171003-2\/\">https:\/\/rainmakerho.github.io\/2017\/10\/03\/20171003-2\/<\/a><\/p>\n\n\n\n<p>(1) Store Procedue \uff0c\u900f\u904e sp_executesql + \u5b83\u7684 output \u53c3\u6578\u4f86\u53d6\u5f97\u8cc7\u6599<br><code>DECLARE @sql NVARCHAR(max), @outputcode NVARCHAR(4000);<br>SET @sql =<br>'EXEC SP_GETSERNO @RETURNS=@outputcode output';<br>exec sp_executesql @sql, N'@outputcode NVARCHAR(4000) out', @outputcode out<br>SELECT @outputcode;<\/code><br><br>(2) \u53d6\u5f97 t-sql \u88e1\u9762\u7684\u5167\u5bb9\uff0c\u9700\u8981\u5148\u5efa\u7acb temp table \uff0c\u6216\u662f TABLE \u8b8a\u6578\uff0c\u7136\u5f8c\u5728 Exec \u7684 t-sql \u88e1\u9762\u4f7f\u7528<br><code>DECLARE @ATT_IDENT TABLE (ATT_IDENT int);<br>INSERT @ATT_IDENT<br>exec ('SELECT top 1 ATT_IDENT FROM [dbo].[myTable]');<br>select * FROM @ATT_IDENT;<\/code><br><br>(3) t-sql\uff0c\u900f\u904e sp_executesql + \u5b83\u7684 output \u53c3\u6578\u4f86\u53d6\u5f97\u8cc7\u6599\uff0c\u4f8b\u5982\uff0c<br><code>DECLARE @IDT NUMERIC<br>exec sp_executesql N'SELECT TOP 1 @IDT = ATT_IDENT FROM [dbo].[myTable]', N'@IDT NUMERIC out', @IDT OUT<br>SELECT @IDT;<\/code><\/p>\n\n\n\n<p>(4) \u900f\u904e OPENQUERY \u7684\u65b9\u5f0f\uff0c\u8a73\u7d30\u53ef\u4ee5\u53c3\u8003\u300c<a rel=\"noreferrer noopener\" href=\"https:\/\/dotblogs.com.tw\/rainmaker\/2015\/02\/02\/148355\" target=\"_blank\">\u8b93 Execute \u53ef\u4ee5\u642d\u914d Select Into\uff0c\u800c\u4e0d\u518d\u53ea\u6709 Insert into<\/a>\u300d <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587: https:\/\/rainmakerho.github.io\/2017\/10\/03\/20171003-2 &hellip; <a href=\"https:\/\/jason695.why3s.tw\/wordpress\/2019\/12\/18\/%e5%8f%96%e5%be%97-execute-sql-%e8%a3%a1%e9%9d%a2%e8%b3%87%e6%96%99%e7%9a%84%e5%80%bc\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587 <span class=\"screen-reader-text\">\u53d6\u5f97 Execute SQL \u88e1\u9762\u8cc7\u6599\u7684\u503c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[47],"tags":[],"class_list":["post-2790","post","type-post","status-publish","format-standard","hentry","category-sql-server","no-featured-image"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts\/2790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/comments?post=2790"}],"version-history":[{"count":0,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts\/2790\/revisions"}],"wp:attachment":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/media?parent=2790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/categories?post=2790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/tags?post=2790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}