Stored Procedure issue - mysql

I am new to sql and most of my training is in MSSQL, however I am taking a class in MYSQL. I have a stored procedure that is giving me an issue. I have the delimiter set to //.
CREATE PROCEDURE AddComment
(
name varchar(50),
emailAddress varchar(50),
Comments text
)
BEGIN
DECLARE ComName varchar(50);
DECLARE ComID int;
SELECT name into ComName;
SELECT ID into ComID from Commenters WHERE names = ComName;
if ComID = NULL
BEGIN
INSERT INTO COMMENTERS ('names', 'emailAddresses') values (name, emailAddress);
SELECT ID into ComID from Commenters WHERE names = ComName;
END
INSERT INTO COMMENTS ('commentersID', 'Comments') values (ComID, Comments);
END;
//
EDIT
This is what I have now:
CREATE PROCEDURE AddComment
(
Username varchar(50),
UseremailAddress varchar(50),
UserComment text
)
BEGIN
DECLARE ComName varchar(50);
DECLARE ComID int;
SELECT Username into ComName;
SELECT ID into ComID from Commenters WHERE names = ComName;
if ComID = NULL
BEGIN
INSERT INTO COMMENTERS(names,emailAddresses) values (Username, UseremailAddress);
SELECT ID into ComID from Commenters WHERE names = ComName;
END
INSERT INTO COMMENTS(commentersID, Comments) values (ComID, UserComment);
END;
//
And the error I am getting is:
ERROR 1064 (42000): 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 'BEGIN INSERT INT
O COMMENTERS(names, emailAddresses) values (Username, Useremail' at line 1
Any Ideas?

You are coding column names and values reversed in you insert statements.
It should be
INSERT INTO COMMENTERS (name, emailAddress) values ('names', 'emailAddresses') ;

Related

What is wrong with my mysql stored procedure?

I have a stored procedure that checks whether or not a new entry is already existing in the table. if it exists, the insert will not happen. when I run it, there is an error
DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$
CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;
IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END
here is the error
Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8
1) You cannot declare a select statement - a declare has to be for a variable..(and I would not use an output parameter for that) 2) or you can use exists instead
if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;

How to create MySQL script using MySQL workbench?

I followed this tutorial but it seems like the queries he used in the video cannot be used in 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
What should be the right syntax for this?
After a year of studying some basics in creating a stored procedure in MySQL, I created my own MySQL version of the above question in like this.
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

return mysql error msg from function for insert statement

I have a following function in MySQL.
How can I make the function to return the GUID when a row gets inserted successfully in it, and the MySQL error message, if something happens wrong with `insert'.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `fnInsertBusinessInfo`(
BU_NAME VARCHAR(300),
BU_PHONE VARCHAR(20),
BU_TYPE VARCHAR(50)
) RETURNS varchar(100) CHARSET latin1
BEGIN
DECLARE
BU_ID VARCHAR(100);
SET BU_ID := (SELECT UUID());
INSERT INTO tb_business_info (BU_ID,BU_NAME,BU_PHONE,BU_TYPE) values (BU_ID,BU_NAME,BU_PHONE,BU_TYPE);
RETURN BU_ID;
END
I have manage to return id but how can I return the error message on an unsuccessful insert?
DECLARE
BU_ID VARCHAR(100);
--modify SET BU_ID := (SELECT UUID());
SELECT UUID() into BU_ID;
INSERT INTO tb_business_info (BU_ID,BU_NAME,BU_PHONE,BU_TYPE) values (BU_ID,BU_NAME,BU_PHONE,BU_TYPE);
RETURN BU_ID;
END

MySQL StoredProcedure Variable set from select with parameters as conditions

So I'm trying to get the PatientID (primary key, not null, autoincrement) from a patient that I added to the database earlier within a stored procedure using the parameters for FName and LName passed in in my select statement to set the variable, but I'm getting a syntax error. So any help as to why the syntax is failing would be appreciated.
Here's the relevant code, pFName and pLName are parameters passed into the stored procedure.
DECLARE pPolicyHolder INT;
SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND LName = pLName);
And here's the entire procedure. I realize it's probably not the best/cleanest way to do this, but the way the tables are set up is that the MedicalHistory needs to be added before the Patient can be added, and the Patient must be added before the PatientInsurance can be added because of Foreign Key constraints.
DELIMITER //
CREATE PROCEDURE AddNewPatient(
IN pAllergies TEXT, IN pMedications TEXT, IN pExistingConditions TEXT, IN pMisc
TEXT, IN pFName VARCHAR(30), IN pLName VARCHAR(45), IN pGender CHAR(1), IN pDOB
DATE, IN pSSN DOUBLE, IN pMedicalHistory INT, IN pPrimaryPhysician INT, IN
pInsuranceCompany INT, IN pCoPay INT)
BEGIN
START TRANSACTION;
INSERT INTO MedicalHistory(Allergies, Medications, ExistingConditions, Misc)
VALUES(pAllergies, pMedications, pExistingConditions, pMisc);
COMMIT;
START TRANSACTION;
INSERT INTO Patient(FName, LName, Gender, DOB, SSN,
MedicalHistory,PrimaryPhysician) VALUES(pFName,
LName,pGender,pDOB,pSSN,pMedicalHistory,pPrimaryPhysician);
COMMIT;
DECLARE pPolicyHolder INT;
SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND
LName = pLName);
START TRANSACTION;
INSERT INTO PatientInsurance(PolicyHolder, InsuranceCompany, CoPay)
VALUES(pPolicyHolder, pInsuranceCompany, pCoPay);
COMMIT;
END //
DELIMITER ;
The error:
ERROR 1064 (42000): 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 'DECLARE pPolicyHolder INT;
SELECT PatientID into pPolicyHolder FROM Patient W' at line 15
You need to declare variables at the beginning of your procedure, not in the middle.
CREATE PROCEDURE AddNewPatient(...)
BEGIN
DECLARE pPolicyHolder INT; <------declare here
START TRANSACTION;
...
COMMIT;
SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND
LName = pLName);
...

Unable to CREATE FUNCTION in MYSQL ERROR 1064 (42000)

Hi I'm using MySQL to create a function:
CREATE FUNCTION INSERTGROUP(name VARCHAR(50))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE 'idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO 'idGroup' FROM groupsTable WHERE groupName='name';
RETURN 'idGroup';
END//
But I get this error when I try to create it:
ERROR 1064 (42000): 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
''idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM serverGroupsTable WHERE gro' at line 5
I've been through forums and other questions similar to this one but I can not make it create the function.
What I'm doing wrong? Is the syntax correct? Do I need to add something else?
Ok this is what I've try:
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = name))
THEN INSERT INTO groupsTable (groupName) VALUES (name);
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName=name;
RETURN idGroup;
END//
and
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName='name';
RETURN idGroup;
END//
and for the last two I got this error:
ERROR 1064 (42000): 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
'' at line 13
Similar to the one above
Also I do DELIMITER // to change it from ;
remove the quotes from the declaration
DECLARE idGroup INT;
and you forgot the delimiter change at the start of the function
delimiter //
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
and you forgot to end your if statement
end if;
whereever you want it to end