简体   繁体   English

如何创建一个包含MySQL数据库中不存在的请求的存储过程

[英]how to create a stored procedure containing request where not exists in mysql database

I have a problem with the request "where not exists" error is : 请求“哪里不存在”错误是我的问题:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS(SELECT 1 FROM users (WHERE Matricule = @Matricule AND mot_d' at line 11 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第11行“ WHERE NOT EXISTS(SELECT 1 FROM users(WHERE Matricule = @Matricule AND mot_d'')附近使用

here is my procedure 这是我的程序

create procedure usp_testInsert(
    Matricule int,
    mot_de_passe varchar(10),
    Nom varchar(10) ,
    Prenom varchar(10)
) begin
    INSERT INTO users (
        Matricule,
        mot_de_passe,
        Nom,
        Prenom)
    SELECT
        @Matricule, 
        @mot_de_passe, 
        @Nom, 
        @Prenom 
    WHERE 
        NOT EXISTS (
            SELECT 1 
            FROM users ( 
            WHERE 
                Matricule = @Matricule AND 
                mot_de_passe = @mot_de_passe AND 
                Nom = @Nom AND Prenom = @Prenom
            )
        );
end

Some problems in your stored procedure: 您的存储过程中的一些问题:

DELIMITER //

create procedure usp_testInsert (
        Matricule int,
        mot_de_passe varchar(10),
        Nom varchar(10) ,
        Prenom varchar(10)
)
begin
        INSERT INTO users (Matricule, mot_de_passe, Nom, Prenom)
        SELECT @Matricule, @mot_de_passe, @Nom, @Prenom
        -- FROM ???
        WHERE NOT EXISTS (
              SELECT 1
              /*
              FROM users (
              WHERE Matricule = @Matricule AND
              */
              FROM users
              WHERE (
                          Matricule = @Matricule AND
                          mot_de_passe = @mot_de_passe AND
                          Nom = @Nom AND
                          Prenom = @Prenom
              )
        );
end//

DELIMITER ;

UPDATE 更新

My version, which certainly will not work, but will help you solve your problem. 我的版本肯定不会起作用,但是可以帮助您解决问题。

DELIMITER //

DROP PROCEDURE IF EXISTS `usp_testInsert`//

CREATE PROCEDURE `usp_testInsert` (
        `_Matricule` INT,
        `_mot_de_passe` VARCHAR(10),
        `_Nom` VARCHAR(10) ,
        `_Prenom` VARCHAR(10)
)
BEGIN
        INSERT INTO `users` (`Matricule`, `mot_de_passe`, `Nom`, `Prenom`)
        SELECT `_Matricule`, `_mot_de_passe`, `_Nom`, `_Prenom`
        FROM DUAL
        WHERE NOT EXISTS (
                SELECT 1
                FROM `users`
                WHERE (
                          `Matricule` = `_Matricule` AND
                          `mot_de_passe` = `_mot_de_passe` AND
                          `Nom` = `_Nom` AND
                          `Prenom` = `_Prenom`
                )
        );
END//

DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM