简体   繁体   English

如何使用 MySQL Workbench 创建函数?

[英]How to create a function using MySQL Workbench?

I have an MSSQL background.我有 MSSQL 背景。

I am trying to create a function in MySQL Workbench by rightclicking on 'Functions' > 'Create Function' .我试图通过右键单击'Functions' > 'Create Function'在 MySQL Workbench 中创建一个函数。

I insert this text to create the function into the window but it says there are errors in sql at the last line missing 'if' .(SQL below).我插入此文本以在窗口中创建函数,但它说最后一行missing 'if' sql 中存在错误。(下面的 SQL)。 What am I missing?我错过了什么?

2nd Qn.第二问。 (Related) (有关的)

If I create the function using the function SQL (not using the menu in MySQL Workbench), the function gets created but it doesn't appear in the 'Functions' being shown in the schema am working on.如果我使用 SQL 函数(不使用 MySQL Workbench 中的菜单)创建函数,该函数将被创建,但它不会出现在正在处理的架构中显示的“函数”中。 What is the recommended way to create functions in MySQL Workbench?在 MySQL Workbench 中创建函数的推荐方法是什么?

Thanks.谢谢。

CREATE FUNCTION fnIsExcluded(ConcattedString NVARCHAR(15), InValue DECIMAL)
RETURNS BIT 

BEGIN 
    DECLARE individual VARCHAR(20) DEFAULT NULL;
    DECLARE ReturnValue BIT;

    IF (LENGTH(ConcattedString)) < 1
        THEN
            SET ReturnValue = 0;


    ELSE IF ConcattedString IS NULL
    THEN
        SET ReturnValue = 0;


    ELSE IF InValue IS NULL
    THEN

        SET ReturnValue = 0;

    ELSE
     SET ReturnValue = 1;

    END IF;

     RETURN ReturnValue;

END;

You didn't say what version of MySQL Workbench you are using.您没有说明您使用的是哪个版本的 MySQL Workbench。

1) The syntax error in the routine editor you is because in older versions the editor did not handle delimiters automatically. 1)例程编辑器中的语法错误你是因为在旧版本中编辑器没有自动处理分隔符。 Hence you'd need a command to change the default delimiter, like:因此,您需要一个命令来更改默认分隔符,例如:

delimiter $$

(at the top of the text) to change the default (semicolon) to a custom one (double dollar). (在文本顶部)将默认值(分号)更改为自定义值(双美元)。 The reason you have to do this is that the default delimiter is needed in the function definition and if not changed the client (here MySQL Workbench) would wrongly split your function command into individual commands.您必须这样做的原因是函数定义中需要默认分隔符,如果不更改客户端(此处为 MySQL Workbench),则会错误地将您的函数命令拆分为单独的命令。 However, you can update to the latest MySQL Workbench version (6.2.3 at the time of this writing) to have this handled automatically.但是,您可以更新到最新的 MySQL Workbench 版本(在撰写本文时为 6.2.3)以自动处理此问题。

2) Usually changes in the db structure cannot be picked up automatically because it would mean to load all db content at a frequent intervals, which is not feasible for any non-trivial db. 2) 通常无法自动获取 db 结构中的更改,因为这意味着以频繁的间隔加载所有 db 内容,这对于任何非平凡的 db 都是不可行的。 Hence you have to trigger this manually, by clicking the refresh button.因此,您必须通过单击刷新按钮手动触发此操作。

在此处输入图片说明

However, some changes (especially additions or deletions you did yourself in MySQL Workbench) either are already tracked or will be there in future versions).但是,某些更改(尤其是您在 MySQL Workbench 中自己进行的添加或删除)已经被跟踪或将在未来版本中存在。

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

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