[英]How to create MySQL script using MySQL workbench?
我遵循了本教程,但似乎他在視頻中使用的查詢無法在MySQL中使用。
CREATE PROCEDURE spInsertIntoStudentCourses
@StudentName varchar(50),
@CourseName varchar(50)
AS
BEGIN
DECLARE @StudentId int;
DECLARE @CourseId int;
SELECT @StudentId = id from Students where StudentName = @StudentName;
if(@StudentId is null);
Begin;
INSERT INTO Student VALUES (@StudentName);
SELECT @StudentId = SCOPE_IDENTITY();
END;
SELECT @CourseId = id from Courses where CourseName = @CourseName;
if(@CourseName is null)
BEGIN;
INSERT INTO Courses VALUES (@CourseName);
SELECT @CourseId = SCOPE_IDENTITY();
END;
INSERT INTO StudentCourses VALUES (@StudentID, @CourseID);
END
正確的語法應該是什么?
在研究了在MySQL中創建存儲過程的一些基礎知識之后,我像這樣創建了上述問題的MySQL版本。
CREATE PROCEDURE `InsertStudentCourse`(
_studentName VARCHAR(50),
_courseName VARCHAR(50)
)
BEGIN
DECLARE _studentId INT;
DECLARE _courseId INT;
SELECT _studentId = studentId FROM students WHERE studentName = _studentName;
IF (_studentId IS NULL) THEN
BEGIN
INSERT INTO students VALUES (NULL, _studentName);
SELECT _studentId = LAST_INSERT_ID();
END;
END IF;
SELECT _courseId = courseId FROM courses WHERE CourseName = _courseName;
IF (_courseId IS NULL) THEN
BEGIN
INSERT INTO courses VALUES (NULL, _courseName);
SELECT _courseId = LAST_INSERT_ID();
END;
END IF;
INSERT INTO student_courses VALUES (NULL, _studentName, _courseName);
END
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.