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
 General SQL Server Forums
 New to SQL Server Programming
 Delete if records do not exist in another table

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-07-25 : 12:22:32
[code]
Working with the following Tables:
Table: ItemAccounts

ItemCode AccountCode ItemCodeAccount
123 YDSS Y123

Table: poitmvnd

Item_no Vend_no Vend_item_no
123 100 Y123

Table: Cicmpy

AccountCode Vend_no
YDSS 100
[/code]

ItemAccounts IA link to poitmvnd_sql PV by IA.itemcode = PV.item_no
IA.itemcodeaccount = PV.vend_item_no

ItemAccounts IA link to cicmpy C by AccountCode

Poitmvnd_sql links to Cicmpy by vend_no

I need a script that will check the itemaccounts table that do not have a corresponding record in the poitmvnd_sql table.


Not sure how to do the linking when using not exists

[code]
delete itemaccounts
from ItemAccounts IA
where not exists(select 1 from poitmvnd_sql where IA.ItemCode = po.item_no and ia.ItemCodeAccount = po.vend_item_no )
[/code]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 12:51:32
[code]
delete IA
from ItemAccounts IA
where not exists(select 1 from poitmvnd_sql where IA.ItemCode = item_no and ia.ItemCodeAccount = vend_item_no )
[/code]

i hope table name is poitmvnd_sql itself as sample data shows tablename without _sql

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-07-25 : 13:08:36
What If I have the following:


ItemAccounts
ItemCode AccountCode ItemCodeAccount
123 ABBC A123
123 ZZZY A123

poitmvnd
item_no Vend_no vend_item_no
123 ZZ A123

cicmpy
AccountCode Vend_no
ABBC AB
ZZZY ZZ


Won't the script not delete anything since its going to find a match? I need to add the accountcode in there somehow don't I?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-25 : 13:53:28
Please put your sample data in a consumable format. Also, what do you expect to happen? Do you want 1 of the 2 rows in the ItemAccounts table deleted?

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 14:02:26
quote:
Originally posted by Vack

What If I have the following:


ItemAccounts
ItemCode AccountCode ItemCodeAccount
123 ABBC A123
123 ZZZY A123

poitmvnd
item_no Vend_no vend_item_no
123 ZZ A123

cicmpy
AccountCode Vend_no
ABBC AB
ZZZY ZZ


Won't the script not delete anything since its going to find a match? I need to add the accountcode in there somehow don't I?


what exactly is your requirement? you want it still to be deleted? whats the rule based on which you need to determine that?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-25 : 14:05:58
Here is a guess using a LEFT JOIN and a MERGE statement:
-- Set up sample data
DECLARE @ItemAccounts TABLE (ItemCode INT, AccountCode VARCHAR(10), ItemCodeAccount VARCHAR(10))
INSERT @ItemAccounts
VALUES
(123, 'ABBC', 'A123'),
(123, 'ZZZY', 'A123')

DECLARE @poitmvnd TABLE (item_no INT, Vend_no CHAR(2), vend_item_no VARCHAR(10))
INSERT @poitmvnd
VALUES
(123, 'ZZ', 'A123')

DECLARE @cicmpy TABLE (AccountCode VARCHAR(10), Vend_no CHAR(2))
INSERT @cicmpy
VALUES
('ABBC', 'AB'),
('ZZZY', 'ZZ')

-- Pre Delete
SELECT *
FROM @ItemAccounts

-- Do actual delete
DELETE IA
FROM @ItemAccounts AS IA
LEFT OUTER JOIN
(
SELECT c.*
FROM
@poitmvnd AS p
INNER JOIN
@cicmpy AS c
ON p.Vend_no = c.Vend_no
) AS t
ON IA.AccountCode = T.AccountCode
WHERE T.AccountCode IS NULL

-- Post delete
SELECT *
FROM @ItemAccounts
Using a MERGE statement:
-- Do actual delete
MERGE
@ItemAccounts AS Target
USING
(
SELECT c.*
FROM
@poitmvnd AS p
INNER JOIN
@cicmpy AS c
ON p.Vend_no = c.Vend_no
) AS Source
ON Source.AccountCode = Target.AccountCode
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-07-25 : 14:17:24
I'm looking for the 1st row in the itemaccounts table to be the only record deleted because it does not exist in the poitmvnd table.

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-25 : 14:30:19
quote:
Originally posted by Vack

I'm looking for the 1st row in the itemaccounts table to be the only record deleted because it does not exist in the poitmvnd table.



Then what is the point of the cicmpy table? Or was my guess close, in that, you need to also join to the cicmpy table to get the AccountCode. Then using both the poitmvnd.vend_item_no and cicmpy.AccountCode columns to join to ItemAccounts to determine which is the "missing" rows?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 15:13:04
quote:
Originally posted by Vack

I'm looking for the 1st row in the itemaccounts table to be the only record deleted because it does not exist in the poitmvnd table.




it does exist as per your sample data. didnt understand on what basis you determine existence

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-07-25 : 15:44:05
I was able to work around this by creating SQL view for the POITMVND table and adding the accountcode to it. Then Visakh16's first suggestion worked by adding account code to the where clause.

Thanks for everyone's help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 16:46:35
quote:
Originally posted by Vack

I was able to work around this by creating SQL view for the POITMVND table and adding the accountcode to it. Then Visakh16's first suggestion worked by adding account code to the where clause.

Thanks for everyone's help.


Glad that you sorted it out
But atleast next time please give sample data that exactly illustrates what you want to avoid unnecessary confusion

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -