May 12, 2005 at 3:06 am
Hi guys,
Greetings from India!!!
I need some help on a delete operation
My scenario:
I have two tables A,B. I need to first find out which records to delete in A based on A.ID ,and then delete related records from both A, B
Is it possible to execute delete on multiple tables from a single statement? I did some googling and some guy @ a forum said this wasnt possible. Just want to make sure if this is indeed not possible.
Assuming u cant do it from a single query, i decided to go for the foll. Stored proc:
I first get all Ids to delete and store it a local variable
Select @Ids = @Ids + ',' + Id from A where.....(neat trick, heh 😉 )
So my @Ids now looks something like '1002,1003,1004'
I then execute multiple deletes like this-
Set @sql = 'Delete from A where Id in (' + @Ids +');'
Set @sql = @sql + 'Delete from B where Id in (' + @Ids +')'
Execute sp_executesql @sql
My problem is that my local variable, @Ids is limited to only 8000 chars. What if my @Ids supposed to have 1000 numbers of 5 digits each, then wont my @Ids get truncated?
I was also thinking abt creating a temp table to stored my Ids. In that case, the above problem wont occur? But i am concerned abt any performance drag.
Can someone help me as to which approach to take?
Thanks in advance,
Shahed
May 12, 2005 at 3:12 am
if the tables are in a Referential Integrity with each other, just deleting from the parent table, will casade the delete to the parent & child table(s). just issue one delete command. gone. whack, pow!, zapped
May 12, 2005 at 3:24 am
Hi, You could set up a delete trigger on the parent table which deletes related records in the child.
I use this method when i can't use the built in referential integrity: for example when there may me many rows in the parent table with the same ID and i want to delete children only when the last row of any specific parent is deleted.
here's an example:
CREATE TRIGGER t_DeleteLinksDT_APPRAISAL ON [dbo].[DT_APPRAISAL]
FOR DELETE
AS
CREATE TABLE #MyTempTable (Entity_Id INT PRIMARY KEY)
INSERT INTO #MyTempTable (Entity_ID) (SELECT DISTINCT Deleted.Entity_ID FROM Deleted LEFT OUTER JOIN DT_APPRAISAL ON Deleted.Entity_ID = DT_APPRAISAL.Entity_Id WHERE DT_APPRAISAL.Entity_Id IS NULL )
DELETE FROM LNK_APPRAISAL_APPRAISOR WHERE Left_Join IN (SELECT Entity_Id FROM #MyTempTable)
DROP TABLE #MyTempTable
---
Dave
May 12, 2005 at 5:32 am
Thanx Dave for that tip. But is it still possible to do all this in one single query ???
May 12, 2005 at 5:43 am
You mean a single line of SQL? I don't think so
I think the delete trigger method is better than writing a sp as it runs automaticaly. It also gives you access to a special Deleted temporary table so you don't have to create your long vchar to use in your IN() filter.
May 12, 2005 at 5:57 am
DELETE only works on one table at a time.
And I think there are some very good reasons for this.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 12, 2005 at 6:20 am
Care to elaborate on those reasons Frank?? Can't think of many right now.
May 12, 2005 at 6:23 am
What about if you created an updatable view with ALL of the fields from both tables. If there was a 1-1 relationship wouldn't that work?
May 12, 2005 at 6:24 am
Once one has deleted more than one wanted to, I could think of many good reasons. Especially in dynamic environments
Actually I think the only reason is, that it syntactically not allowed.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 12, 2005 at 7:56 am
Thanx Frank,
Can u tell me how this Referential Integrity is implemented, as Bersileus mentioned?
May 12, 2005 at 8:03 am
BOL can do this better than me. You might want to start reading at "cascading referential integrity constraints"
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply