一个简单的用户函数却不会写

青出于蓝 2014-07-18 03:51:03
我要编写一个用户函数,并在函数内部进行一些简单判断,并返回值。但居然查找了N多资料都无法实现这么一个简单的需求,编译执行时会提示语法错误。求各高手帮助啊!

CREATE FUNCTION f_MyFun1(Param1 TEXT)
RETURNS INT

IF (Param1 LIKE 'ABC') THEN
RETURN 1;
END IF;

IF (Param1 LIKE 'DEF') THEN
RETURN 2;
ELSE
RETURN 3;
END IF;

上在只是例子,但不要告诉我使用IF()函数。意思是函数里面有会一些判断(IF),并根据这些判断返回一些值。我觉得上面的代码应该是可以的,但执行时就是会提示语法错误。
...全文
253 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
青出于蓝 2014-07-21
  • 打赏
  • 举报
回复
谢谢各位,WWWWA的是正解。
ACMAIN_CHM 2014-07-18
  • 打赏
  • 举报
回复
少了begin / end 这个在MYSQL的官方免费手册中有例子啊。
引用
12.8. MySQL Compound-Statement Syntax This section describes the syntax for the BEGIN ... END compound statement and other statements that can be used in the body of stored programs: Stored procedures and functions, triggers, and events. These objects are defined in terms of of SQL code that is stored on the server for later invocation (see Chapter 18, Stored Programs and Views). 12.8.1. BEGIN ... END Compound Statement Syntax [begin_label:] BEGIN [statement_list] END [end_label] BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs. A compound statement can contain multiple statements, enclosed by the BEGIN and END keywords. statement_list represents a list of one or more statements, each terminated by a semicolon (;) statement delimiter. statement_list is optional, which means that the empty compound statement (BEGIN END) is legal. Use of multiple statements requires that a client is able to send statement strings containing the ; statement delimiter. This is handled in the mysql command-line client with the delimiter command. Changing the ; end-of-statement delimiter (for example, to //) allows ; to be used in a program body. For an example, see Section 18.1, “Defining Stored Programs”. A compound statement can be labeled. end_label cannot be given unless begin_label also is present. If both are present, they must be the same. The optional [NOT] ATOMIC clause is not supported. This means that no transactional savepoint is set at the start of the instruction block and the BEGIN clause used in this context has no effect on the current transaction. 12.8.2. DECLARE Syntax The DECLARE statement is used to define various items local to a program: Local variables. See Section 12.8.3, “Variables in Stored Programs”. Conditions and handlers. See Section 12.8.4, “Conditions and Handlers”. Cursors. See Section 12.8.5, “Cursors”. DECLARE is allowed only inside a BEGIN ... END compound statement and must be at its start, before any other statements. Declarations must follow a certain order. Cursors must be declared before declaring handlers, and variables and conditions must be declared before declaring either cursors or handlers. 12.8.3. Variables in Stored Programs You may declare and use variables within stored programs. 12.8.3.1. DECLARE for Local Variables DECLARE var_name [, var_name] ... type [DEFAULT value] This statement is used to declare local variables within stored programs. To provide a default value for the variable, include a DEFAULT clause. The value can be specified as an expression; it need not be a constant. If the DEFAULT clause is missing, the initial value is NULL. Local variables are treated like stored routine parameters with respect to data type and overflow checking. See Section 12.1.12, “CREATE PROCEDURE and CREATE FUNCTION Syntax”. Local variable names are not case sensitive. The scope of a local variable is within the BEGIN ... END block where it is declared. The variable can be referred to in blocks nested within the declaring block, except those blocks that declare a variable with the same name. 12.8.3.2. Variable SET Statement SET var_name = expr [, var_name = expr] ... The SET statement in stored programs is an extended version of the general SET statement (see Section 12.5.4, “SET Syntax”). Each var_name may refer to a local variable declared inside a stored program, a system variable, or a user-defined variable. The SET statement in stored programs is implemented as part of the pre-existing SET syntax. This allows an extended syntax of SET a=x, b=y, ... where different variable types (locally declared variables, global and session system variables, user-defined variables) can be mixed. This also allows combinations of local variables and some options that make sense only for system variables; in that case, the options are recognized but ignored. 12.8.3.3. SELECT ... INTO Statement SELECT col_name [, col_name] ... INTO var_name [, var_name] ... table_expr SELECT ... INTO syntax enables selected columns to be stored directly into variables. The query should return a single row. If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query returns multiple rows, error 1172 occurs (Result consisted of more than one row). If it is possible that the statement may retrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row. SELECT id,data INTO x,y FROM test.t1 LIMIT 1; User variable names are not case sensitive. See Section 8.4, “User-Defined Variables”. In the context of SELECT ... INTO statements that occur as part of events executed by the Event Scheduler, diagnostics messages (not only errors, but also warnings) are written to the error log, and, on Windows, to the application event log. For additional information, see Section 18.4.5, “Event Scheduler Status”. 12.8.3.4. Scope and Resolution of Local Variables The scope of a local variable is within the BEGIN ... END block where it is declared. The variable can be referred to in blocks nested within the declaring block, except those blocks that declare a variable with the same name. Local variables are within scope only during stored routine execution, so references to them are disallowed within prepared statements because those are global to the current session and the variables might have gone out of scope when the statement is executed. For example, SELECT ... INTO local_var cannot be used as a prepared statement. Local variable names should not be the same as column names. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable. For example, in the following statement, xname is interpreted as a reference to the xname variable rather than the xname column: CREATE PROCEDURE sp1 (x VARCHAR(5)) BEGIN DECLARE xname VARCHAR(5) DEFAULT 'bob'; DECLARE newname VARCHAR(5); DECLARE xid INT; SELECT xname,id INTO newname,xid FROM table1 WHERE xname = xname; SELECT newname; END; When this procedure is called, the newname variable returns the value 'bob' regardless of the value of the table1.xname column. See also Section D.1, “Restrictions on Stored Routines, Triggers, and Events”. 12.8.4. Conditions and Handlers Certain conditions may require specific handling. These conditions can relate to errors or warnings, as well as to general flow control inside a stored program.
WWWWA 2014-07-18
  • 打赏
  • 举报
回复
DELIMITER $$ CREATE FUNCTION f_MyFun1(Param1 TEXT) RETURNS INT BEGIN IF (Param1 LIKE 'ABC') THEN RETURN 1; END IF; IF (Param1 LIKE 'DEF') THEN RETURN 2; ELSE RETURN 3; END IF; END$$ DELIMITER ;
小在在 2014-07-18
  • 打赏
  • 举报
回复
少了begin,end

CREATE FUNCTION f_MyFun1(Param1 TEXT)
  RETURNS INT
BEGIN
  IF (Param1 LIKE 'ABC') THEN
    RETURN 1;
  END IF;

  IF (Param1 LIKE 'DEF') THEN
    RETURN 2;
  ELSE
    RETURN 3;
  END IF;
END
小在在 2014-07-18
  • 打赏
  • 举报
回复
提示什么错误? 有没有开启创建函数的功能?

mysql> show variables like '%func%';
+-----------------------------------------+-------+
| Variable_name | Value |
+-----------------------------------------+-------+
| log_bin_trust_function_creators | ON    |
+-----------------------------------------+-------+
1 row in set (0.02 sec)

二、如果Value处值为OFF,则需将其开启。
mysql> set global log_bin_trust_function_creators=1;
源码直接下载地址: https://pan.quark.cn/s/387d7c8f6289 在使用IE浏览器的过程中,偶尔会出现重置为默认配置操作未能顺利完成的现象。这种情况的发生可能源于多种因素,包括注册表项出现偏差、浏览器附加组件之间存在不兼容、操作系统授权设置不当或受到恶意程序影响等。针对这一挑战,我们可以通过一系列系统性的方法来逐一诊断和纠正。首先需要验证当前用户是否具备管理员级别的操作权限。在执行重置配置的操作时,必须确保IE浏览器是以管理员身份启动的。具体操作方式为,使用鼠标右键单击IE程序图标,并从弹出的菜单中选择“以管理员身份运行”这一选项。其次,应当检查是否存在恶意软件。恶意程序有时会非法修改浏览器的配置信息,从而阻碍重置过程的正常进行。用户可以借助Windows系统自带的Microsoft Safety Scanner进行检测,或者选择安装并使用第三方安全防护软件执行全面的系统扫描。紧接着,需要对浏览器的缓存数据以及Cookies文件进行清理。在IE浏览器界面中,通过访问“工具”菜单并选择“Internet选项”,然后在弹出的设置界面中,切换至“常规”标签页,并执行清除浏览历史记录的操作,此过程应包含Cookies和临时互联网文件。倘若上述步骤均未能解决问题,那么可以考虑禁用或移除那些可能引发冲突的浏览器附加组件。相关操作需要在“工具”菜单下找到并打开“管理加载项”,对可疑的插件进行禁用处理,并在浏览器重新启动后再次尝试执行重置操作。再者,修复IE浏览器相关的注册表配置也是至关重要的环节。注册表编辑器是一个功能强大的系统工具,任何不当的操作都可能导致严重的系统问题,因此在进行修改前必须创建系统还原点作为安全备份。用户可以通过按下“Win+R”组合...
内容概要:本文详细介绍了光伏储能虚拟同步发电机(VSG)并网仿真模型的Simulink实现方法,系统研究了光伏发电与储能系统融合VSG技术并入电网的动态特性与控制策略。通过构建完整的VSG控制模块,模拟其在并网过程中的虚拟惯性响应、频率调节能力和有功无功功率支撑特性,验证了该系统在增强电网稳定性、提升新能源并网适应性方面的有效性。模型全面涵盖光伏出力特性、储能系统充放电管理、VSG有功-无功解耦控制、下垂控制机制以及并网点动态响应等关键环节,并通过多种工况下的仿真分析,评估系统在不同运行条件下的动态性能与鲁棒性。; 适合人群:具备电力系统、新能源发电、自动控制等相关专业背景,熟悉Matlab/Simulink仿真环境,从事新能源并网、微电网控制或电力电子系统研究的科研人员、工程技术人员及研究生。; 使用场景及目标:①深入理解光伏储能系统如何通过VSG技术模拟同步发电机的惯性和阻尼特性以支持电网稳定;②掌握虚拟惯量控制、下垂控制等VSG核心控制策略在Simulink中的建模、参数设计与实现方法;③为新型电力系统中新能源并网的动态仿真、稳定性分析及控制策略优化提供可靠的模型基础和技术参考。; 阅读建议:建议结合所提供的仿真模型进行逐步操作与参数调试,重点关注VSG控制环路的结构设计、控制器参数整定及其对系统动态性能的影响,同时可将该模型拓展应用于多机并联、微电网孤岛/并网切换、多能源协同控制等更复杂的系统场景中进行进阶研究。

57,062

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