Unix Technical Forum

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 forums, part of the Database Server Software category; --> I have a stored proc like the following. It works on a table called ProductType with only one field, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:31 AM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:31 AM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:42 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com