SQL删除当列a和列b不存在时

时间:2009-11-20 19:50:43

标签: sql

好的,所以你有这样的工作。您从tmp表中插入表格,其中缺少设备编号和帐号...

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

但是如果设备编号之前属于不同的帐号,则发现问题,那么我之前的查询将插入一个新行,具有相同的设备编号但是新的帐号。

我需要它做的很简单:

  1. 如果存在帐号和设备号,请不要插入。
  2. 如果设备编号存在,但已将其分配给其他帐号,请删除旧行。 (导入文件处理分配,因此我100%确定需要将其分配给新帐户)
  3. 在上一段代码中添加的东西:

    DELETE FROM ClientEquipments
    WHERE     (clientEquipmentId =
                              (SELECT     clientEquipmentId
                                FROM          ClientEquipments AS ClientEquipments_1
                                WHERE      (equipmentNumber = '0012345CAEC6')))
    
    1. 如果不存在,则插入新行。
    2. :::编辑更多信息以帮助我:::

      我正在阅读CSV文件:

      示例数据:

      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"
      

      这会被批量导入到临时表中。 (dbo.clients_temp)

      请注意帐户1234如何拥有2个设备编号。

      从这里我通过从dbo.clients_temp到dbo.clients的查询将新帐户插入dbo.clients

      然后我用dbo.clients_temp中的新信息更新dbo.clients(即帐户1234可能存在,但现在他们有了新地址。)

      现在我的dbo.clients表是使用新客户端更新的,以及现有客户端的新信息,我需要更新我的dbo.equipments表。我原来做的就是你上面看到的,插入不存在的帐号和设备号。

      现在的问题是,由于设备确实会更改帐户,例如,帐号5678可能已变为非活动状态,我不会在数据库级别跟踪或关注,但设备编号现在可能属于帐号1234。在这种情况下,我的原始查询将在数据库中插入一个新行,因为在SELECT中不返回Account 1234和Equipment Number。

      好的,我现在已经失去了这个:P我会在周末晚些时候尝试重新回答这个问题,因为我只是把自己混淆了O.o

3 个答案:

答案 0 :(得分:1)

我不得不稍微修改一下Gordon的答案,但这就是诀窍......

以下是删除非活动帐户的相关代码行。

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)

答案 1 :(得分:0)

我可能会误解,但是如果你要做的就是删除帐号不等于某物且设备号等于某物的记录,你不能只用多个执行删除条件在哪里?

示例:

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

然后,您可以使用@@ROWCOUNT获取受影响的行数,以检查受影响的行数,然后在未删除任何内容时插入。上面的TechNet链接中的示例使用以下示例:

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

我认为你可以轻松地调整它来做你想要做的事情。

答案 2 :(得分:0)

-- 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)