stored procedure to avoid data redundancy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssknov
    New Member
    • Dec 2007
    • 40

    stored procedure to avoid data redundancy

    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_SaveGroupCla ssification]
    @SubGroupName nvarchar(50),
    --@GroupId int,
    @ParentId int,
    @ClassType nchar(2),
    @RootId int
    as
    begin
    INSERT INTO GroupClassifica tion
    (GroupName, ParentId, ClassType, RootId)
    VALUES (@SubGroupName, @ParentId,@Clas sType,@RootId)
    end
    return

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

    thank u
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    You need to check for existance of the record before inserting into the table using COUNT. Insert into the table only count is 0.

    Comment

    • Jim Doherty
      Recognized Expert Contributor
      • Aug 2007
      • 897

      #3
      Originally posted by ssknov
      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_SaveGroupCla ssification]
      @SubGroupName nvarchar(50),
      --@GroupId int,
      @ParentId int,
      @ClassType nchar(2),
      @RootId int
      as
      begin
      INSERT INTO GroupClassifica tion
      (GroupName, ParentId, ClassType, RootId)
      VALUES (@SubGroupName, @ParentId,@Clas sType,@RootId)
      end
      return

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

      thank u
      Look at (SQL Books online plenty of references) the keyword EXISTS to test for the existence of the value before insertion and if not true only then insert. EXISTS will stop at the first value it finds rather than performing an entire table scan.

      Jim :)

      Comment

      Working...