Reputation: 99
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:
@passedParam
as a parameterTable_2
contains around 28,000 rowsTable_2
and inserting result into Table_1
based on some logic. Prop_1
and Prop_2
are regular columns, not PK or FK and datatypes are INT
and BIGINT
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
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