Reputation: 1954
I just tried to use the IF-ELSE with SELECT
of mySQL. I know there's a syntax error here but can't find it. It's returning Error Code 1241. Operand should contain 1 column(s)
on mySQL Workbench.
I don't see any error or maybe I just can't find it. What could be causing this?
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCurriculum`(
IN p_curcName varchar(100),
IN p_description TEXT,
IN p_yearLevel VARCHAR(50),
IN p_syStart INT,
IN p_syEnd INT,
IN p_creator VARCHAR(50) )
BEGIN
IF
(SELECT name,description,yearLevel,syStart,syEnd
FROM curriculum
WHERE
name = p_curcName
AND description = p_description
AND yearLevel = p_yearLevel
AND syStart = p_syStart
AND syEnd = p_syEnd
AND creator = p_creator )
THEN --
BEGIN
SELECT 'Curriculum you are trying to add already exists';
END;
ELSE
BEGIN
INSERT INTO curriculum(name, description, yearLevel, syStart, syEnd, creator)
VALUES(p_curcName,p_description,p_yearLevel,p_syStart,p_syEnd,p_creator);
END;
END IF;
END
I'd appreciate any help since I'm new to the language. I just started learning Transaction and Procedures.
Thanks.
Upvotes: 0
Views: 119
Reputation: 1954
Okay, so I just have to add EXISTS
IF EXISTS
(SELECT name,description,yearLevel,syStart,syEnd
FROM curriculum
WHERE
name = p_curcName
AND description = p_description
AND yearLevel = p_yearLevel
AND syStart = p_syStart
AND syEnd = p_syEnd
AND creator = p_creator )
I don't know how MERGE
works so I guess I'll have to read more.
Upvotes: 0
Reputation: 17615
I think you are looking for IF EXISTS, but there are a number of ways of upserting (UPdatinSERT) using IF and Merge, lots of them in SO :)
Upvotes: 1
Reputation: 98388
IF
(SELECT name,description,yearLevel,syStart,syEnd
doesn't make sense; it is expecting just one column for the if to test.
Perhaps you want IF (SELECT COUNT(*) FROM ...)
Upvotes: 0