This is a discussion on How to check in a stored proc if data is already present? within the MySQL Database forums, part of the Database Forums category; I have a stored proc like the following. It works on a table called ProductType with only one field, called ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a stored proc like the following. It works on a table called
ProductType with only one field, called ProductTypeName. DROP PROCEDURE IF EXISTS myDB.setProductType; CREATE PROCEDURE myDB.`setProductType`( IN ProductType VARCHAR(20)) BEGIN INSERT INTO ProductType ( ProductTypeName ) VALUES ( ProductTypeName ); END; It works fine, but I would like to avoid duplicates. How can I make the stored proc check if that ProductTypeName is already in the table, and only insert it if it's not? Thanks. |
|
|||
|
Bruno Panetta <bpanetta@gmail.com> wrote in news:9ffbff93-5518-414e-a8dd-
5f8f3a8b3298@b40g2000prf.googlegroups.com: > I have a stored proc like the following. It works on a table called > ProductType with only one field, called ProductTypeName. > > DROP PROCEDURE IF EXISTS myDB.setProductType; > CREATE PROCEDURE myDB.`setProductType`( > > IN ProductType VARCHAR(20)) > BEGIN > INSERT INTO ProductType > ( > ProductTypeName > ) > VALUES > ( > ProductTypeName ); > END; > > It works fine, but I would like to avoid duplicates. How can I make > the stored proc check if that ProductTypeName is already in the table, > and only insert it if it's not? Thanks. > Place a UNIQUE index on that column which will preclude duplicates. |