Overview:I've got a clients table and a salesrep table, in between i got a clientsalesrep table which stores information on which salesreps belong to which clients. I have two sprocs, one which adds a new clients and assigns the salesrep(s) for that client. Goal:I'm building the sproc that updates a client and their salesrep(s). When updating a client, there are 4 case scenarios:The client may have:1) the same rep(s);2) the same rep(s) and one or more rep(s) need to be added;3) one or more rep(s) to be deleted;4) one or more rep(s) need to be added AND one or more rep(s) to be deleted.Process:I figured as much there is only needed 2 sprocs, one to add a rep (which is already done) and the other to delete (more like update a field saying the rep is no longer 'active' with the particular client, you'll see more with the table design). What I want to get from this forum, is the simplest query statement possible to do both add a new rep and delete an old one.For exmaple:When I add or update a client, I sent to the sproc a varchar(100) of a list of reps for that client in the format "1,2,3", where 1, 2, and 3 are the repids. I currently have code that cycles thru the comma deliminated list and add the rep to the client.So what I'm REALLY looking for here, is a sproc where I can send a list like 1,2,3 along with the clientid and if the client has salesreps 2,3,4,5 then I want 4 and 5 to be marked as inactive and 1 to be added. Make sense? Hope so, if not just ask.The sproc which processes the SalesRepIDsCREATE procedure dbo.proc_ProcessClientSalesReps @ClientUID char(32) , @SalesRepID varchar(100)as declare @Error int , @ClientID int declare @ErrorCode int select @ErrorCode = @@Error select @ClientID = Client.ClientID from Client where (Client.ClientUID = dbo.FormatGUID(@ClientUID)) declare @TransactionCountOnEntry int --Loop thru the @SalesRepID to grab each ID then process them accordinglyif @ErrorCode = 0begin declare @SalesRepsID int , @Count int , @CurSalesRepID int , @Pos int select @Count = 0 select @SalesRepID = ltrim(rtrim(@SalesRepID)) + ',' select @Pos = charindex(',', @SalesRepID, 1) if replace(@SalesRepID, ',', '') <> '' begin while @Pos > 0 begin select @CurSalesRepID = ltrim(rtrim(left(@SalesRepID, @Pos - 1))) if @CurSalesRepID <> '' begin-- Here i'd like to run a sproc that runs an update sproc to update the reps that are already listed with the client-- then another to add any reps that are in the list, but not already in the db.-- I understand that here I am splitting up the IDs in @SalesRepID, but i'd like to eliminate this but just sending the whole lst to a query if possible.-- This one adds, but does not check first to see if they already exist.-- exec @Error = dbo.proc_AddClientSalesRep @ClientID, @CurSalesRepID, @SalesRepsID output if @Error = 0 select @Count = @Count + 1 else break end select @SalesRepID = right(@SalesRepID, len(@SalesRepID) - @Pos) select @Pos = charindex(',', @SalesRepID, 1) end select @ErrorCode = @Error endendif @ErrorCode = 0begin if @@Error <> 0 select @ErrorCode = @@Errorendif @ErrorCode = 0begin if @Count < 1 select @ErrorCode = 1endif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGOCREATE procedure dbo.proc_AddClientSalesRep @ClientID int , @SalesRepID int , @ClientSalesRepID int outputas declare @ErrorCode int select @ErrorCode = @@Error declare @TransactionCountOnEntry int if @ErrorCode = 0begin select @TransactionCountOnEntry = @@TranCount begin transaction insert ClientSalesRep ( ClientID , SalesRepID ) values ( @ClientID , @SalesRepID ) select @ClientSalesRepID = @@Identity, @ErrorCode = @@Errorendif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGO
Table Design:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ClientSalesRep]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[ClientSalesRep]GOCREATE TABLE [dbo].[ClientSalesRep] ( [ClientSalesRepID] [int] IDENTITY (1, 1) NOT NULL , [ClientID] [int] NOT NULL , [SalesRepID] [int] NOT NULL , [SalesRepEffectiveDate] [datetime] NOT NULL , [SalesRepEndDate] [datetime] NULL , [SalesRepActive] [bit] NOT NULL ) ON [PRIMARY]GOdrop table [dbo].[ClientSalesRep]GO
- RoLY roLLs