Question : Duplicate entires to be modified in Stored procedure

hi,

We have two SP's which is making duplicate objectives to enter   into the table  bussinessorg review period acesss

columns are  :  Bussinessorgreviewperiod acess_ID (PK)

Business_ID
ReviewPeriod_ID

Aprrsaiallock.


here are two sp's

1)  HRBusiness_Org_Insert



-     2)  BusinessOrgReviewPeriodAccess_INS



HRBussiness_org_insert

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[HRBusiness_Org_Insert]
(
      @Parent_BusinessOrg_ID int,
      @BusinessOrg_Name nvarchar(128),
      @RequestingEmployeeID int
)
AS

DECLARE
      @parentLFT                  int,
      @parentIndentation      int,
      @newBusOrgID            int

if ((select count(businessOrg_id) from businessorg where Name=@BusinessOrg_Name) = 0)
      Begin
            BEGIN TRANSACTION

            -- Insert Bus Org
            SELECT @parentLFT = LFT
            FROM BusinessOrg
            WHERE BusinessOrg_ID = @Parent_BusinessOrg_ID

            SELECT @parentIndentation = Indentation
            FROM BusinessOrg
            WHERE BusinessOrg_ID = @Parent_BusinessOrg_ID

            UPDATE BusinessOrg
            SET RGHT = RGHT + 2
            WHERE RGHT > @parentLFT

            UPDATE BusinessOrg
            SET LFT = LFT + 2
            WHERE LFT > @ParentLFT

            INSERT INTO BusinessOrg
            (Parent_BusinessOrg_ID, Indentation, LFT, RGHT, Active, ImageFileName,
             ImageFilePath, Name, CheckedOut_By, Created_By_EmpID, Created_DateTime)
            VALUES
            (@Parent_BusinessOrg_ID,
             @ParentIndentation + 1,
             @ParentLFT + 1,
             @ParentLFT + 2,
             1,
             NULL,
             './images/',
             @BusinessOrg_Name,
             @RequestingEmployeeID,
             @RequestingEmployeeID,
             getDate())

            IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusinessOrg table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END

            SET @newBusOrgID = IDENT_CURRENT('BusinessOrg')

            -- Create missing scenario records based on the parent org.
            INSERT INTO BusOrgScenarioXref
            (BusinessOrg_ID, Scenario_ID, ReviewPeriod_ID, Created_DateTime, Created_By_EmpID, Updated_DateTime, Updated_By_EmpID)
            SELECT
                  @newBusOrgID,
                  x.Scenario_ID,
                  x.ReviewPeriod_ID,
                  getdate(),
                  @RequestingEmployeeID,
                  getdate(),
                  @RequestingEmployeeID
            FROM
                  BusOrgScenarioXref x
            WHERE
                  x.BusinessOrg_ID = @Parent_BusinessOrg_ID
                  AND NOT EXISTS (
                              SELECT xx.*
                              FROM BusOrgScenarioXref xx
                              WHERE x.Scenario_ID = xx.Scenario_ID
                                AND x.ReviewPeriod_ID = xx.ReviewPeriod_ID
                                AND xx.BusinessOrg_ID = @newBusOrgID
                  )

            IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusOrgScenarioXref table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END

            -- Create missing config records based on the parent org.
            INSERT INTO BusinessOrgConfigOptionXref
            (BusinessOrg_ID, ReviewPeriod_ID, ConfigOption_ID, IsSelected, Created_By_EmpID, Created_DateTime,
             Updated_By_EmpID, Updated_DateTime)
            SELECT
                  @newBusOrgID,
                  x.ReviewPeriod_ID,
                  x.ConfigOption_ID,
                  x.IsSelected,
                  @RequestingEmployeeID,
                  getdate(),
                  @RequestingEmployeeID,
                  getdate()
            FROM
                  BusinessOrgConfigOptionXref x
            WHERE
                  x.BusinessOrg_ID = @Parent_BusinessOrg_ID
                  AND NOT EXISTS (
                              SELECT xx.*
                              FROM BusinessOrgConfigOptionXref xx
                              WHERE x.ReviewPeriod_ID = xx.ReviewPeriod_ID
                                AND x.ConfigOption_ID = xx.ConfigOption_ID
                                AND xx.BusinessOrg_ID = @newBusOrgID
                  )

               IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusinessOrgConfigOptionXref table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END
                  
            -- Create missing config records based on the parent org.
            INSERT INTO BusinessOrgReviewPeriodAccess
            (BusinessOrg_ID, ReviewPeriod_ID, AppraisalsLocked)
            SELECT
                  @newBusOrgID,
                  p.ReviewPeriod_ID,
                  p.AppraisalsLocked
            FROM
                  BusinessOrgReviewPeriodAccess p
            WHERE
                  p.BusinessOrg_ID = @Parent_BusinessOrg_ID
                  AND NOT EXISTS (
                        SELECT pp.*
                        FROM BusinessOrgReviewPeriodAccess pp
                        WHERE p.ReviewPeriod_ID = pp.ReviewPeriod_ID
                          AND p.AppraisalsLocked = pp.AppraisalsLocked
                          AND pp.BusinessOrg_ID = @newBusOrgID
                  )
                  
            IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusinessOrgReviewPeriodAccess table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END
            
            -- Create record for talent plan entitlements based on parent
            INSERT INTO BusOrgMngrLvlRoleXref
            (BusinessOrg_ID, ManagerLevel_ID, AuthorRole_ID, Created_By_Emp_ID, Created_Datetime,
             Updated_By_Emp_ID, Updated_DateTime)
            SELECT
                  @newBusOrgID,
                  p.ManagerLevel_ID,
                  p.AuthorRole_ID,
                  @RequestingEmployeeID,
                  getdate(),
                  @RequestingEmployeeID,
                  getdate()
            FROM
                  BusOrgMngrLvlRoleXref p
            WHERE
                  p.BusinessOrg_ID = @Parent_BusinessOrg_ID
                  AND NOT EXISTS (
                              SELECT pp.*
                              FROM BusOrgMngrLvlRoleXref pp
                              WHERE p.ManagerLevel_ID = pp.ManagerLevel_ID
                                AND p.AuthorRole_ID = pp.AuthorRole_ID
                                AND pp.BusinessOrg_ID = @newBusOrgID
                  )

            IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusOrgMngrLvlRoleXref table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END

            INSERT INTO BusOrgMngrLvlRoleSectionXref
            (BusOrgMngrLvlRoleXref_ID, Section_ID, IsSelected, ColumnPosition, Ordinal,
             Created_By_Emp_ID, Created_DateTime, Updated_By_Emp_ID, Updated_DateTime)
            SELECT
                  c.BusOrgMngrLvlRoleXref_ID,
                  px.Section_ID,
                  px.IsSelected,
                  px.ColumnPosition,
                  px.Ordinal,
                  @RequestingEmployeeID,
                  getdate(),
                  @RequestingEmployeeID,
                  getdate()
            FROM
                  BusOrgMngrLvlRoleXref c
                  INNER JOIN BusOrgMngrLvlRoleXref p ON p.ManagerLevel_ID = c.ManagerLevel_ID
                        AND p.AuthorRole_ID = c.AuthorRole_ID
                        AND p.BusinessOrg_ID = @Parent_BusinessOrg_ID
                        AND c.BusinessOrg_ID = @newBusOrgID
                  INNER JOIN BusOrgMngrLvlRoleSectionXref px ON p.BusOrgMngrLvlRoleXref_ID = px.BusOrgMngrLvlRoleXref_ID

            IF @@ERROR > 0
                  BEGIN
                        RAISERROR 50001 'Error inserting into BusOrgMngrLvlRoleSectionXref table'
                        ROLLBACK TRANSACTION
                        RETURN -1
                  END

            COMMIT TRANSACTION
            
            -- Return new ID.
            SELECT @newBusOrgID

      End
