Mysql-stored procedure learning notes

Source: Internet
Author: User

1. ID registration

Stored Procedure Code:

Delimiter $;
Drop procedure if exists 'zebra '. 'adduser' $
Create definer = 'root' @ 'localhost' procedure 'adduser' (in uid varchar (50), In upwd varchar (50), In uquest varchar (50 ), in uanswer varchar (50), Out regreturnvalue INT)
Begin
Declare chkid varchar (50 );
Select loginid from login where loginid = uid into chkid;
If (chkid <> '') then
Set regreturnvalue = 10;
Else
Insert into login (loginid, 'Password', question1, answer1) values (UID, upwd, uquest, uanswer );
Set regreturnvalue = 20;
End if;
End $
Delimiter; $

Usage:
Procedure tform1.btn1click (Sender: tobject );
VaR
Errorcode: integer;
Begin
Try
Mystrdprc1.storedprocname: = 'adduser ';
Mystrdprc1.params. parambyname ('uid'). Value: = 'eboy ';
Mystrdprc1.params. parambyname ('upwd '). Value: = 'eewwee ';
Mystrdprc1.params. parambyname ('uquest '). Value: = 'meipaihao ';
Mystrdprc1.params. parambyname ('uancer'). Value: = '20140901 ';
Mystrdprc1.execproc;
Errorcode: = mystrdprc1.params. parambyname ('regreturnvalue'). value;
Case errorcode
10: showmessage ('Id already exists. ');
20: showmessage ('registration successful. ');
End;
Mystrdprc1.close;
Except
End;
End;

2. Password Modification

Stored Procedure Code:

Delimiter $;
Drop procedure if exists 'zebra '. 'editpwd' $
Create definer = 'root' @ 'localhost' procedure 'editpwd' (in uid varchar (50), In oldpwd varchar (50), In newpwd varchar (50), Out editpwdreturnvalue INT)
Begin
Declare chkid varchar (50 );
Declare chkpwd varchar (50 );
Select loginid from login where loginid = uid into chkid;
If (chkid = UID) then
Select 'Password' from login where loginid = uid into chkpwd;
If (chkpwd = oldpwd) then
Update login set 'Password' = newpwd where loginid = uid;
Set editpwdreturnvalue = 30 ;#?????????
Else
Set editpwdreturnvalue = 20 ;#??????????
End if;
Else
Set editpwdreturnvalue = 10; # ID ??????
End if;
End $
Delimiter; $

Usage:

Procedure tform1.btn2click (Sender: tobject );
VaR
Errorcode: integer;
AD: string;
Begin
Try
Mystrdprc1.storedprocname: = 'editpwd ';
Mystrdprc1.params. parambyname ('uid'). Value: = 'eboy ';
Mystrdprc1.params. parambyname ('oldpwd'). Value: = '20140901 ';
Mystrdprc1.params. parambyname ('newpwd'). Value: = '1233d3 ';
Mystrdprc1.execproc;
Errorcode: = mystrdprc1.params. parambyname ('editpwdreturnvalue'). value;
Case errorcode
10: showmessage ('Id does not exist. ');
20: showmessage ('old password is incorrect. ');
30: showmessage ('password modified successfully. ');
End;
Mystrdprc1.close;
Except
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.