Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:42:44+00:00 2026-05-31T02:42:44+00:00

I have a table with just over 1 million records. Initially, my table is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:42:46+00:00Added an answer on May 31, 2026 at 2:42 am

    Yes. You can do it with a single UPDATE - FROM statement after your BULK INSERT:

    UPDATE t3 SET initialValue = t.mySum
    FROM test3 t3
    CROSS APPLY (SELECT SUM(
           CASE t3.v1 WHEN 'M170_2' THEN CASE d.M170_2 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           CASE t3.v1 WHEN 'M170_3' THEN CASE d.M170_3 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           CASE t3.v1 WHEN 'M170_4' THEN CASE d.M170_4 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           d.RESP_WEIGHT / 4898.947426) as mySum 
           FROM my_data_db d WHERE d.combo = t3.combo) t
    WHERE t3.v1 IS NOT NULL OR t3.v2 IS NOT NULL OR t3.v3 IS NOT NULL
    

    To do this from your trigger, you need a minor change:

    UPDATE t3 SET initialValue = t.mySum
    FROM test3 t3
    -- Here's the change
    INNER JOIN inserted i ON i.RowID = t3.RowID
    CROSS APPLY (SELECT SUM(
           CASE t3.v1 WHEN 'M170_2' THEN CASE d.M170_2 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           CASE t3.v1 WHEN 'M170_3' THEN CASE d.M170_3 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           CASE t3.v1 WHEN 'M170_4' THEN CASE d.M170_4 
                 WHEN 1 THEN 1 
                 WHEN 2 THEN .75 
                 WHEN 3 THEN .25 
                 WHEN 4 THEN .1 
           ELSE 1 END END * 
           d.RESP_WEIGHT / 4898.947426) as mySum 
           FROM my_data_db d WHERE d.combo = t3.combo) t
    WHERE t3.v1 IS NOT NULL OR t3.v2 IS NOT NULL OR t3.v3 IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySql database with just over 30 Million rows, but some of
I have a table that has grown to over 1 million records... today (all
I have a relatively large database tables (just under a million rows) that has
I have a large table consisting of over 60 millions records and I would
I have some database table and need to process records from it 5 at
I have a table with a TEXT column where the contents is just strings
I'm just learning ruby on rails and I have a table of user roles
If I have a table of a hundred users normally I would just set
Just a small question regarding joins. I have a table with around 30 fields
We have a 10 thousand row table that has just 2 columns, a primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.