performance in sql server

  • Hi every one,

    I have 3 table as below:

    Table Attachment

    IDAttachment, Name

    1 | A

    2 | B

    Table ActionPerformedAttachment

    IDActionPerformedAttachmentIDActionPerformedIDAttachment

    1| A| 1

    2| A| 1

    3| A| 1

    4| B| 2

    5| B| 2

    Table ActionPerformed

    IDActionPerformedIDProject

    A | P1

    B | P2

    Now I have IDProject = P1, and I want to delete data in table Attachment and ActionPerformedAttachment

    So I can do it by 2 case:

    Case 1: I use temp table for delete

    Case 2: I use cursor for delete.

    Please help me, what is the best case.

    Thank you very much.

  • Probably neither. Cursors should be avoided wherever possible, and I don't see a reason for a temp table here.

    You just want to delete the data relating to Project P1? If so, please can you post actual table definitions (with foreign keys) so we can see how the tables are related

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for your reply, and I think user temp table is the best case.

    Sample:

    DECLARE @TmpIDAttachment TABLE(

    IDActionPerformedAttachment INT,

    IDAttachment INT)

    INSERT INTO @TmpIDAttachment( IDActionPerformedAttachment ,IDAttachment)

    SELECT ActionPerformedAttachment.IDActionPerformedAttachment, dbo.ActionPerformedAttachment.IDAttachment

    FROM dbo.ActionPerformed

    INNER JOIN dbo.ActionPerformedAttachment ON dbo.ActionPerformed.IDActionPerformed = dbo.ActionPerformedAttachment.IDActionPerformed

    WHERE dbo.ActionPerformed.IDProject = P1

  • What are you using a table variable for? There's no need for table variable, temp table or cursor that I can see here.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank you very much

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply