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

  • Home
  • SEARCH
  • 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 3345952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:14:46+00:00 2026-05-18T01:14:46+00:00

I have designed a stored procedure usign Sql Server 2005 below to compare 3

  • 0

I have designed a stored procedure usign Sql Server 2005 below to compare 3 million records in each of the Profile and Source Table, and update the Source table with records exist in another table (PROFILE_BC) which will also have about 3 million records.
I am trying to Optimize this code below. Can you suggest any other method ? I just worried that this will take more than about 6 hours to complete. Can we do the same using DTS ? And ideas how this can be done using DTS. Some suggested that there is a component called, Lookup, Fuzzy Lookup that can be used. Any ideas in optimizing the same are welcome.

USE Database

GO
/****** Object:  StoredProcedure [dbo].[ProcName]    Script Date: 11/13/2010 17:15:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[ProcName] 
AS
BEGIN

 SET NOCOUNT ON;

 DECLARE @not_on_ebc_file_xx## char(2);
    SET @not_on_ebc_file_xx## = 35;

 DECLARE @voters_no varchar(18);
    DECLARE @candidate_id char(10);
 DECLARE @perm_disq_temp char(2);
 DECLARE @voters_no_jms varchar(18);


 DECLARE PROFILES_CURSOR CURSOR LOCAL FAST_FORWARD
    FOR SELECT CP.CANDIDATE_ID, CP.VOTERS_NO FROM PROFILE CP INNER JOIN SOURCE SR ON 
 CP.CANDIDATE_ID = SR.CANDIDATE_ID
 WHERE CP.CANDIDATE_ID NOT LIKE 'MA%';


 OPEN PROFILES_CURSOR;
 FETCH NEXT FROM PROFILES_CURSOR 
 INTO @candidate_id, @voters_no;


  WHILE @@FETCH_STATUS = 0
  BEGIN

   SELECT @voters_no_jms = VOTERS_NO FROM PROFILE_BC WHERE VOTERS_NO = @voters_no;  
   SELECT @perm_disq_temp = PERM_DISQ FROM SOURCE WHERE CANDIDATE_ID = @candidate_id;

   IF (@voters_no_jms = @voters_no)  -- record exists in jms_temp table/ebc file
   BEGIN   
    IF (@perm_disq_temp = @not_on_ebc_file_xx##)
    BEGIN
       UPDATE SOURCE SET PERM_DISQ = '' WHERE CANDIDATE_ID = @candidate_id;
    END

    END
    ELSE
    BEGIN
    IF (@perm_disq_temp = '' OR @perm_disq_temp IS NULL) 
    BEGIN
       UPDATE SOURCE SET PERM_DISQ = @not_on_ebc_file_xx##  WHERE CANDIDATE_ID = @candidate_id;
    END   
    END

   SET @voters_no_jms = '';

   FETCH NEXT FROM PROFILES_CURSOR INTO @candidate_id, @voters_no;

  END 

 CLOSE PROFILES_CURSOR;
 DEALLOCATE PROFILES_CURSOR;

END
  • 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-18T01:14:46+00:00Added an answer on May 18, 2026 at 1:14 am

    you should always try to avoid using cursors if you have performance on mind.
    Instead of using a cursor you could do this. Try to think in terms of sets when dealing with SQL. I commented out the update part of the query and added a select so that you can see the data.

    BEGIN TRANSACTION
    
    DECLARE @not_on_ebc_file_xx## char(2); 
    SET @not_on_ebc_file_xx## = 35;
    
    --UPDATE SR
    --SET       PERM_DISQ = 
    --          CASE WHEN NOT PROFILE_BC.VOTERS_NO IS NULL THEN
    --                  CASE WHEN PERM_DISQ.PERM_DISQ = @not_on_ebc_file_xx## THEN '' 
    --                      ELSE PERM_DISQ.PERM_DISQ 
    --                  END
    --              WHEN PERM_DISQ.PERM_DISQ = '' OR PERM_DISQ   IS NULL THEN @not_on_ebc_file_xx##
    --              ELSE  PERM_DISQ.PERM_DISQ
    --          END
    SELECT  CASE WHEN NOT PROFILE_BC.VOTERS_NO IS NULL THEN
                        CASE WHEN PERM_DISQ.PERM_DISQ = @not_on_ebc_file_xx## THEN '' 
                            ELSE PERM_DISQ.PERM_DISQ 
                        END
                    WHEN PERM_DISQ.PERM_DISQ = '' OR PERM_DISQ   IS NULL THEN @not_on_ebc_file_xx##
                    ELSE  PERM_DISQ.PERM_DISQ
                END AS PERM_DISQ
    FROM    PROFILE CP
            INNER JOIN SOURCE SR
                ON CP.CANDIDATE_ID = SR.CANDID_ID
            LEFT JOIN PROFILE_BC
                ON CP.VOTERS_NO = PROFILE_BC.VOTERS_NO
            LEFT JOIN SOURCE PERM_DISQ
                ON CP.CANDIDATE_ID = PERM_DISQ.CANDIDATE_ID
    WHERE   CP.CANDIDATE_ID NOT LIKE 'MA%';
    
    
    ROLLBACK TRANSACTION;
    

    uncomment the update and set statement and comment out the select statement to update

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

Sidebar

Related Questions

I have designed database tables (normalised, on an MS SQL server) and created a
I have a rather large project developed on Sharepoint and Project Server, designed as
I have Designed the Javascript function My.js it contains following code My.js function display(){
I have a poorly designed database. One of the most important tables has 11,000+
I have a database designed to hold card data from various trading card games.
I have built a CFC designed to serve as a dynamic, aging cache intended
I have a class (class A) that is designed to be inherited by other
I have created a non-visual component in C# which is designed as a placeholder
Question I have an application written in Java. It is designed to run on
Windows Forms allows you to develop Components, non-visual elements that can have a designer.

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.