Click here to Skip to main content
12,818,315 members (34,258 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 11 Jul 2010

Combining Insert/Update to one Procedure

, 14 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Why do developers insist on separate procedures to do these jobs
I can never understand why developers have both an Insert and an Update procedure, they do basically the same thing. If you combine them, you reduce the CRUD procedures by 25%, your DAL has 1 less method and management is simpler.

By simply managing the ID field(s), you can combine these operations into 1 procedure.
ALTER PROC [APExclusionUpdate]
  @ExclID INT,
  @EntityID INT,
  @AttrTypeID INT,
  @APLinkID INT,
  @Modified DATETIME,
  @ModifiedBy VARCHAR(100)

-------------------- Created by Classbuilder -----------

IF ISNULL(@ExclID, 0) = 0 -- decide to Insert based on ID
    INSERT  INTO APExclusion
      SCOPE_IDENTITY() AS ExclID -- return the new ID value
      EntityID = @EntityID,
      AttrTypeID = @AttrTypeID,
      APLinkID = @APLinkID,
      Modified = @Modified,
      ModifiedBy = @ModifiedBy
      ExclID = @ExclID
      @ExclID AS ExclID -- return the passed in ID value

This procedure decides whether to insert or update the record based on the ID fields. You can either return the ID or the new/updated record. I generally return the ID.

I have used this with both identity and GUID primary keys, GUID requires a bit more management.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mycroft Holmes
Software Developer (Senior) Contractor
Singapore Singapore
Started my programming life writing Excel 1.0 macros, God what a long time ago.

Now I'm a dotnet developer, I get to influence direction, play with new toys, build stuff, life is wonderful.

Greatest buzz you can get, walk past a row of desks and see your application running on all of them (and getting paid).

Greatest irritant, pouring 12 months of knowledge and experience into an empty head only to have it leave.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Classic solution ...thnx bro Pin
Ravindra_Parcha23-Jan-12 8:20
memberRavindra_Parcha23-Jan-12 8:20 
GeneralFor those of you that "don't get it", you should read Spaanj... Pin
EngleA12-Apr-11 8:54
memberEngleA12-Apr-11 8:54 
GeneralReason for my vote of 2 I don't get it. You wouldn't combine... Pin
dmjm-h12-Apr-11 7:05
memberdmjm-h12-Apr-11 7:05 
GeneralRe: What makes you think an update and an insert have different ... Pin
Mycroft Holmes12-Apr-11 13:12
memberMycroft Holmes12-Apr-11 13:12 
GeneralI don't have any reason for separating the 2 methods In the ... Pin
Mycroft Holmes4-Apr-11 20:10
memberMycroft Holmes4-Apr-11 20:10 
GeneralRe: you can still do that in one procedure. Instead of SELECT S... Pin
EngleA12-Apr-11 8:53
memberEngleA12-Apr-11 8:53 
GeneralReason for my vote of 3 - Multiple interpretation of the sam... Pin
Jani Giannoudis4-Apr-11 19:14
mvpJani Giannoudis4-Apr-11 19:14 
GeneralReason for my vote of 3 Reduces the numbers of SP on server ... Pin
Sunnyonfire4-Apr-11 8:51
memberSunnyonfire4-Apr-11 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 14 Jul 2010
Article Copyright 2010 by Mycroft Holmes
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid