3

i would like to use two tables in my deleting stored procedure. Normally, i can write the query like this:

delete * from table_1 x, table_2 y
where x.ID = y.ID

But i coundn't do it with stored procedure(as you know=)

please help..

2

4 Answers 4

0
DELETE FROM table_1
    FROM table_1 
        INNER JOIN table_2 AS Y
            ON table_1.id = Y.id
Sign up to request clarification or add additional context in comments.

1 Comment

This will only delete rows from table_1. The OP wishes to delete all rows from both tables with only one delete statement, which after multiple tries, I think he'd better go with the link in Sebastian Piu's comment.
0

Thank you for all answers, i could do it with using "renegm" and "Sebastian Piu"s comments. Yes, i'd like to delete all rows from both tables so i did it like that:

USE DB

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE delUser
(
@ID int
)
AS
DELETE FROM Table_1
FROM Table_1
INNER JOIN Table_2 AS Y
ON Table_1.ID = Y.ID
AND Y.ID = @ID
DELETE FROM Table_2
WHERE Table_2.ID = @ID

It's working but is it really solution i don't know exactly =))

Comments

0
select distinct ID
into #temp
from table_1
join table_2
on table_1.ID = table_2.ID

delete table_1
where ID in (select * from #temp)

delete table_2
where ID in (select * from #temp)

drop table #temp

simplest + no confusion

Comments

0

Delete only is for a table.

MSDN description for delete

DELETE (Transact-SQL)

Removes rows from a table or view.

if u want always delete from table2 when delete from table1, u can use trigger on table1.

see this link: http://forums.devshed.com/showpost.php?p=322160&postcount=5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.