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 6698629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:36:02+00:00 2026-05-26T06:36:02+00:00

I have written following method with three sql queries in order to update a

  • 0

I have written following method with three sql queries in order to update a single column in a single table.since there are nearly 10000 records to be updated it takes more than 20 mins to complete.is there any other better way to do this updation.some thin like set based update…

private void UpdateLatest()
    {
        string connstr = "Data Source=CHAMARA-PC;Initial Catalog=PHPA_Production_fromEWP;Integrated Security=True";
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            conn.Open();
            SqlTransaction myTransaction = conn.BeginTransaction();
            try
            {
                DataTable dt = new DataTable();
                DataTable DTT = new DataTable();

                string command = "select dh.DocumentNumber,max(dr.RevisionDate) as latestdate from " +
                  "tblDocumentHeader dh inner join tblDocumentRevision dr on dh.DocumentHeaderID=dr.DocumentHeaderID " +
                      "group by dh.DocumentNumber ";


                using (SqlCommand cmd = new SqlCommand(command, conn, myTransaction))
                {
                    using (SqlDataAdapter adapt = new SqlDataAdapter())
                    {
                        adapt.SelectCommand = cmd;
                        adapt.Fill(dt);

                    }
                }
                label1.Text = dt.Rows.Count.ToString();
                foreach (DataRow item in dt.Rows)
                {
                    try
                    {
                        int res = 0;
                        string query2 = "select dr.DocumentRevisionID from " +
                  "tblDocumentHeader dh inner join tblDocumentRevision dr on dh.DocumentHeaderID=dr.DocumentHeaderID " +
                  " where dh.DocumentNumber='" + item["DocumentNumber"].ToString().Trim() + "' and dr.RevisionDate='" + item["latestdate"].ToString().Trim() + "'";

                        using (SqlCommand cmd = new SqlCommand(query2, conn, myTransaction))
                        {
                            using (SqlDataAdapter adapt = new SqlDataAdapter())
                            {
                                adapt.SelectCommand = cmd;
                                adapt.Fill(DTT);

                            }
                        }

                        foreach (DataRow Ritem in DTT.Rows)
                        {
                            string updatequery = "update tblDocumentRevision set LatestRev='latest' where DocumentRevisionID='" + Ritem["DocumentRevisionID"].ToString().Trim() + "'";

                            using (SqlCommand cmd = new SqlCommand(updatequery, conn, myTransaction))
                            {
                                cmd.ExecuteNonQuery();

                                res++;

                            }
                        }
                        listBox1.Items.Add(item["DocumentNumber"].ToString() + " " + "updated");

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                myTransaction.Commit();
                MessageBox.Show("successfully updated");
            }
            catch (Exception ex)
            {
                myTransaction.Rollback();
                MessageBox.Show(ex.Message);

            }
        }
    }
  • 1 1 Answer
  • 1 View
  • 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-26T06:36:03+00:00Added an answer on May 26, 2026 at 6:36 am

    I don’t have your database at hand to test this, but basically, your code boils down to this:

    DECLARE @Temp TABLE (DocNumber INT, LatestDate DATETIME)
    
    INSERT INTO @Temp(DocNumber, LatestDate)
        SELECT 
            dh.DocumentNumber, MAX(dr.RevisionDate) 
        FROM 
            dbo.tblDocumentHeader dh 
        INNER JOIN 
            dbo.tblDocumentRevision dr ON dh.DocumentHeaderID = dr.DocumentHeaderID
        GROUP BY 
            dh.DocumentNumber
    
    UPDATE 
        dbo.tblDocumentRevision 
    SET 
        LatestRev = 'latest' 
    FROM
        dbo.tblDocumentRevision dr
    INNER JOIN
        dbo.tblDocumentHeader dh ON dh.DocumentHeaderID = dr.DocumentHeaderID 
    INNER JOIN
        @Temp t ON dh.DocumentNumber = t.DocNumber AND dr.RevisionDate = t.LatestDate
    

    This can be very easily wrapped in a stored procedure, it does not use any dead slow cursors, and it does not cause any SQL injection possibilities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule
i have written the following code: As you can see there is a for
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written the following Utility class to get an instance of any class
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following: var pages=[[www.google.co.uk] This is the WWW. ,[www.yahoo.co.uk] This is
I have written the following code. But it is removing only &nbsp; not <br>
I have written the following function but it's isn't returning anything when I run
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *

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.