SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Delete if records do not exist in another table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vack
Constraint Violating Yak Guru

USA
464 Posts

Posted - 07/25/2012 :  12:22:32  Show Profile  Reply with Quote

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


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


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 )

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 07/25/2012 :  12:51:32  Show Profile  Reply with Quote

delete IA
from ItemAccounts IA
where not exists(select 1 from poitmvnd_sql where IA.ItemCode = item_no and ia.ItemCodeAccount = vend_item_no )


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
Constraint Violating Yak Guru

USA
464 Posts

Posted - 07/25/2012 :  13:08:36  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3826 Posts

Posted - 07/25/2012 :  13:53:28  Show Profile  Reply with Quote
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

India
47040 Posts

Posted - 07/25/2012 :  14:02:26  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3826 Posts

Posted - 07/25/2012 :  14:05:58  Show Profile  Reply with Quote
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;

Edited by - Lamprey on 07/25/2012 14:06:22
Go to Top of Page

Vack
Constraint Violating Yak Guru

USA
464 Posts

Posted - 07/25/2012 :  14:17:24  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3826 Posts

Posted - 07/25/2012 :  14:30:19  Show Profile  Reply with Quote
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

India
47040 Posts

Posted - 07/25/2012 :  15:13:04  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

USA
464 Posts

Posted - 07/25/2012 :  15:44:05  Show Profile  Reply with Quote
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

India
47040 Posts

Posted - 07/25/2012 :  16:46:35  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000