0
在下面的代碼中,我聲明瞭一個遊標,然後在創建一個表之後不久。然後,它通過循環,並增加值表:MySQL不會創建表或使用值填充它。
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 *,它給了我一個「表不存在」的錯誤。我可以通過運行創建表的一行代碼手動創建表,但如果我運行整個代碼塊,它將不會創建它。即使我手動創建它,它仍然不會用任何值填充它。我在這裏做錯了什麼?謝謝。
哇,我覺得很愚蠢。謝謝。 – crseat