当前位置:网站首页>MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
2022-06-25 12:47:00 【good_ good_ xiu】
Problem scenario
Try to write mysql Function to sort the data in the record , When the first data is obtained according to the demand ,sql Medium limit @ Variable ,1 Syntax has never been compiled .
The error information is as follows :
[Err] 1064 - 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 '@N,1
Question why
limit n,m The syntax requirement of the statement is , The next two parameters must be integer constants , Cannot be a variable . So in the function select * from a order by xx limit @ Variable ,1 The function compilation fails .
resolvent
1、 Use integer constants
First, receive a dynamic integer parameter according to the function parameter , If the integer parameter does not need operation, it is directly placed in limit Back .
for example : When select test(1); Will return to select xxx limit 1,1 The data of
CREATE FUNCTION test(N INT) RETURNS double
BEGIN
RETURN select distinct gz from gzb order by gz desc limit N,1;
END
If the function parameter needs to be involved in the operation before it can get the actual value , Also use constants , because limit (n-1),1 It will also fail to compile . Because no matter 2-1 perhaps n-1 It's all variables . So we have to process the value in advance into a constant .
for example :
CREATE FUNCTION test(N INT) RETURNS double
BEGIN
set cl = N-1
RETURN select distinct gz from gzb order by gz desc limit cl,1;
END
So that we don't make a mistake , because cl The value of has passed n-1 Only when you get it sql, and limit (n-1),1 perhaps set @cl = n-1;limit @cl,1 Wait until sql The value is calculated after execution , So it will fail to compile .
2、 Writing stored procedures + precompile
stay mysql-5.0.7 Later versions , Updated to preprocess sql sentence , When processing, put variables to execute .
CREATE PROCEDURE test(in n INT)
BEGIN
set @mmsql = 'select distinct gz from gzb order by gz desc limit ?,1';
set @cl = n-1;
prepare exce from @mmsql;
EXECUTE exce USING @cl;
DEALLOCATE prepare exce;
END
Tips
Declare two types and ranges of variables
-- local variable
--Declare Variable name type default The default value is ;
declare N int defaul 0; -- Declare a variable named N Integral variable of , The default value is 0
-- Global variables
-- set @ Variable name = value
set @N = 'xxx'; -- Declare global variables @N, The value is a string xxx
Stored procedure syntax
Calling stored procedure : call Store name ( The ginseng );
View all stored procedures :show procedure status;
View the stored procedure creation statement :show create procedure Store name ;
Delete stored procedure :drop procedure Store name ;
create procedure name (in|out|inout Parameter name Parameter type ) -- Multiple parameters are separated by commas
begin
-- sql sentence ;
end
-- in The value representing the parameter is obtained from the user input ( The default is in)
-- out The value representing the parameter will be output to the user , But in stored procedures , The value passed in for this parameter is NULL
-- inout It can receive parameters , You can also output parameters
-- for example :
set @a = 1;
set @b = 2;
set @c = 3;
select @a,@b,@c; -- 1,2,3
create procedure demo(a int, out b int, inout c int)
begin
select a,b,c; -- 1,NULL,3
set a = 10;
set b = 10;
set c = 10;
select a,b,c; -- 10,10,10
end
call demo(@a,@b,@c);
select @a,@b,@c; -- 1,10,10
After the execution , Obviously @b,@c It's all changed 
Function syntax
Call function : select The name of the function ( The ginseng );
View all custom functions :show function status;
View the function creation statement :show create function The name of the function ;
Delete function :drop function The name of the function ;
CREATE FUNCTION The name of the function ( Parameter name Parameter type ) RETURNS return type
begin
-- xxxx
end
-- for example : Output 1+2+...+n Result
create function Accumulate(n int) returns int
begin
declare i int default 1;
declare sum int default 0;
while i <= n do
set sum = sum + i;
set i = i + 1;
end while;
return sum;
end
-- Or use loop and leave Loop and jump out of the loop to achieve accumulation
create function Accumulate(n int) RETURNS int
begin
declare i int default 1;
declare sum int default 0;
add_num:LOOP
set sum = sum + i;
set i = i + 1;
IF i = n + 1 THEN LEAVE add_num;
END IF;
END LOOP add_num;
return sum;
end
-- Call function
select Accumulate(5); -- 15 = 1 + 2 + 3 + 4 + 5

边栏推荐
- Error while sending STMT_ PREPARE packet. PID=29294
- laravel 9
- 为何数据库也云原生了?
- Render values to corresponding text
- Connect with the flight book and obtain the user information according to the userid
- PHP files running online
- Qiantang Pingou source code -- Qiantang Pingou app system development source code sharing
- 浏览器的5种观察器
- JS function exercises
- Array reorder based on a field
猜你喜欢

Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection

High performance + million level Excel data import and export
![Maximum number [abstract rules for abstract sorting]](/img/47/f6bafacc95f487854a3e304325f3f0.png)
Maximum number [abstract rules for abstract sorting]

Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation

(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)

MySQL common interview questions

CUDA error: unspecified launch failure

Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability

515. Find Largest Value in Each Tree Row

Penetration tool environment - installing sqli labs in centos7 environment
随机推荐
Error while sending STMT_ PREPARE packet. PID=29294
又是被Visdom搞崩溃的一夜
Jeecgboot startup popup configuration is still incorrect
Event triggered when El select Clear clears content
冷启动的最优解决方案
Ubuntu uninstalling PHP
3+1保障:高可用系统稳定性是如何炼成的?
为何数据库也云原生了?
聊聊高可用的 11 个关键技巧
Online blind box system development function introduction and some source code sharing
使用Visio画立方体
Huile optimization system -- sharing of secondary development source code of huile optimization app system
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
Summary of common MySQL database commands (from my own view)
JS array length is defined
PHP takes the difference set of two arrays
PHP files running online
Total number of MySQL statistics, used and unused
Baidu search stability analysis story
Flutter automatically disappears after receiving push