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. how to avoid data redundancy in insert query?

how to avoid data redundancy in insert query?

Scheduled Pinned Locked Moved Database
databasesharepointhelptutorialquestion
7 Posts 5 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.
  • K Offline
    K Offline
    kssknov
    wrote on last edited by
    #1

    hi all i am a beginner, i have written an insert stored procedure. i need my SP to check for the ID field whether the data is already present. pls help ,me. ALTER PROCEDURE [dbo].[SP_SaveGroupClassification] @SubGroupName nvarchar(50), --@GroupId int, @ParentId int, @ClassType nchar(2), @RootId int as begin INSERT INTO GroupClassification (GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) end return Here i need to check the value in (@SubGroupName not to be repeated.it is my key field. thank u

    senthil

    T S C S 4 Replies Last reply
    0
    • K kssknov

      hi all i am a beginner, i have written an insert stored procedure. i need my SP to check for the ID field whether the data is already present. pls help ,me. ALTER PROCEDURE [dbo].[SP_SaveGroupClassification] @SubGroupName nvarchar(50), --@GroupId int, @ParentId int, @ClassType nchar(2), @RootId int as begin INSERT INTO GroupClassification (GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) end return Here i need to check the value in (@SubGroupName not to be repeated.it is my key field. thank u

      senthil

      T Offline
      T Offline
      Tobias Schoenig
      wrote on last edited by
      #2

      Hi, what you could do is try to get the ID for the specified @SubGroupName. If the ID does not exisit, you can insert the new row. DECLARE @ID int SET @ID = (SELECT ID FROM GroupClassification WHERE GroupName = @SubGroupName ORDER BY ID DESC) IF(@ID IS NULL) BEGIN INSERT INTO GroupClassification(GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) END

      K 1 Reply Last reply
      0
      • K kssknov

        hi all i am a beginner, i have written an insert stored procedure. i need my SP to check for the ID field whether the data is already present. pls help ,me. ALTER PROCEDURE [dbo].[SP_SaveGroupClassification] @SubGroupName nvarchar(50), --@GroupId int, @ParentId int, @ClassType nchar(2), @RootId int as begin INSERT INTO GroupClassification (GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) end return Here i need to check the value in (@SubGroupName not to be repeated.it is my key field. thank u

        senthil

        S Offline
        S Offline
        SimulationofSai
        wrote on last edited by
        #3

        Use IF NOT EXISTS IF NOT EXISTS(Select a from table where x=??) INSERT INTO...

        SG Cause is effect concealed. Effect is cause revealed.

        1 Reply Last reply
        0
        • K kssknov

          hi all i am a beginner, i have written an insert stored procedure. i need my SP to check for the ID field whether the data is already present. pls help ,me. ALTER PROCEDURE [dbo].[SP_SaveGroupClassification] @SubGroupName nvarchar(50), --@GroupId int, @ParentId int, @ClassType nchar(2), @RootId int as begin INSERT INTO GroupClassification (GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) end return Here i need to check the value in (@SubGroupName not to be repeated.it is my key field. thank u

          senthil

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

          kssknov wrote:

          Here i need to check the value in (@SubGroupName not to be repeated.it is my key field.

          You should declare it as your PRIMARY KEY. As soon as you do that the database will do the work for you by ensuring that it remains unique and is not repeated.


          Upcoming FREE developer events: * Developer Day Scotland My website | blog

          1 Reply Last reply
          0
          • K kssknov

            hi all i am a beginner, i have written an insert stored procedure. i need my SP to check for the ID field whether the data is already present. pls help ,me. ALTER PROCEDURE [dbo].[SP_SaveGroupClassification] @SubGroupName nvarchar(50), --@GroupId int, @ParentId int, @ClassType nchar(2), @RootId int as begin INSERT INTO GroupClassification (GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) end return Here i need to check the value in (@SubGroupName not to be repeated.it is my key field. thank u

            senthil

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            The field should either be a primary key or, or should be associated with a unique index. If a unique index exists for a field (or if it's a primary key, which is effectively a special type of unique index) then any attempt to add a record that already exists is guaranteed to fail. If two attempts are made simultaneously to add the same record, only one will succeed. In that regard, this approach is much better than the approach others here suggested of querying first to see if the record exists (two clients could both check for a record and discover it doesn't yet exist, and then both add it). There are few stylistic issues to consider: -1- If the record already exists, checking before doing the insert will avoid having a thrown exception except in the case where two clients attempt to add the same value simultaneously. Code must be prepared to handle the exception even if it checks before the insert, but avoiding the insert will probably be faster than attempting it and having it fail -2- If the record does not exist, checking the database for it will take a little time; adding the record without pre-checking would probably be faster in that case. -3- Some databases allow nulls in a unique index; some do not. Microsoft allows exactly one null to appear in (adding a second item with a null value would violate the unique index constraint). This scenario is applicable to cases where, e.g., any number of workers might have no Social Security Number, but no SSN may be shared among workers. If the database does not allow nulls in the field, one may have to store an arbitrary value instead. If there's a unique positive integer record number available, one could set the SSN field to be negative that (e.g. for record 1,234 store -1234 in the SSN). One would have to recognize and handle those numbers when viewing the data, but there would be no problem handling multiple SSN-less employees.

            K 1 Reply Last reply
            0
            • T Tobias Schoenig

              Hi, what you could do is try to get the ID for the specified @SubGroupName. If the ID does not exisit, you can insert the new row. DECLARE @ID int SET @ID = (SELECT ID FROM GroupClassification WHERE GroupName = @SubGroupName ORDER BY ID DESC) IF(@ID IS NULL) BEGIN INSERT INTO GroupClassification(GroupName, ParentId, ClassType, RootId) VALUES (@SubGroupName,@ParentId,@ClassType,@RootId) END

              K Offline
              K Offline
              kssknov
              wrote on last edited by
              #6

              hi, it is working Thank u Tobias Schoenig.

              senthil

              1 Reply Last reply
              0
              • S supercat9

                The field should either be a primary key or, or should be associated with a unique index. If a unique index exists for a field (or if it's a primary key, which is effectively a special type of unique index) then any attempt to add a record that already exists is guaranteed to fail. If two attempts are made simultaneously to add the same record, only one will succeed. In that regard, this approach is much better than the approach others here suggested of querying first to see if the record exists (two clients could both check for a record and discover it doesn't yet exist, and then both add it). There are few stylistic issues to consider: -1- If the record already exists, checking before doing the insert will avoid having a thrown exception except in the case where two clients attempt to add the same value simultaneously. Code must be prepared to handle the exception even if it checks before the insert, but avoiding the insert will probably be faster than attempting it and having it fail -2- If the record does not exist, checking the database for it will take a little time; adding the record without pre-checking would probably be faster in that case. -3- Some databases allow nulls in a unique index; some do not. Microsoft allows exactly one null to appear in (adding a second item with a null value would violate the unique index constraint). This scenario is applicable to cases where, e.g., any number of workers might have no Social Security Number, but no SSN may be shared among workers. If the database does not allow nulls in the field, one may have to store an arbitrary value instead. If there's a unique positive integer record number available, one could set the SSN field to be negative that (e.g. for record 1,234 store -1234 in the SSN). One would have to recognize and handle those numbers when viewing the data, but there would be no problem handling multiple SSN-less employees.

                K Offline
                K Offline
                kssknov
                wrote on last edited by
                #7

                hi thank u for ur reply. As u said i had other ways of doing the same.but the field i need to check is not a primary key field,also i cant set it as PK since other id field is already set to PK. thank u very much kssk

                senthil

                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