I have a table with just over 1 million records. Initially, my table is empty, but I use a BULK INSERT
to add these records to the database. I have an AFTER INSERT
trigger that I use to update the initialValue
field in this table. The value of initialValue
is a calculation of specific variables in another table (my_data_db
) summed across all records. I am using the values of the v1
, v2
, etc. columns as the column names in the table my_data_db
.
I know it's poor practice, but the only way I currently know how to do this calculation of every single row is by using a cursor. Obviously with a million records this is really, really slow.
Here's an example of the table that I have the trigger on:
TABLE: test3
rowID v1 v2 v3 combo initialValue
1 NULL M170_3 M170_4 C NULL
2 M170_2 M170_3 M170_4 ABC NULL
3 M170_2 M170_3 NULL AB NULL
...
My trigger is:
CREATE TRIGGER [dbo].[trig_UPDATE_test3]
ON [dbo].[test3]
AFTER INSERT
AS
Begin
DECLARE @sql VARCHAR(MAX)
DECLARE @v1 VARCHAR(20)
DECLARE @v2 VARCHAR(20)
DECLARE @v3 VARCHAR(20)
DECLARE @combo VARCHAR(30)
DECLARE mycursor CURSOR FOR
SELECT v1, v2, v3, combo
FROM Inserted
OPEN mycursor
FETCH NEXT FROM mycursor INTO @v1, @v2, @v3, @combo
WHILE @@FETCH_STATUS = 0
BEGIN
IF( @v1 IS NOT NULL OR @v2 IS NOT NULL OR @v3 IS NOT NULL)
BEGIN
SET @sql = 'DECLARE @finalValue DECIMAL(18, 15);'
SET @sql = @sql + 'UPDATE test3 Set initialValue = (SELECT CAST(SUM('
IF(@v1 IS NOT NULL)
BEGIN
SET @sql = @sql + 'CASE ' + @v1 + ' WHEN 1 THEN 1 WHEN 2 THEN .75 WHEN 3 THEN .25 WHEN 4 THEN .1 END * '
END
IF(@v2 IS NOT NULL)
BEGIN
SET @sql = @sql + 'CASE ' + @v2 + ' WHEN 1 THEN 1 WHEN 2 THEN .75 WHEN 3 THEN .25 WHEN 4 THEN .1 END * '
END
IF(@v3 IS NOT NULL)
BEGIN
SET @sql = @sql + 'CASE ' + @v3 + ' WHEN 1 THEN 1 WHEN 2 THEN .75 WHEN 3 THEN .25 WHEN 4 THEN .1 END * '
END
SET @sql = @sql + 'RESP_WEIGHT / 4898.947426) AS FLOAT) FROM dbo.my_data_db) WHERE combo = ''' + @combo + ''';'
EXECUTE(@sql)
END
FETCH NEXT FROM mycursor INTO @v1, @v2, @v3, @combo
END
CLOSE mycursor
DEALLOCATE mycursor
End
After the trigger runs, my test3
table would look something like:
TABLE: test3
rowID v1 v2 v3 combo initialValue
1 NULL M170_3 M170_4 C 0.138529
2 M170_2 M170_3 M170_4 ABC 0.683190
3 M170_2 M170_3 NULL AB 0.014923
...
Is there a way I can accomplish this without the use of a cursor?