{"id":562,"date":"2024-11-15T00:23:30","date_gmt":"2024-11-14T16:23:30","guid":{"rendered":"http:\/\/www.wengdao.net\/?p=562"},"modified":"2024-11-15T00:23:30","modified_gmt":"2024-11-14T16:23:30","slug":"mysql-%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e8%bf%90%e7%94%a8","status":"publish","type":"post","link":"https:\/\/jiandao.zliby.com\/?p=562","title":{"rendered":"MYSQL \u5b58\u50a8\u8fc7\u7a0b\u7684\u8fd0\u7528"},"content":{"rendered":"<p>mysql\u5b58\u50a8\u8fc7\u7a0b\u662f\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u7ec4\u9884\u7f16\u8bd1 SQL \u8bed\u53e5\u3002\u5b58\u50a8\u8fc7\u7a0b\u901a\u5e38\u7528\u4e8e\u5c01\u88c5\u590d\u6742\u7684\u64cd\u4f5c\u903b\u8f91\uff0c\u7b80\u5316\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u5b58\u50a8\u8fc7\u7a0b\u652f\u6301\u8f93\u5165\u53c2\u6570\u3001\u8f93\u51fa\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u5305\u542b\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u5982\u6761\u4ef6\u5224\u65ad\u3001\u5faa\u73af\u7b49\uff09\uff0c\u7528\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<p>\u5b58\u50a8\u8fc7\u7a0b<\/p>\n<pre><code class=\"prettyprint\" >\n# \u7b80\u6613\u6a21\u578b\nCREATE PROCEDURE procedure_name ([parameters])\nBEGIN\n    -- SQL statements\nEND;\n\nprocedure_name: \u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u79f0\nparameters: \u8f93\u5165\u3001\u8f93\u51fa\u53c2\u6570\u5217\u8868\uff08\u53ef\u9009\uff09\nBEGIN ... END: \u5b58\u50a8\u8fc7\u7a0b\u7684\u4e3b\u4f53\u90e8\u5206\uff0c\u5305\u542b SQL \u8bed\u53e5\u3002\n\n# \u5b58\u50a8\u8fc7\u7a0b\u652f\u6301\u4e09\u79cd\u7c7b\u578b\u7684\u53c2\u6570\uff1a\n\nIN\uff1a\u8f93\u5165\u53c2\u6570\u3002\nOUT\uff1a\u8f93\u51fa\u53c2\u6570\u3002\nINOUT\uff1a\u8f93\u5165\u8f93\u51fa\u53c2\u6570\u3002\n\n\u793a\u4f8b\uff1a\n\nDELIMITER $$\nCREATE PROCEDURE GetEmployeeDetails(\n    IN emp_id INT,          -- \u8f93\u5165\u53c2\u6570\n    OUT emp_name VARCHAR(100),  -- \u8f93\u51fa\u53c2\u6570\n    OUT emp_salary DECIMAL(10,2)  -- \u8f93\u51fa\u53c2\u6570\n)\nBEGIN\n    SELECT name, salary\n    INTO emp_name, emp_salary\n    FROM employees\n    WHERE employee_id = emp_id;\nEND $$\n\nDELIMITER ;\n<\/code><\/pre>\n<p>\u8c03\u7528<\/p>\n<pre><code class=\"prettyprint\" >CALL GetEmployeeDetails(1001, @name, @salary);\nSELECT @name, @salary;\n<\/code><\/pre>\n<p>\u6d41\u7a0b\u63a7\u5236\u793a\u4f8b:<\/p>\n<pre><code class=\"prettyprint\" >DELIMITER $$\n\nCREATE PROCEDURE CheckSalary(IN emp_id INT)\nBEGIN\n    DECLARE emp_salary DECIMAL(10,2);\n\n    SELECT salary INTO emp_salary\n    FROM employees\n    WHERE employee_id = emp_id;\n\n    IF emp_salary &gt; 10000 THEN\n        SELECT &#039;Salary is high&#039; AS Message;\n    ELSE\n        SELECT &#039;Salary is normal&#039; AS Message;\n    END IF;\nEND $$\n\nDELIMITER ;\n<\/code><\/pre>\n<p>\u5220\u9664\u5b58\u50a8\u8fc7\u7a0b<\/p>\n<p><code class=\"prettyprint\" >DROP PROCEDURE procedure_name;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql\u5b58\u50a8\u8fc7\u7a0b\u662f\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u7ec4\u9884\u7f16\u8bd1 SQL \u8bed\u53e5\u3002\u5b58\u50a8\u8fc7\u7a0b\u901a\u5e38\u7528\u4e8e\u5c01\u88c5\u590d\u6742\u7684\u64cd\u4f5c\u903b\u8f91\uff0c\u7b80\u5316\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/562"}],"collection":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=562"}],"version-history":[{"count":1,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}