2015-06-25 45 views
0

我有「其中不存在」 錯誤是請求一個問題:如何創建包含其中不存在mysql數據庫請求存儲過程

您的SQL語法錯誤;檢查 對應於你的MySQL服務器版本的使用權語法手冊 附近 'WHERE NOT EXISTS(SELECT 1 FROM用戶(WHERE Matricule = @Matricule和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 

回答

0

在存儲過程中的一些問題:

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

我的版本,這肯定是行不通的,但會幫助您解決問題。

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 ; 
相關問題