Unix Technical Forum

Update but insert if not exist

This is a discussion on Update but insert if not exist within the MySQL General forum forums, part of the MySQL category; --> I am trying to think of a trick way to handle something. I have been successful in using the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:02 AM
Steffan A. Cline
 
Posts: n/a
Default Update but insert if not exist

I am trying to think of a trick way to handle something. I have been
successful in using the multiple inserts in one row by using the
,(xx,xx,xx),(xx,xx,xx),(xx,xx,xx) in a values block. BUT Is it possible to
insert if not there for example


Update xxxx
if anyone not found then insert new with same criteria as update
Where region_id in (2,3,4,5,6)



Thanks

Steffan

---------------------------------------------------------------
T E L 6 0 2 . 7 9 3 . 0 0 1 4 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
Steffan A. Cline
Steffan@ExecuChoice.net Phoenix, Az
http://www.ExecuChoice.net USA
AIM : SteffanC ICQ : 57234309
YAHOO : Steffan_Cline MSN : steffan@hldns.com
GOOGLE: Steffan.Cline Lasso Partner Alliance Member
---------------------------------------------------------------



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:02 AM
Afan Pasalic
 
Posts: n/a
Default Re: Update but insert if not exist

try REPLACE
http://dev.mysql.com/doc/refman/5.1/en/replace.html

-afan

Steffan A. Cline wrote:
> I am trying to think of a trick way to handle something. I have been
> successful in using the multiple inserts in one row by using the
> ,(xx,xx,xx),(xx,xx,xx),(xx,xx,xx) in a values block. BUT Is it possible to
> insert if not there for example
>
>
> Update xxxx
> if anyone not found then insert new with same criteria as update
> Where region_id in (2,3,4,5,6)
>
>
>
> Thanks
>
> Steffan
>
> ---------------------------------------------------------------
> T E L 6 0 2 . 7 9 3 . 0 0 1 4 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
> Steffan A. Cline
> Steffan@ExecuChoice.net Phoenix, Az
> http://www.ExecuChoice.net USA
> AIM : SteffanC ICQ : 57234309
> YAHOO : Steffan_Cline MSN : steffan@hldns.com
> GOOGLE: Steffan.Cline Lasso Partner Alliance Member
> ---------------------------------------------------------------
>
>
>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:02 AM
Rob Wultsch
 
Posts: n/a
Default Re: Update but insert if not exist

On Dec 16, 2007 10:29 AM, Afan Pasalic <afan@afan.net> wrote:
> try REPLACE
> http://dev.mysql.com/doc/refman/5.1/en/replace.html
>
> -afan

Replace will not do an update. All previous data will be lost.

A mysql-centric alternative solution would be to use INSERT ... ON
DUPLICATE KEY UPDATE
http://dev.mysql.com/doc/refman/5.0/...duplicate.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:02 AM
J Trahair
 
Posts: n/a
Default Re: Update but insert if not exist

This is a question I want to know the answer to, as well! Is there any way of avoiding looking up a specific record in a table to see if it exists, before deciding whether to INSERT INTO or UPDATE, eg:

mstrSQL = "SELECT * FROM Shops WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
Set rsRecordset = New ADODB.Recordset
gconn.CursorLocation = adUseServer
rsRecordset.Open mstrSQL, gconn, adOpenDynamic, adLockOptimistic
If rsRecordset.EOF = True Then
mstrSQL = "INSERT INTO Shops (ShopNameInFull, ShopReference, TillNumber) VALUES (strShopNameInFull, strShopReference, strTillNumber)"
mconn.Execute mstrSQL
Else
mstrSQL = "UPDATE Shops SET ShopNameInFull = 'strShopNameInFull', ShopReference = 'strShopReference', TillNumber = 'strTillNumber' WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
mconn.Execute mstrSQL
End If


.....just thought I'd ask!

Jonathan Trahair

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 07:02 AM
Jay Pipes
 
Posts: n/a
Default Re: Update but insert if not exist

INSERT ... ON DUPLICATE KEY UPDATE:

http://dev.mysql.com/doc/refman/5.0/...duplicate.html

Cheers,

Jay

J Trahair wrote:
> This is a question I want to know the answer to, as well! Is there any way of avoiding looking up a specific record in a table to see if it exists, before deciding whether to INSERT INTO or UPDATE, eg:
>
> mstrSQL = "SELECT * FROM Shops WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
> Set rsRecordset = New ADODB.Recordset
> gconn.CursorLocation = adUseServer
> rsRecordset.Open mstrSQL, gconn, adOpenDynamic, adLockOptimistic
> If rsRecordset.EOF = True Then
> mstrSQL = "INSERT INTO Shops (ShopNameInFull, ShopReference, TillNumber) VALUES (strShopNameInFull, strShopReference, strTillNumber)"
> mconn.Execute mstrSQL
> Else
> mstrSQL = "UPDATE Shops SET ShopNameInFull = 'strShopNameInFull', ShopReference = 'strShopReference', TillNumber = 'strTillNumber' WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
> mconn.Execute mstrSQL
> End If
>
>
> ....just thought I'd ask!
>
> Jonathan Trahair
>

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:34 AM.


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