chaseivey
asked on
MySQL conditional INSERT
Hello,
I am trying to do a conditional INSERT based on whether a product # already exists in my table.
Does anyone know how to do this? I have tried several options, including the EXISTS and IF clauses, but it keeps throwing a syntax error. Here's what I've tried so far:
INSERT INTO products ( productNum, price )
VALUES (12345, 100)
WHERE NOT EXISTS ( SELECT * FROM products WHERE productNum=12345 )
and...
IF NOT EXISTS ( SELECT * FROM products WHERE productNum=12345 )
THEN INSERT INTO products ( productNum, price )
VALUES (12345, 100)
END IF
Any ideas?
I am trying to do a conditional INSERT based on whether a product # already exists in my table.
Does anyone know how to do this? I have tried several options, including the EXISTS and IF clauses, but it keeps throwing a syntax error. Here's what I've tried so far:
INSERT INTO products ( productNum, price )
VALUES (12345, 100)
WHERE NOT EXISTS ( SELECT * FROM products WHERE productNum=12345 )
and...
IF NOT EXISTS ( SELECT * FROM products WHERE productNum=12345 )
THEN INSERT INTO products ( productNum, price )
VALUES (12345, 100)
END IF
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER