SQL query+SQL Server

I want to delete rows from 3 tables based on ID column. Can you please share logic/or query to delete rows.

ex: 3 tables with the name of A,B,C

ID column exist on all 3 tables. I will pass ID as input parameter and want to delete rows from 3 tables from single query.
mssql_v2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

You can create a stored procedure as below and try.

CREATE PROCEDURE sp_DeleteData(@Id int)
AS

BEGIN 
   DELETE * FROM A WHERE Id=@Id;
   DELETE * FROM B WHERE Id=@Id;
   DELETE * FROM C WHERE Id=@Id;
END

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mssql_v2005Author Commented:
I don;t want SP, can you share without SP
Vitor MontalvãoMSSQL Senior EngineerCommented:
The DELETE command only accepts a table or a view name so I don't think you can't do it from a single statement.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

MohitPanditCommented:
If you want to achieve with your single query then you need to CREATE FOREIGN KEY with ON DELETE CASCADE

Best Regards
mssql_v2005Author Commented:
This solution is working partially for me.
Vitor MontalvãoMSSQL Senior EngineerCommented:
I think you were too fast to accept a solution.
Are the tables related? A delete trigger is an acceptable solution for you?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.