How to check in a stored proc if data is already present?

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 ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-23-2007
Bruno Panetta
 
Posts: n/a
Default How to check in a stored proc if data is already present?

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.
Reply With Quote
  #2 (permalink)  
Old 12-23-2007
Ana C. Dent
 
Posts: n/a
Default Re: How to check in a stored proc if data is already present?

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:30 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0