Else
      Begin
            Select -1 as Businessorg_id
      End


2)Bussinessorgreview period _acess



USE [PMTP]
GO
/****** Object:  StoredProcedure [dbo].[BusinessOrgReviewPeriodAccess_INS]    Script Date: 06/30/2010 12:17:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[BusinessOrgReviewPeriodAccess_INS]
(
      @intBusinessOrgID      int
)
AS

INSERT INTO dbo.BusinessOrgReviewPeriodAccess(BusinessOrg_ID, ReviewPeriod_ID, AppraisalsLocked)
SELECT @intBusinessOrgID, ReviewPeriod_ID, 0
FROM ReviewPeriod
WHERE StatusCode <> 'UPCM'



please let me know modifications to over come duplicate objectives

Answer : Duplicate entires to be modified in Stored procedure

I presume you want to prevent the insert if the exact record already exists.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
ALTER PROCEDURE [dbo].[BusinessOrgReviewPeriodAccess_INS]
(
      @intBusinessOrgID      int
)
AS

INSERT INTO dbo.BusinessOrgReviewPeriodAccess(BusinessOrg_ID, ReviewPeriod_ID, AppraisalsLocked)
SELECT @intBusinessOrgID, ReviewPeriod_ID, 0
FROM dbo.ReviewPeriod
WHERE StatusCode <> 'UPCM'
-- Check if it didn't already existed
AND NOT EXISTS (
 SELECT 1 
 FROM dbo.BusinessOrgReviewPeriodAccess
 WHERE BusinessOrg_ID = @intBusinessOrgID
 AND ReviewPeriod_ID =ReviewPeriod_ID
 AND AppraisalsLocked=0
)
Random Solutions  
 
programming4us programming4us