{"id":5190,"date":"2024-11-14T11:58:57","date_gmt":"2024-11-14T03:58:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5190\/"},"modified":"2024-11-14T11:58:57","modified_gmt":"2024-11-14T03:58:57","slug":"sql%e4%b8%adexec%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5190\/","title":{"rendered":"sql\u4e2dexec\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  exec \u547d\u4ee4\u5728 sql \u4e2d\u7528\u4e8e\u52a8\u6001\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u6216 sql \u8bed\u53e5\uff0c\u901a\u8fc7\u6307\u5b9a\u53c2\u6570\u548c\u5d4c\u5957\u8c03\u7528\u5b9e\u73b0\u7075\u6d3b\u7684\u4ee3\u7801\u64cd\u4f5c\u3002\u5b83\u652f\u6301\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\u3001\u6267\u884c ad-hoc sql \u8bed\u53e5\u3001\u4f20\u9012\u53c2\u6570\u548c\u5d4c\u5957\u6267\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cexec \u4e0d\u80fd\u6267\u884c ddl \u8bed\u53e5\uff0c\u4e0d\u4f1a\u8fd4\u56de\u53ef\u68c0\u7d22\u7684\u7ed3\u679c\u96c6\uff0c\u4e14\u53ef\u4ee5\u5728\u4e8b\u52a1\u4e2d\u6267\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052419272380688.jpg\" class=\"aligncenter\" title=\"sql\u4e2dexec\u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"sql\u4e2dexec\u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d EXEC \u7684\u7528\u6cd5<\/strong><\/p>\n<p><strong>exec \u547d\u4ee4 \u7b80\u4ecb<\/strong><\/p>\n<p>EXEC \u547d\u4ee4\u7528\u4e8e\u5728 Transact-SQL \u4e2d\u6267\u884c\u53e6\u4e00\u4e2a\u5b58\u50a8\u8fc7\u7a0b\u6216 ad hoc SQL \u8bed\u53e5\u3002\u5b83\u5141\u8bb8\u60a8\u52a8\u6001\u5730\u6267\u884c\u8bed\u53e5\uff0c\u4f7f\u5176\u80fd\u591f\u6839\u636e\u9700\u8981\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>EXEC [ @parameter_name = ] 'statement'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>@parameter_name<\/strong>\uff1a\u53ef\u9009\u53c2\u6570\u540d\u79f0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u3002<\/li>\n<li> <strong>&#8216;statement&#8217;<\/strong>\uff1a\u8981\u6267\u884c\u7684 SQL \u8bed\u53e5\u6216\u5b58\u50a8\u8fc7\u7a0b\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>EXEC \u547d\u4ee4\u53ef\u7528\u4e8e\u4ee5\u4e0b\u76ee\u7684\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<pre>EXEC sp_AddProduct 'Product1', 'Sony'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u6267\u884c ad-hoc SQL \u8bed\u53e5\uff1a<\/strong><\/p>\n<pre>EXEC 'SELECT * FROM Products WHERE Name = 'Product1''<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u4f20\u9012\u53c2\u6570\uff1a<\/strong><\/p>\n<pre>DECLARE @product_id INT\nEXEC sp_GetProductDetails @product_id<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u5d4c\u5957 EXEC \u8c03\u7528\uff1a<\/strong><\/p>\n<pre>EXEC EXEC sp_UpdateProduct 'Product1', 'New Sony'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>EXEC \u547d\u4ee4\u4e0d\u80fd\u7528\u4e8e\u6267\u884c DDL (\u6570\u636e\u5b9a\u4e49\u8bed\u8a00) \u8bed\u53e5\uff0c\u4f8b\u5982 CREATE TABLE \u6216 DROP TABLE\u3002<\/li>\n<li>EXEC \u547d\u4ee4\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u4f46\u5b83\u4e0d\u80fd\u4ece\u4ee3\u7801\u4e2d\u68c0\u7d22\u3002<\/li>\n<li>EXEC \u547d\u4ee4\u53ef\u4ee5\u5728\u4e8b\u52a1\u4e2d\u6267\u884c\uff0c\u53ea\u8981\u5d4c\u5957\u8bed\u53e5\u4e0d\u4f1a\u66f4\u6539\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 EXEC \u547d\u4ee4\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/p>\n<pre>CREATE PROCEDURE sp_AddProduct\n(\n  @name VARCHAR(50),\n  @brand VARCHAR(50)\n)\nAS\nBEGIN\n  -- \u5728 Products \u8868\u4e2d\u63d2\u5165\u65b0\u4ea7\u54c1\n  INSERT INTO Products (Name, Brand) VALUES (@name, @brand);\nEND\nGO\n\nDECLARE @product_name VARCHAR(50) = 'Product1'\nDECLARE @brand VARCHAR(50) = 'Sony'\n\nEXEC sp_AddProduct @product_name, @brand<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e2dexec\u7684\u7528\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>exec \u547d\u4ee4\u5728 sql \u4e2d\u7528\u4e8e\u52a8\u6001\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u6216 sql \u8bed\u53e5\uff0c\u901a\u8fc7\u6307\u5b9a\u53c2\u6570\u548c\u5d4c\u5957\u8c03\u7528\u5b9e\u73b0\u7075\u6d3b\u7684\u4ee3\u7801\u64cd\u4f5c\u3002\u5b83\u652f\u6301\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\u3001\u6267\u884c ad-hoc sql \u8bed\u53e5\u3001\u4f20\u9012\u53c2\u6570\u548c\u5d4c\u5957\u6267\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cexec \u4e0d\u80fd\u6267\u884c ddl \u8bed\u53e5\uff0c\u4e0d\u4f1a\u8fd4\u56de\u53ef\u68c0\u7d22\u7684\u7ed3\u679c\u96c6\uff0c\u4e14\u53ef\u4ee5\u5728\u4e8b\u52a1\u4e2d\u6267\u884c\u3002 SQL \u4e2d EXEC \u7684\u7528\u6cd5 exec \u547d\u4ee4 \u7b80\u4ecb EXEC \u547d\u4ee4\u7528\u4e8e\u5728 Transact-SQL \u4e2d\u6267\u884c\u53e6\u4e00\u4e2a\u5b58\u50a8\u8fc7\u7a0b\u6216 ad hoc SQL \u8bed\u53e5\u3002\u5b83\u5141\u8bb8\u60a8\u52a8\u6001\u5730\u6267\u884c\u8bed\u53e5\uff0c\u4f7f\u5176\u80fd\u591f\u6839\u636e\u9700\u8981\u8fdb\u884c\u64cd\u4f5c\u3002 \u8bed\u6cd5 EXEC [ @parameter_name = ] &#8216;statement&#8217; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a @parameter_name\uff1a\u53ef\u9009\u53c2\u6570\u540d\u79f0\uff0c\u7528\u4e8e\u6307\u5b9a\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u3002 &#8216;statement&#8217;\uff1a\u8981\u6267\u884c\u7684 SQL \u8bed\u53e5\u6216\u5b58\u50a8\u8fc7\u7a0b\u540d\u79f0\u3002 \u7528\u6cd5 EXEC \u547d\u4ee4\u53ef\u7528\u4e8e\u4ee5\u4e0b\u76ee\u7684\uff1a \u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\uff1a EXEC sp_AddProduct &#8216;Product1&#8217;, &#8216;Sony&#8217; \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c ad-hoc SQL \u8bed\u53e5\uff1a EXEC &#8216;SELECT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5190","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5190"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5190\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}