以下是我的MySQL程序。
DELIMITER $$
DROP PROCEDURE IF EXISTS set_dummy_feedback $$
CREATE PROCEDURE set_dummy_feedback ( )
BEGIN
-- First we declare all the variables we will need
DECLARE current_code,current_emp_id,current_subject_id VARCHAR(150);
DECLARE current_count_facultyfeedback,current_count_studentfeedback INT DEFAULT 0;
DECLARE temp_index INT DEFAULT 0;
DECLARE gpa DECIMAL(4,2);
-- flag which will be set to true, when cursor reaches end of table
DECLARE exit_loop BOOLEAN;
-- Declare the sql for the cursor
DECLARE example_cursor CURSOR FOR
SELECT DISTINCT emp_id,subject_id,code,c
FROM feedback_subjectfaculty
ORDER BY `c` ASC ;
-- Let mysql set exit_loop to true, if there are no more rows to iterate
DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
-- open the cursor
OPEN example_cursor;
-- marks the beginning of the loop
example_loop: LOOP
-- read the name from next row into the variable l_name
FETCH example_cursor INTO current_emp_id,current_subject_id,current_code,current_count_facultyfeedback;
-- check if the exit_loop flag has been set by mysql,
-- if it has been set we close the cursor and exit
-- the loop
IF exit_loop THEN
CLOSE example_cursor;
LEAVE example_loop;
END IF;
-- fetch data from table feedback_studentfeedback
SELECT SQL_CALC_FOUND_ROWS * FROM `feedback_studentfeedback` where
fb_subjects_10 like CONCAT("%",current_code,"%") ;
-- save the number of rows fetched in the previous select
SET current_count_studentfeedback = FOUND_ROWS() ;
SET temp_index = current_count_studentfeedback ;
IF temp_index IS NULL
THEN SET temp_index = 0;
END IF;
IF current_count_facultyfeedback IS NULL
THEN SET current_count_facultyfeedback = 0;
END IF;
WHILE temp_index > current_count_facultyfeedback DO
SET gpa = ((round( abs( (rand( ) *100 ) )) %5)*2)+2.12);
IF gpa > 10.0
THEN SET gpa = 8.12;
END IF;
INSERT INTO tbl2 VALUES
('B.Tech',
'CSE',
current_emp_id,
'0',
current_subject_id,
'MONSOON',
"2012",
(round( abs( (rand( ) *100 ) )) %5 +1),
'',
gpa);
SET temp_index = temp_index - 1;
END WHILE;
END LOOP;
CLOSE example_cursor;
END $$
DELIMITER ;
尽管我付出了最大的努力,它仍然给我以下错误:
1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
');
IF gpa > 10.0
THEN SET gpa = 8.12;
END IF;'
我最早需要完成这个,但似乎无法弄清楚出了什么问题。 提前谢谢。
答案 0 :(得分:1)
检查此行:
SET gpa = ((round( abs( (rand( ) *100 ) )) %5)*2)+2.12);
您有6个左括号和7个结束括号,导致语法错误!
删除最后一个关闭的,因为它似乎不是必需的
SET gpa = ((round( abs( (rand( ) *100 ) )) %5)*2)+2.12;