Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Delete When column a and column b does not exist
    primarykey
    data
    text
    <p>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...</p> <pre><code>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 </code></pre> <p>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.</p> <p>What I need it to do is simple:</p> <ol> <li>If Account Number and Equipment Number exists, leave it alone no need to insert.</li> <li>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)</li> </ol> <p>Something Like this added somewhere in the previous code:</p> <pre><code>DELETE FROM ClientEquipments WHERE (clientEquipmentId = (SELECT clientEquipmentId FROM ClientEquipments AS ClientEquipments_1 WHERE (equipmentNumber = '0012345CAEC6'))) </code></pre> <ol start="3"> <li>If nothing exists then Insert a new row.</li> </ol> <p><strong>:::EDIT SOME MORE INFORMATION TO HELP ME OUT:::</strong></p> <p>I am reading a CSV file:</p> <p>Sample Data:</p> <pre><code>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" </code></pre> <p>This gets bulked imported into a temp table. (dbo.clients_temp)</p> <p>Notice how account 1234 has 2 equipment numbers.</p> <p>From here I insert new accounts into dbo.clients by doing a query from dbo.clients_temp to dbo.clients</p> <p>Then I update dbo.clients with new information from dbo.clients_temp (ie Account 1234 might exists but now they have a new address.)</p> <p>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.</p> <p>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. </p> <p>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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload