{"id":2584,"date":"2017-02-06T15:45:17","date_gmt":"2017-02-06T07:45:17","guid":{"rendered":"http:\/\/jason695.why3s.tw\/wordpress\/?p=2584"},"modified":"2017-02-06T15:45:17","modified_gmt":"2017-02-06T07:45:17","slug":"sql-server%e4%b8%ad%e7%8d%b2%e5%be%97exec%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/jason695.why3s.tw\/wordpress\/2017\/02\/06\/sql-server%e4%b8%ad%e7%8d%b2%e5%be%97exec%e7%9a%84%e5%80%bc\/","title":{"rendered":"SQL Server\u4e2d\u7372\u5f97EXEC\u7684\u503c"},"content":{"rendered":"<div>\u524d\u8a00\uff1a<\/div>\n<div>\u5728\u8cc7\u6599\u5eab\u7a0b\u5f0f\u958b\u767c\u7684\u904e\u7a0b\u4e2d\uff0c\u6211\u5011\u7d93\u5e38\u6703\u78b0\u5230\u5229\u7528EXEC\u4f86\u57f7\u884c\u4e00\u6bb5\u9700\u8981\u8fd4\u56de\u67d0\u4e9b\u503c\u7684sql\u8a9e\u53e5\uff08\u901a\u5e38\u662f\u69cb\u9020\u52d5\u614bsql\u8a9e\u53e5\u6642\u4f7f\u7528\uff09\uff0c\u6216\u8005\u5728\u4e00\u500b\u9810\u5b58\u7a0b\u5e8f\u4e2d\u5229\u7528EXEC\u8abf\u7528\u53e6\u4e00\u500b\u6709\u50b3\u56de\u503c\u7684\u9810\u5b58\u7a0b\u5e8f\uff08\u5fc5\u9808\u7372\u5f97\u50b3\u56de\u503c\uff09\uff0c\u90a3\u9ebc\u5982\u4f55\u7372\u5f97\u9019\u4e9b\u50b3\u56de\u503c\u5462?<\/div>\n<div><\/div>\n<div>1.EXEC\u57f7\u884csql\u8a9e\u53e5\u7684\u60c5\u6cc1<\/div>\n<div>\n<pre class=\"lang:tsql decode:true\">declare @rsql Varchar(250)\r\ndeclare @csql Varchar(300)\r\ndeclare @rc Nvarchar(500)\r\ndeclare @cstucount int\r\ndeclare @ccount int\r\nset @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''\u5426'') and ClassroomType=''1'''\r\n--exec(@rsql)\r\nset @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '\r\nset @rc=@csql+@rsql\r\nexec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--\u5c07exec\u7684\u7d50\u679c\u653e\u5165\u8b8a\u6578\u4e2d\u7684\u505a\u6cd5\r\n--select @csql+@rsql<\/pre>\n<p>&#8211;select @cstucount\u4e0a\u9762\u7684@rc\u9019\u500bsql\u8a9e\u53e5\u7684\u529f\u80fd\u662f\u627e\u51fa\u7279\u5b9a\u6642\u9593\u6bb5\u88e1\u6240\u6709\u6709\u7a7a\u7684\u6559\u5ba4\u6578\u91cf\u4ee5\u53ca\u9019\u4e9b\u6559\u5ba4\u6240\u80fd\u5bb9\u7d0d\u7684\u5b78\u751f\u4eba\u6578,\u56e0\u70ba\u6d89\u53ca\u5230\u52d5\u614b\u7684sql\u8a9e\u53e5\uff08@csql\u9019\u53e5\u88e1\u689d\u4ef6\u4e2d\u6709\u4e00\u500b\u5217\u540d\u662f\u52d5\u614b\u8b8a\u5316\u7684\uff09\u7684\u69cb\u9020,\u6240\u4ee5\u8981\u653e\u5728exec\u88e1\u57f7\u884c\uff0c\u4f46\u662f\u540c\u6642\u6211\u53c8\u8981\u8fd4\u56de2\u500b\u7d50\u679c\uff0c\u6240\u4ee5\u57f7\u884c\u6642\u7684\u4ee3\u78bc\u70ba\uff1a<\/p>\n<\/div>\n<div>\n<pre class=\"lang:tsql decode:true\">exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--\u5c07exec\u7684\u7d50\u679c\u653e\u5165\u8b8a\u6578\u4e2d\u7684\u505a\u6cd5\r\n<\/pre>\n<p>\u9019\u6a23\u5c31\u5c07\u50b3\u56de\u503c\u653e\u5230\u4e86\uff0c@cstucount\uff0c@ccount\u5169\u500b\u8b8a\u6578\u4e2d\uff0c\u5f97\u5230\u4e86\u6211\u5011\u60f3\u8981\u7684\u7d50\u679c\u3002<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>2.exec\u57f7\u884c\u5e36\u50b3\u56de\u503c\u7684\u9810\u5b58\u7a0b\u5e8f\u7684\u60c5\u6cc1<\/div>\n<p>\u6211\u5011\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u9810\u5b58\u7a0b\u5e8f\uff1a<\/p>\n<div>\n<pre class=\"lang:tsql decode:true\">create procedure ProTest\r\n(\r\n@name Varchar(10),\r\n@money int output\r\n)\r\nas\r\nbegin\r\nif(@name='1')\r\n  set @money=1000\r\nelse\r\n  set @money=2000\r\nend<\/pre>\n<p>\u9019\u500b\u53ea\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u9019\u500b\u9810\u5b58\u7a0b\u5e8f\u8fd4\u56de\u7684\u662f@money \u9019\u500b\u53c3\u6578\u7684\u503c\uff0c\u90a3\u9ebc\u7576\u6211\u5011\u5728\u53e6\u5916\u4e00\u500b\u9810\u5b58\u7a0b\u5e8f\u4e2d\u8abf\u7528\u6b64\u9810\u5b58\u7a0b\u5e8f\u7684\u6642\u5019\u5982\u4f55\u7372\u53d6\u9019\u500b\u53c3\u6578\u5462\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/div>\n<pre class=\"lang:tsql decode:true\">declare @m int ---\u7528\u4f86\u63a5\u6536\u50b3\u56de\u503c\u7684\u8b8a\u6578\r\nexec ProTest @name='1',@money=@m output --\u4e00\u5b9a\u8981\u6ce8\u540d\u662foutput<\/pre>\n<p>\u5c31\u9019\u9ebc\u7c21\u55ae\uff0c\u6211\u5011\u5c31\u7372\u5f97\u4e86\u50b3\u56de\u503c\uff0c\u7136\u5f8c\u5c31\u53ef\u4ee5\u5229\u7528\u5b83\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\uff1a \u5728\u8cc7\u6599\u5eab\u7a0b\u5f0f\u958b\u767c\u7684\u904e\u7a0b\u4e2d\uff0c\u6211\u5011\u7d93\u5e38\u6703\u78b0\u5230\u5229\u7528EXEC\u4f86\u57f7\u884c\u4e00\u6bb5\u9700\u8981\u8fd4\u56de\u67d0\u4e9b\u503c\u7684sql\u8a9e\u53e5\uff08\u901a\u5e38\u662f\u69cb\u9020\u52d5 &hellip; <a href=\"https:\/\/jason695.why3s.tw\/wordpress\/2017\/02\/06\/sql-server%e4%b8%ad%e7%8d%b2%e5%be%97exec%e7%9a%84%e5%80%bc\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587 <span class=\"screen-reader-text\">SQL Server\u4e2d\u7372\u5f97EXEC\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-2584","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\/2584","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=2584"}],"version-history":[{"count":0,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts\/2584\/revisions"}],"wp:attachment":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/media?parent=2584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/categories?post=2584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/tags?post=2584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}