Avatar of Philip van Gass
Philip van GassFlag for South Africa

asked on 

A question concerning agents used by companies on an accounting system

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

Avatar of undefined
Last Comment
Philip van Gass
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Can an agent have different permissions / system functions for different companies?

If not try using if not exists (select 1 from dbo_btblAgentSystemFunctions where ...)

before the insert.

This might be coming from another trigger though.
ASKER CERTIFIED SOLUTION
Avatar of Philip van Gass
Philip van Gass
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Philip van Gass

ASKER

You can close this question.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo