1

我有一些麻烦DECLARECALL功能和IF解决。到目前为止我所拥有的是:

CREATE PROCEDURE number_of_projects(project_name VARCHAR)
BEGIN
    DECLARE variable INT;
    SET variable = 4;
    SELECT variable;

    SELECT count(project_id) FROM atm_projects WHERE project_name = variable;
END

IF number_of_projects("PROJECT NAME") = 0
    THEN
    INSERT INTO atm_projects(project_id,project_name,added_from_mti)
    VALUES (project_id,'PROJECT NAME',1)
ENDIF

project_name主要目标是在不重复的表中插入一行。

我可以更改project_nameUNIQUE密钥,但请告诉我我的代码有什么问题,我该如何解决?

我需要了解 a PROCEDURE, CALL procedure,的IF工作原理。

4

3 回答 3

1

尝试:

CREATE PROCEDURE number_of_projects(project_name VARCHAR(255))
BEGIN
    DECLARE var_project_no INT;
    SET var_project_no = 0;
    SELECT var_project_no;

    SELECT count(project_id) INTO var_project_no FROM atm_projects WHERE var_project_name = 4;


    IF var_project_no = 0
    THEN
        INSERT INTO atm_projects(project_id,project_name,added_from_mti)
        VALUES (project_id,'PROJECT NAME',1)
    END IF;
END;
于 2012-07-26T11:03:36.560 回答
0

您已将 project_name 声明为 varchar,但未指定长度。将其更改为 varchar(100)

于 2012-07-26T10:57:31.897 回答
0
CREATE PROCEDURE number_of_projects(project_name VARCHAR(255))
BEGIN

 if NOT  exists(SELECT Top 1 1  FROM atm_projects WHERE var_project_name = 4)
 BEGIN 
   INSERT INTO atm_projects(project_id,project_name,added_from_mti)
   Select project_id,project_name ,1
 END
END;
于 2012-07-26T11:09:40.783 回答