MySQL Forums
Forum List  »  InnoDB

Conditional Insert Statement
Posted by: Edward Chong
Date: July 15, 2008 10:46AM

I need to perform an bunch of insert queries on a database only if one of the tables does not contain a certain entry. However I keep getting an error on the line: 'IF @my_count = 0 THEN'. Can someone give me a hand?
For example:

Table a
IDa as int
GUIDa as char

Table b
IDa as int
GUIDb as char

Table c
IDc as int
GUIDc as char
GUIDb as char

Insert Statement:

SET @my_count := 0;
SELECT @my_count:=COUNT(*) FROM b WHERE GUIDb='5cc9dc86-a334-102b-94d8-00096bb57d48';
IF @my_count = 0 THEN
INSERT INTO a (`IDa`, `GUIDa`)
SELECT NULL, '0bce4306-87bb-102b-a077-00142209493c' WHERE NOT EXISTS (SELECT GUID FROM a WHERE GUIDa='0bce4306-87bb-102b-a077-00142209493c');

INSERT INTO b (`IDb`, `GUIDb`)
SELECT NULL, '5cc9dc86-a334-102b-94d8-00096bb57d48' WHERE NOT EXISTS (SELECT GUID FROM b WHERE GUIDb='5cc9dc86-a334-102b-94d8-00096bb57d48');

INSERT INTO c (`IDc`, `GUIDc` `GUIDc`) VALUES
(NULL, '9934b2d6-a334-102b-94d8-00096bb57d48','5cc9dc86-a334-102b-94d8-00096bb57d48'),
(NULL, '9934c618-a334-102b-94d8-00096bb57d48','5cc9dc86-a334-102b-94d8-00096bb57d48'),

END IF

Options: ReplyQuote


Subject
Views
Written By
Posted
Conditional Insert Statement
4030
July 15, 2008 10:46AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.