MySQL 不会创建 table 或用值填充它。
MySQL will not create a table or fill it with values.
在下面的代码中,我声明了一个游标,然后在不久之后创建了一个 table。然后循环并将值添加到 table:
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 ;
然后当我从学生数据中转到 运行 Select * 时,它给了我一个 'table does not exist' 错误。我可以通过 运行 创建 table 的一行代码手动创建 table,但如果我 运行 整个代码块,它就不会创建它。即使我手动创建它,它仍然不会填充任何值。我在这里做错了什么?谢谢
到目前为止,您只定义了一个存储过程。为了在 sp 中创建 table,您必须调用 studentinfo sp。
在下面的代码中,我声明了一个游标,然后在不久之后创建了一个 table。然后循环并将值添加到 table:
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 ;
然后当我从学生数据中转到 运行 Select * 时,它给了我一个 'table does not exist' 错误。我可以通过 运行 创建 table 的一行代码手动创建 table,但如果我 运行 整个代码块,它就不会创建它。即使我手动创建它,它仍然不会填充任何值。我在这里做错了什么?谢谢
到目前为止,您只定义了一个存储过程。为了在 sp 中创建 table,您必须调用 studentinfo sp。