【问题标题】:how to create a stored procedure containing request where not exists in mysql database如何在mysql数据库中创建一个包含请求的存储过程
【发布时间】:2015-06-25 09:57:19
【问题描述】:

我对“不存在的地方”的请求有疑问 错误是:

您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 附近 'WHERE NOT EXISTS(从用户中选择 1(WHERE Matricule = @Matricule AND mot_d' 在第 11 行

这是我的程序

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

【问题讨论】:

    标签: mysql insert exists


    【解决方案1】:

    存储过程中的一些问题:

    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 ;
    

    更新

    我的版本,肯定行不通,但会帮你解决问题。

    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 ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多