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. SQL Server Insert

SQL Server Insert

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
5 Posts 3 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.
  • C Offline
    C Offline
    clint1982
    wrote on last edited by
    #1

    Hi I want to insert into a table information and if the value exists it shouldn't insert but also not throw an error message. Is there any option to the insert statement ? It is in SQL Server. Thanks, Clint

    C M 2 Replies Last reply
    0
    • C clint1982

      Hi I want to insert into a table information and if the value exists it shouldn't insert but also not throw an error message. Is there any option to the insert statement ? It is in SQL Server. Thanks, Clint

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      BEGIN TRANSACTION
      IF NOT EXISTS (SELECT * FROM SomeTable WHERE SomeColumn = @SomeValue)
      BEGIN
      INSERT ....
      END
      END TRANSACTION


      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      C 1 Reply Last reply
      0
      • C Colin Angus Mackay

        BEGIN TRANSACTION
        IF NOT EXISTS (SELECT * FROM SomeTable WHERE SomeColumn = @SomeValue)
        BEGIN
        INSERT ....
        END
        END TRANSACTION


        Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        C Offline
        C Offline
        clint1982
        wrote on last edited by
        #3

        Is there anyway to avoid two passes over the table ?

        C 1 Reply Last reply
        0
        • C clint1982

          Is there anyway to avoid two passes over the table ?

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Yes, but you'll have to catch the exception. The overhead for the exception is probably greater than the overhead for passing over the table twice in a single operation.


          Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          1 Reply Last reply
          0
          • C clint1982

            Hi I want to insert into a table information and if the value exists it shouldn't insert but also not throw an error message. Is there any option to the insert statement ? It is in SQL Server. Thanks, Clint

            M Offline
            M Offline
            Marc Soleda
            wrote on last edited by
            #5

            Are you doing from Sql Server itself orfrom a client application? If it's the case wich is the programming language you are using? Marc.

            ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

            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