• As Sean said, you need an ORDER BY statement.

    I created some sample data and a query that you could use for reference. I am a fan of using the CTE approach for this kind of thing. You need something more like this:

    -- (1) Some sample data

    DECLARE @sampleData1 TABLE (id int primary key, data varchar(36) not null);

    DECLARE @sampleData2 TABLE (id int unique not null);

    WITH sampleData AS

    (SELECTROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS id,

    NEWID() AS data

    FROM sys.all_columns)

    INSERT INTO @sampleData1

    SELECT * FROM sampleData

    WHERE id<=20;

    INSERT INTO @sampleData2

    VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

    -- review the output pre-delete

    SELECT * FROM @sampleData1;

    WITH DeleteThisStuff AS

    (

    SELECT TOP 10 s1.id

    FROM @sampleData1 s1

    JOIN @sampleData2 s2 ON s1.id=s2.id

    ORDER BY s1.id

    )

    DELETE FROM @sampleData1

    WHERE id IN (SELECT id FROM DeleteThisStuff)

    SELECT * FROM @sampleData1;

    Note: I did TOP 10 for an easier-to-read result set. Let us know if this helps.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001