The SQL stored procedure allows you to add new users to determine if they already exist.

Source: Internet
Author: User
This article shares an article about how to use stored procedures to add new users and determine whether a feature exists. We used to write it in a separate SQL statement. Let's take a look at the implementation method below.

This article shares an article about how to use stored procedures to add new users and determine whether a feature exists. We used to write it in a separate SQL statement. Let's take a look at the implementation method below.

The Code is as follows:

DELIMITER $

Drop procedure if exists 'monitor'. 'P _ user _ insertuser' $

# If a stored procedure with the same name exists, delete the stored procedure. monitor is named p_user_Insertuser and stored procedure name (p _ indicates _ operation + indicates)

Create definer = 'liteng' @ '%' PROCEDURE 'P _ user_Insertuser '(

UserName varchar (200) # input parameter userName at the DAL layer (data access layer)

)

BEGIN

Declare id int default 0; # variable id that defines the default value of int

USER_ID into id from monitor_user where USER_Name = userName and USER_State = 1 limit 1;

# In the search table, the USER_Name column is equal to the userName parameter and the USER_State column (user status. When a user is deleted, this value is 0; if the user exists, it is 1, and put the data primary key into the variable id.

If (! Id) then # if there is no data equal to the variable userName, that is, no such user has registered

Insert into monitor_user (USER_Name) VALUES (userName );

# Insert data with userName

Select row_count (); # returns the number of affected rows.

Else

Select-1; # If the data already exists,-1 is returned.

End if;

END $

DELIMITER;

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.