tags:

views:

158

answers:

3

Ok, so you have something like this working. You Insert into a table from a tmp table, where the Equipment Number and the Account Number are missing...

Insert INTO ClientEquipments(
    SUB_ACCT_NO_SBB,
    EquipmentDate,
    EquipmentText,
    EquipmentNumber)
Select 
    replace(a.SUB_ACCT_NO_SBB,'"','') as SUB_ACCT_NO_SBB,
    getdate() as edate,'' as etext,
    replace(a.equipmentNumber,'"','')  equipmentNumber 
from clientspaymenttemp a 
where not exists 
    (select b.equipmentNumber 
    from clientEquipments b 
    where b.sub_acct_no_sbb=replace(a.SUB_ACCT_NO_SBB,'"','') and b.equipmentNumber=replace(a.equipmentNumber,'"',''))
group by SUB_ACCT_NO_SBB,equipmentNumber

But found a problem if the Equipment Number belonged to a different account number before, then my previous query will insert a new row, with the same Equipment Number but a new Account Number.

What I need it to do is simple:

  1. If Account Number and Equipment Number exists, leave it alone no need to insert.
  2. If Equipment Number exists, but it's assigned to a different Account Number, delete the old row. (Import file handles assignments so I am 100% sure that it needs to be assigned to new account)

Something Like this added somewhere in the previous code:

DELETE FROM ClientEquipments
WHERE     (clientEquipmentId =
                          (SELECT     clientEquipmentId
                            FROM          ClientEquipments AS ClientEquipments_1
                            WHERE      (equipmentNumber = '0012345CAEC6')))
  1. If nothing exists then Insert a new row.

:::EDIT SOME MORE INFORMATION TO HELP ME OUT:::

I am reading a CSV file:

Sample Data:

Account | Name | Address | Some Extra Stuff | Equipment Number
"1234","First1,Last1","Address 1",etc etc... "ENum1234"
"1234","First1,Last1","Address 1",etc etc... "ENum5678"
"5678","First2,Last2","Address 2",etc etc... "ENum9123"
"9123","First3,Last3","Address 3",etc etc... "ENum4567"

This gets bulked imported into a temp table. (dbo.clients_temp)

Notice how account 1234 has 2 equipment numbers.

From here I insert new accounts into dbo.clients by doing a query from dbo.clients_temp to dbo.clients

Then I update dbo.clients with new information from dbo.clients_temp (ie Account 1234 might exists but now they have a new address.)

Now that my dbo.clients table is update with new clients, and new information for existing clients, I need to update my dbo.equipments table. I was originally doing what you see above, Insert Where Not Exists Account Number and Equipment Number.

Now the problem is that since equipments do change accounts, for example, Account Number 5678 might have become inactive which I don't track or care for at the database level, but the equipment Number might now belong to Account Number 1234. In this case, my original query will insert a new row into the database, since Account 1234 and Equipment Number are not returned in the SELECT.

Ok, I have lost this now :P I will try and revisit the question later on the weekend because I just confused myself O.o

A: 

I may be misunderstanding, but if all you're looking to do is delete a record where the account number isn't equal to something and the equipment number is equal to something, can't you just perform a delete with multiple where conditions?

Example:

DELETE FROM table
WHERE
equipmentNumber = someNumber AND
accountNumber <> someAccount

You could then get the number of rows affected using @@ROWCOUNT to check the number of rows affected and then insert if nothing was deleted. The example from the TechNet link above uses the following example:

USE AdventureWorks;
GO
UPDATE HumanResources.Employee 
SET Title = N'Executive'
WHERE NationalIDNumber = 123456789
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';
GO

I would think you could easily adapt that to do what you're looking to do.

Brian Hasden
Well, let me add more information maybe we can both understand this better. I will added to my original post.
jesusOmar
Ok. Currently, it sounds like what you're trying to do is delete a row where the equipment number is assigned to a different account than what it should be. If it's assigned to the correct account number, do nothing. Otherwise if the equipment number doesn't exist, insert a new row with the equipment number associated with the correct account number.
Brian Hasden
A: 
-- Fix Account Numbers and Equipment Numbers
update ClientPaymentTemp
set SUB_ACCT_NO_SBB = replace(SUB_ACCT_NO_SBB,'"',''),
equipmentNumber = replace(equipmentNumber,'"','')

-- Delete Existing Accounts Mapped to New Equipment
delete e
from ClientEquipments e
inner join clientspaymenttemp  t
on e.EquipmentNumber = t.EquipmentNumber
and e.SUB_ACCT_NO_SBB <> t.SUB_ACCT_NO_SBB 

-- Insert New Accounts
insert into ClientEquipments
(SUB_ACCT_NO_SBB,
    EquipmentDate,
    EquipmentText,
    EquipmentNumber)
Select 
    SUB_ACCT_NO_SBB,
    getdate() as edate,
    '' as etext,
    equipmentNumber 
from ClientsPaymentTemp a 
where not exists (select 1 from ClientEquipments where SUB_ACCT_NO_SBB = a.SUB_ACCT_NO_SBB and EquipmentNumber = a.EquipmentNumber)
Gordon Bell
Not sure why you have a -1, but I will try this code on Monday to see how it works out. Will post back the results.
jesusOmar
+1  A: 

I had to modify Gordon's answer above a bit, but that did the trick...

Below is the relevant line of code that deletes the inactive accounts.

DELETE FROM ClientEquipments WHERE EquipmentNumber =  
(SELECT E.equipmentNumber FROM ClientEquipments As E INNER JOIN ClientsPaymentTemp AS T 
 on E.equipmentNumber = T.equipmentNumber and e.SUB_ACCT_NO_SBB <> T.SUB_ACCT_NO_SBB)
jesusOmar
Good to hear you got it figured out.
Brian Hasden