Mysql/Oracle自定义函数/存储过程报错合集

参考: Mysql自定义函数报错解决方法

1.在MySql中创建自定义函数报错信息如下:

1.1 错误显示

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)

1.2 错误原因

开启了二进制日志binlog后,自定义函数需要声明包含这几个关键字的一个或多个
在 MySQL 中,如果你要创建一个自定义函数,并且数据库启用了二进制日志记录(binary logging),则函数必须在其声明中包含 DETERMINISTIC、NO SQL 或 READS SQL DATA 中的一个或多个关键字,以确保函数的安全性和可靠性。
这是我们开启了bin-log, 我们就必须指定我们的函数是否是
1 DETERMINISTIC 不确定的
2 NO SQL 没有SQl语句,当然也不会修改数据
3 READS SQL DATA 只是读取数据,当然也不会修改数据
4 MODIFIES SQL DATA 要修改数据
5 CONTAINS SQL 包含了SQL语句

1.3 解决方法:

mysql>set global log_bin_trust_function_creators=1; ##true也可以

2. Mysql没有 create or replace function

Oracle 下这种是可以的, 但是Mysql只有 create or replace procedure
这里要创建函数时实现相同效果,可以这么写:
drop function if exists XXX

3.不允许直接在过程或函数中使用聚合函数,如max

create function get_max_number(num1 int, num2 int, num3 int)
returns int 
begin
	declare maxNum int;
	set maxNum = max(num1, num2);
	set maxNum = max(maxNum, num3);
	return maxNum;
end;

SQL 错误 [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' num2); set maxNum = max(maxNum, num3); return maxNum; end' at line 5

4.游标和处理程序声明之后不允许变量或条件声明

SQL 错误 [1337] [42000]: Variable or condition declaration after cursor or handler declaration

create procedure pro_delete_all_order()
begin 
	declare cur_orders cursor for select order_num from orders;
	declare done int default false;
	declare order_var int;
	declare continue handle for not found set done = true;	

	open cur_orders;
	repeat
		fetch order_orders into order_var;
		if 
		delete from orders where order_num = order_var;
		delete from orderitems where order_num = order_var;
	until done end repeat;
end;

2. 在Oracle中自定义存储过程报错如下

2.1 在在 PL/SQL 中,使用了 -- XXX 注释

SQL 错误 [6550] [65000]: ORA-06550: 第 16 行, 第 3 列: 
PLS-00103: 出现符号 "end-of-file"在需要下列之一时:
 ; <an identifier>
   <a double-quoted delimited-identifier>

set serveroutput ON;
BEGIN
	-- 执行存储过程
  pro_delete_all_order;
  DBMS_OUTPUT.PUT_LINE('Employee with ID 100 exists.');
  ROLLBACK;
END;

去掉之后就好了!

2.2 PL/SQL中select 必须使用into 填充变量

SQL 错误 [6550] [65000]: ORA-06550: 第 2 行, 第 2 列: 
PLS-00428: 在此 SELECT 语句中缺少 INTO 子句

BEGIN
	SELECT * FROM orders;
	pro_delete_all_order;
	ROLLBACK;
END;

2.3 Oracle存储过程编译错误,但是命令行不提示错误!!!

看上去是成功创建了存储过程,但是实际上存在错误!!!

可从下图中看出,实际上编译未通过,跟Mysql不一(创建都不行)样,这里能创建,但不能通过编译.

如何查看错误信息呢?

--查看存储过程编译后的错误信息--
select * from SYS.USER_ERRORS where NAME = upper('get_product_info');

posted @   DawnTraveler  阅读(276)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示