JmukhadzeT
JmukhadzeT

Reputation: 99

Insert statement takes too long to finish

I am trying to Insert some data from one table to another in SQL Server , but it takes too long to finish even when affected rows are 0.

Below is the query I am using to Insert :

INSERT INTO Table_1 (Prop_1, Prop_2)
    SELECT @passedParam , Prop_2 
    FROM Table_2
    WHERE Prop_1 = @passedParam - 1 
      AND Prop_2 NOT IN (SELECT Prop_2 
                         FROM Table_2
                         WHERE Prop_1 = @passedParam)

Some additional information:

Can anyone explain what the issue is, and why it takes so long to finish?

Please tell if there is a faster way to insert data into the table.

Upvotes: 0

Views: 424

Answers (1)

SQLpro
SQLpro

Reputation: 5121

Try this way :

INSERT INTO Table_1 
       (Prop_1,      Prop_2)
SELECT @passedParam, Prop_2 
FROM   Table_2
WHERE  Prop_1 = @passedParam - 1 
EXCEPT
SELECT Prop_2, @passedParam 
FROM   Table_2
WHERE  Prop_1 = @passedParam;

Upvotes: 1

Related Questions