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..

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
jakobiyem
  • 107
  • 1
  • 5
  • 15

4 Answers4

0
DELETE FROM table_1
    FROM table_1 
        INNER JOIN table_2 AS Y
            ON table_1.id = Y.id
renegm
  • 610
  • 2
  • 5
  • 12
  • 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. – Will Marcouiller Dec 19 '10 at 02:10
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 =))

Community
  • 1
  • 1
jakobiyem
  • 107
  • 1
  • 5
  • 15
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

Bonshington
  • 3,970
  • 2
  • 25
  • 20
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

ABI
  • 1,714
  • 15
  • 14