Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Complex? Or Simple Query?

Author  Topic 

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2004-11-08 : 16:40:59
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 SalesRepIDs
CREATE 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 accordingly
if @ErrorCode = 0
begin
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
end
end

if @ErrorCode = 0
begin
if @@Error <> 0
select @ErrorCode = @@Error
end

if @ErrorCode = 0
begin
if @Count < 1
select @ErrorCode = 1
end

if @@TranCount > @TransactionCountOnEntry
begin
if @ErrorCode = 0
commit transaction
else
rollback transaction
end

return @ErrorCode

GO


CREATE procedure dbo.proc_AddClientSalesRep
@ClientID int
, @SalesRepID int
, @ClientSalesRepID int output
as
declare @ErrorCode int
select @ErrorCode = @@Error

declare @TransactionCountOnEntry int

if @ErrorCode = 0
begin
select @TransactionCountOnEntry = @@TranCount
begin transaction
insert
ClientSalesRep
(
ClientID
, SalesRepID
)
values
(
@ClientID
, @SalesRepID
)

select @ClientSalesRepID = @@Identity, @ErrorCode = @@Error
end

if @@TranCount > @TransactionCountOnEntry
begin
if @ErrorCode = 0
commit transaction
else
rollback transaction
end

return @ErrorCode


GO



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]
GO

CREATE 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]
GO
drop table [dbo].[ClientSalesRep]
GO




- RoLY roLLs

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2004-11-08 : 17:00:21
BTW: I'm looking to see if "NOT IN" will help me....maybe an update statement that updates rows with the current client, but which have clients that are "NOT IN" the list.

But i'm having problems with a select like this:

declare @id varchar(100)
select @id = '1,2'

select
*
from
clientsalesrep
where
clientid = 1
and salesrepid not in (select @id)


...i get the error
'Syntax error converting the varchar value '1,2' to a column of data type int.'

- RoLY roLLs
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-11-08 : 21:42:23
I reckon you want to look at a Splitter function - there are plenty on here, perhaps search for CSV and SPLIT as a kick off.

The Splitter function will covnert your
@SalesRepID varchar(100)
into a "table" of inidivual values - then you can jsut join that to your ClientSalesRep and insert/update as appropriate

-- Add new reps
INSERT ClientSalesRep ( column list )
SELECT Column1, Column2 ...
FROM dbo.MySplitter(@SalesRepID) REP
LEFT OUTER JOIN ClientSalesRep CSR
ON CSR.SalesRepID = REP.ID
AND CSR.ClientID = @ClientUID
WHERE CSR.SalesRepID IS NULL -- Just the "missing" ones

-- Inactivate "stale" reps
UPDATE U
SET SalesRepActive = 0
FROM ClientSalesRep U
LEFT OUTER JOIN dbo.MySplitter(@SalesRepID) REP
ON REP.ID = U.SalesRepID
WHERE U.ClientID = @ClientUID
AND REP.ID IS NULL -- SalesRep not in current list

Kristen
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2004-11-16 : 15:17:58
Sorry I took long to respond. Thanks Kristen. I'll look around for that.

- RoLY roLLs
Go to Top of Page
   

- Advertisement -