Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Update+Increment else Insert

Update+Increment else Insert

Scheduled Pinned Locked Moved Database
databasecsharpasp-netannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael P Scherer
    wrote on last edited by
    #1

    I'm not much of one with SQL, and I'm not sure if this is possible, but it seemed to me that it would be better to do it this way if it is than to split it into several queries. I'm making a page usage site for my ASP.NET page, and I want to store statistics for what browser is being used, which OS, etc. So I have a table with 2 fields: BrowserName and Sessions. I want to make a query that if the BrowserName exists, then it increments Sessions, otherwise it adds the BrowserName and sets Sessions to 1. Switching between Update/Insert in particular has left me clueless. Thanks in advance!

    SoCoder - CAT Program

    H 1 Reply Last reply
    0
    • M Michael P Scherer

      I'm not much of one with SQL, and I'm not sure if this is possible, but it seemed to me that it would be better to do it this way if it is than to split it into several queries. I'm making a page usage site for my ASP.NET page, and I want to store statistics for what browser is being used, which OS, etc. So I have a table with 2 fields: BrowserName and Sessions. I want to make a query that if the BrowserName exists, then it increments Sessions, otherwise it adds the BrowserName and sets Sessions to 1. Switching between Update/Insert in particular has left me clueless. Thanks in advance!

      SoCoder - CAT Program

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      Hello, this is a sample stored procedure that does what you need, I assume that you use MS SQL This is just one way of doing it.

      create proc InsertOrUpdateCount(@Browsername varchar)
      as
      update tablename
      Set Sessions=session+1
      where BrowserName = @Browsername

      if @@rowcount=0 --no updates so the record does not exist
      insert into tablename (BrowserName ,Sessions) values (@Browsername,1)

      Hesham A. Amin My blog

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups