I work with a packaged accounting system that is distributed and supported to clients by my company. We are not the designers but we can make enhancements at the client's request by adding triggers to the tables at the back end.
There is an agents table and an agent system functions table that are related by an agent id. The functions table has a primary key constraint that comprises (1) agent id; (2) agent type and (3) system function id.
When a new agent is added the system automatically adds 742 new records to the functions table each with a unique key.
My job is to provide a trigger that will add the agent to multiple companies that are selected by the user adding a number to an unused field on the agent record that identifies the company. Each company will have it's own version of the accounting system but on the same server. Getting the agents trigger to add the agent to the companies is working but when the functions trigger comes into operation it falls over with the message "Violation of PRIMARY KEY constraint 'PK_btblAgentSystemFunctions'. Cannot insert duplicate key in object 'dbo_btblAgentSystemFunctions'. The duplicate key value is (258,A,1)".
Here follows the trigger:
USE [Rugs Original Wholesale cc]
GO
/****** Object: Trigger [dbo].[tr_Add_Agent_Function_to_Companies] Script Date: 7/10/2015 3:39:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_Add_Agent_Function_to_Companies]
ON [dbo].[_btblAgentSystemFunctions]
AFTER INSERT
AS
BEGIN
declare @comments VARCHAR(1024)
set @comments = (select cComments from _rtblAgents a inner join inserted i on a.idAgents = i.iAgentID)
declare @ctagentid INT
declare @whagentid INT
declare @rsagentid INT
set nocount on
declare @count as INT = 1
while substring(@comments, @count, 2) <> ' '
begin
if substring(@comments, @count, 2) = '01'
begin
SET @ctagentid = (select MAX(idAgents) from [Rugs Original Cape Town cc].dbo._rtblAgents)
SET @whagentid = (select MAX(idAgents) from [Rugs Original Wholesale cc].dbo._rtblAgents)
INSERT INTO [Rugs Original Cape Town CC].[dbo].[_btblAgentSystemFunctions]
(iAgentID, cAgentType, iSystemFunctionID, iReadAccess,
iEditAccess, iAddAccess, iDeleteAccess, iRuleAccess)
SELECT
@ctagentid, waf.cAgentType, waf.iSystemFunctionID, waf.iReadAccess,
waf.iEditAccess, waf.iAddAccess, waf.iDeleteAccess, waf.iRuleAccess
FROM [Rugs Original Wholesale cc].dbo._btblAgentSystemFunctions waf
WHERE iagentID = @whagentid
--INNER JOIN [Rugs Original Wholesale cc].dbo._rtblAgents wa ON waf.iAgentID = wa.idAgents
--INNER JOIN inserted i ON wa.idAgents = i.iAgentID
end;
if substring(@comments, @count, 2) = '02'
begin
SET @rsagentid = (select MAX(idAgents) from [Re-SA].dbo._rtblAgents)
SET @whagentid = (select MAX(idAgents) from [Rugs Original Wholesale cc].dbo._rtblAgents)
INSERT INTO [Re-SA].[dbo].[_btblAgentSystemFunctions]
(iAgentID, cAgentType, iSystemFunctionID, iReadAccess,
iEditAccess, iAddAccess, iDeleteAccess, iRuleAccess)
SELECT
@rsagentid, waf.cAgentType, waf.iSystemFunctionID, waf.iReadAccess,
waf.iEditAccess, waf.iAddAccess, waf.iDeleteAccess, waf.iRuleAccess
FROM [Rugs Original Wholesale cc].dbo._btblAgentSystemFunctions waf
WHERE iagentID = @whagentid
--INNER JOIN [Rugs Original Wholesale cc].dbo._rtblAgents wa ON waf.iAgentID = wa.idAgents
--INNER JOIN inserted i ON waf.iAgentID = i.iAgentID
end
set @count = @count + 3
end
set nocount off
END
I have used two different methods to solve this problem and both give the same error, viz. (1) using inner joins and (2) identifying the latest agent id to get added using a SELECT MAX statement.
If not try using if not exists (select 1 from dbo_btblAgentSystemFunctio
before the insert.
This might be coming from another trigger though.