在下面的代码中,我声明了一个游标,然后在创建一个表后不久。然后循环并向表中添加值:
DELIMITER //
DROP PROCEDURE IF EXISTS studentinfo//
CREATE PROCEDURE studentinfo()
BEGIN
-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE studentNum INT(2);
DECLARE gradepoint DECIMAL(3,2);
DECLARE lastName VarChar(15);
DECLARE classYear INT(1);
-- Declare the cursor
DECLARE studentNumber CURSOR
FOR
SELECT Student_number FROM student;
-- Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
-- Create a table to store the results
**CREATE TABLE IF NOT EXISTS studentdata(gpa DECIMAL(3,2), LName VarChar(15),class INT(1));**
-- Open the cursor
OPEN studentNumber;
-- Loop through all rows
REPEAT
-- Get student number
FETCH studentNumber INTO studentNum;
-- Get gpa
Select student_gpa(studentNum) INTO gradepoint;
-- Get name
Select LName from student WHERE Student_number = studentNum INTO lastName;
-- get grade level
Select Class from student WHERE Student_number = studentNum INTO classYear;
-- Insert info into table
**INSERT INTO studentdata(gpa,Lname,class)
VALUES(gradepoint, lastName, classYear);**
-- End of loop
UNTIL done END REPEAT;
-- Close the cursor
CLOSE studentNumber;
END//
DELIMITER ;
当我然后从studentdata运行Select *时,它给了我一个表不存在'错误。我可以通过运行创建表的一行代码手动创建表,但如果我运行整个代码块,它将不会创建它。即使我手动创建它,它仍然不会用任何值填充它。我在这做错了什么?感谢。