Click here to Skip to main content
Sign Up to vote bad
good
See more: SQL2005
i used below code to delete rows from two table by passing single value but i am getting error like
 
Incorrect syntax near ','
 
DELETE messages ,usersmessages FROM messages INNER JOIN usersmessages
WHERE messages.messageid= usersmessages.messageid and messages.messageid = 1
Posted 17 Dec '12 - 22:27
Edited 17 Dec '12 - 23:37

Comments
Rai Pawan - 18 Dec '12 - 4:41
you can use foreign key references with cascading delete.
Umapathi K - 18 Dec '12 - 4:46
i didnt use any primary key and foreign key
skydger - 18 Dec '12 - 5:51
If you have no any foreign keys with cascade delete constraint then you should create a procedure which combines two separate deletes DELETE FROM usermessages WHERE messageid = 1 DELETE FROM messages WHERE messageid = 1 You can also create a trigger on one of these tables on delete action.

5 solutions

Try
 
BEGIN TRY  
  BEGIN TRANSACTION  
 
   DELETE FROM usersmessages
   WHERE  messageid = 1
 
   DELETE FROM messages 
   WHERE  messageid = 1
 

  COMMIT TRANSACTION  
 END TRY  
 BEGIN CATCH  
  ROLLBACK TRANSACTION  
 END CATCH  
  Permalink  
begin transaction;
 
   declare @deletedIds table ( id int );
 
   delete t1
   output deleted.id into @deletedIds;
   from table1 t1
    join table2 t2
      on t2.id = t1.id
    join table3 t3
      on t3.id = t2.id;
 
   delete t2
   from table2 t2
    join @deletedIds d
      on d.id = t2.id;
 
   delete t3
   from table3 t3 ...
 
commit transaction;
 
Happy Coding Smile | :)
  Permalink  
Please Read the DELETE Query Syntax
 
DELETE FROM table_name
WHERE some_column=some_value
  Permalink  
Hi,
 
Your syntax is wrong here. Use like the following.
 
DELETE  FROM messages INNER JOIN usersmessages
WHERE messages.messageid= usersmessages.messageid and messages.messageid = 1
 

Thanks
  Permalink  
Hi
 
Can you try the below query:
 
DELETE MESSAGES  FROM MESSAGES INNER JOIN USERSMESSAGES
ON MESSAGES.MESSAGEID= USERSMESSAGES.MESSAGEID WHERE MESSAGES.MESSAGEID = 1
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,123
1 OriginalGriff 6,040
2 CPallini 3,432
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid