INSERT or UPDATE

  • Thread starter Thread starter pxpilot
  • Start date Start date
P

pxpilot

Hi All,
I have the a table with 2 fields: uid, date.
When the user does some activity I log his uid and the date, the
problem I have is that the table is not prepoulated with all uid's so
sometimes I need to INSERT and other times I need to UPDATE (if that
uid was already logged). Checking first with SELECT to see if the uid
exist is the obvious thing but I wonder is there a more effective way?

App is ASP.NET 2.0 in VB

Thanks
PxP
 
but I wonder is there a more effective way?

I don't believe so...

So long as the id is (part of) the primary key, the first check should be
virtually instantaneous...
 
I don't believe so...

So long as the id is (part of) the primary key, the first check should be
virtually instantaneous...

--http://www.markrae.net

Is there some sort of user registration process? If so, modify it to
insert a new record into your table. Then the registration process can
always do the insert, and the other code can always do updates.
 
Hello pxpilot,

It depends on your DB, some of them support MERGE (Oracle for example), which
call either insert or update.
for SQL server you need to implement your SP, there is no merge afaik


---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

p> Hi All,
p> I have the a table with 2 fields: uid, date.
p> When the user does some activity I log his uid and the date, the
p> problem I have is that the table is not prepoulated with all uid's so
p> sometimes I need to INSERT and other times I need to UPDATE (if that
p> uid was already logged). Checking first with SELECT to see if the uid
p> exist is the obvious thing but I wonder is there a more effective
p> way?
p> App is ASP.NET 2.0 in VB
p>
p> Thanks
p> Px
 
Hi All,
I have the a table with 2 fields: uid, date.
When the user does some activity I log his uid and the date, the
problem I have is that the table is not prepoulated with all uid's so
sometimes I need to INSERT and other times I need to UPDATE (if that
uid was already logged). Checking first with SELECT to see if the uid
exist is the obvious thing but I wonder is there a more effective way?
App is ASP.NET 2.0 in VB

Thanks
PxP

You could start with the UPDATE and check how many rows were updated.
If 0, then use the INSERT.
This way you execute 1 or 2 statements, instead of always 2 for the
SELECT-route.

Hans Kestin
 
Back
Top