Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
DELETE FROM 
ledger l 
LEFT JOIN Party p on p.Id = l.Account_Id OR p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL

Shows an error
"Incorrect syntax near 'l'. Severity 15"
Posted
Updated 1-Mar-12 9:06am
v7
Comments
OriginalGriff 29-Feb-12 5:37am    
Do you get an error message?
Or does something happen that shouldn't?
Photon_ 29-Feb-12 5:54am    
i got error
Incorrect syntax near 'l'. Severity 15

I assume that your tables name is "ledger l"?
Try delimiting it:
SQL
DELETE FROM 
[ledger l]
LEFT JOIN Party WHERE p.Id = l.Account_Id OR p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL
The problem could well be that the space in the name is being treated as the name end.
 
Share this answer
 
Comments
Photon_ 29-Feb-12 7:51am    
gives error Incorrect syntax near the keyword 'LEFT'. Severity 15
OriginalGriff 29-Feb-12 8:52am    
Ah! Take teh delimiters back out - it's more complex than that!
Have a look at this blog. It does what you want but the syntax is a little odd to my mind:
http://www.bennadel.com/blog/939-Using-A-SQL-JOIN-In-A-SQL-DELETE-Statement-Thanks-Pinal-Dave-.htm
Hi,
Find you solution here:
SQL
DELETE FROM ledger
WHERE Account_ID IN (SELECT id FROM Party LEFT JOIN AcGroup ON AcGroup.ID = Party.AcGroup_ID AND AcGroup.Id IS NULL)
AND ContraAccount_Id IN (SELECT id FROM Party LEFT JOIN AcGroup ON AcGroup.ID = Party.AcGroup_ID AND AcGroup.Id IS NULL)

I don't know the connection between Account_id and ContractAccount_id relationship.
 
Share this answer
 
Comments
Photon_ 1-Mar-12 2:09am    
both are connected to party.id
[no name] 1-Mar-12 4:11am    
yes
i think the query should be something like

SQL
DELETE FROM
ledger l
JOIN Party ON p.Id = l.Account_Id 
JOIN Party ON p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL


Before deleting the contents check the data by using the Select statement. If the records are correct then execute the delete statement..
 
Share this answer
 
Comments
Photon_ 29-Feb-12 5:45am    
see improved question error comes near 'l'
DELETE FROM
ledger l
LEFT JOIN Party p WHERE p.Id = l.Account_Id OR p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL
 
Share this answer
 
Comments
Photon_ 1-Mar-12 0:22am    
gives error
Incorrect syntax near 'l'. Severity 15
Hi there..

Try this..

SQL
DELETE l FROM
ledger  l
LEFT JOIN Party p ON p.Id = l.Account_Id OR p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL


all the best..
 
Share this answer
 
Comments
Photon_ 1-Mar-12 2:10am    
Great good work
hi findz..
in ur query there is no alias name table party and u r using 'p' as alias in query so first correct it and use ON before WHERE clause, try this may this is ur answer

DELETE l FROM ledger l
LEFT JOIN Party p ON p.Id = l.Account_Id OR p.Id = l.ContraAccount_Id
LEFT JOIN AcGroup acg ON acg.Id = p.AcGroup_Id
WHERE acg.Id is NULL



plz revert back with ur comments.....
 
Share this answer
 
v2
Comments
Photon_ 1-Mar-12 2:13am    
you were right but the main mistake was
delete 'l'

i was not adding this 'l' thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900