简体   繁体   English

如何在MySQL Workbench中创建代码段?

[英]How to create snippet in MySQL Workbench?

I'm looking for a snippet(template) that will create a procedure and also do exception handling. 我正在寻找一个片段(模板),它将创建一个过程并进行异常处理。 I know there are already some snippets for different operations but I want to add my own snippet. 我知道已有一些针对不同操作的片段,但我想添加自己的片段。

There's a litte button to add the entire content of the current editor or only its selection (if any) in the snippet toolbar: 有一个litte按钮可以添加当前编辑器的全部内容或仅添加其在片段工具栏中的选择(如果有):

在此输入图像描述

  1. Snippet is a programming term for a small region of re-usable source code, machine code, or text. Snippet是一个可重复使用的源代码,机器代码或文本的小区域的编程术语。
  2. For MySQL Workbench all snippets are located in “C:\\Program Files (x86)\\MySQL\\MySQL Workbench 6.0 CE\\snippets”. 对于MySQL Workbench,所有代码段都位于“C:\\ Program Files(x86)\\ MySQL \\ MySQL Workbench 6.0 CE \\ snippets”中。 If you want add your own snippet you can add it in the above directory else you can also edit the existing one. 如果您想添加自己的代码段,可以将其添加到上面的目录中,否则您也可以编辑现有的代码段。
  3. Below is the sample snippet which will create stored procedure with exception handling. 下面是示例代码段,它将创建带有异常处理的存储过程。

     CREATE PROCEDURE Syntax DELIMITER $$ CREATE PROCEDURE `new_procedure` () BEGIN /* Created By - Discription - --------------------------- Modified By - Discription - */ DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN rollback; GET DIAGNOSTICS CONDITION 1 @errorNo = MYSQL_ERRNO, @errorText = MESSAGE_TEXT; SET @spName = 'new_procedure'; # SELECT concat('[',@spName,']-(',@errorNo,'-',@errorText,')'); INSERT INTO errorlogs (ErrorType, `Error`, DateCreated) VALUES (4, concat('[',@spName,']-(',@errorNo,'-',@errorText,')'), now()); END; SET autocommit = 0; START TRANSACTION; ##################### #Put your logic here ##################### COMMIT; END 
  4. Save the above file as 'CustomeSnippet.txt' and add it in “C:\\Program Files (x86)\\MySQL\\MySQL Workbench 6.0 CE\\snippets”. 将上述文件保存为“CustomeSnippet.txt”并将其添加到“C:\\ Program Files(x86)\\ MySQL \\ MySQL Workbench 6.0 CE \\ snippets”中。

  5. Now open your MySQL workbench and connect to your database. 现在打开MySQL工作台并连接到您的数据库。 In the right side bar you will find your 'CustomeSnippet' listed in the dropdown. 在右侧栏中,您会在下拉列表中找到“CustomeSnippet”。 在此输入图像描述

  6. Use it and enjoy coding :-) 使用它并享受编码:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM