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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:51:49+00:00 2026-06-05T20:51:49+00:00

I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable

  • 0

I have the following SQL Server stored procedure :

BEGIN TRAN
CREATE TABLE #TempTable (
                            SampleOrderID int,
                            SampleOrderNo varchar(512),
                            ChallanNoAndChallanDate varchar(MAX)
                        )
CREATE NONCLUSTERED INDEX #IX_Temp2_1 ON #TempTable(SampleOrderID)

DECLARE 
@SQL as varchar(MAX)
SET @SQL='      SELECT  SampleOrderID,  SampleOrderNo FROM SampleOrder WHERE SampleOrderID IN (37808,37805,37767,37571,37745,37772,37843,37394,37909,37905,37903) '
INSERT INTO #TempTable  (SampleOrderID, SampleOrderNo)
EXEC (@SQL)

DECLARE 
@SampleOrderID as int,
@ChallanNoAndChallanDate as varchar(max)

DECLARE Cur_AB1 CURSOR GLOBAL FORWARD_ONLY KEYSET FOR           
SELECT  SampleOrderID FROM #TempTable
OPEN Cur_AB1
FETCH NEXT FROM Cur_AB1 INTO @SampleOrderID
WHILE(@@Fetch_Status <> -1)
    BEGIN--2
        SET @ChallanNoAndChallanDate=''
        SELECT @ChallanNoAndChallanDate= COALESCE(@ChallanNoAndChallanDate+ ',', '') + CONVERT(VARCHAR(12),ChallanDate,106)+':'+ChallanNo  FROM Challan WHERE OrderID =@SampleOrderID AND OrderType=2

        UPDATE #TempTable SET ChallanNoAndChallanDate=@ChallanNoAndChallanDate WHERE SampleOrderID=@SampleOrderID   
        FETCH NEXT FROM Cur_AB1 INTO @SampleOrderID
    END--2
CLOSE Cur_AB1
DEALLOCATE Cur_AB1

SELECT * FROM #TempTable
DROP TABLE #TempTable
COMMIT TRAN 

Output :

SamID       SamNo    ChallanNoAndDaet
37394   37394   ,31 May 2012:151592
37571   37571   ,31 May 2012:151580
37745   37745   ,31 May 2012:151582
37767   37767   ,30 May 2012:151507,31 May 2012:151576
37772   37772   ,31 May 2012:151587
37805   37805   ,31 May 2012:151574
37808   37808   ,31 May 2012:151573
37843   37843   ,31 May 2012:151588
37903   37903   ,31 May 2012:151597
37905   37905   ,31 May 2012:151596
37909   37909   ,31 May 2012:151593

It works successfully for small volume of data but When i try to execute it on a Large volume (i.e. more then 500,000 record) my C# interface throws the time out exception.

Can anyone help me edit my stored procedure to avoid the cursor?

Thanks for response.

  • 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-06-05T20:51:51+00:00Added an answer on June 5, 2026 at 8:51 pm

    I usually use something like the following:

    SELECT @SampleOrderID = MIN (SampleOrderID) FROM #TempTable
    WHILE @SampleOrderID IS NOT NULL
    BEGIN
    
       SET @ChallanNoAndChallanDate=''
       SELECT @ChallanNoAndChallanDate= COALESCE(@ChallanNoAndChallanDate+ ',', '') + CONVERT(VARCHAR(12),ChallanDate,106)+':'+ChallanNo  FROM Challan WHERE OrderID =@SampleOrderID AND OrderType=2
    
       UPDATE #TempTable SET ChallanNoAndChallanDate=@ChallanNoAndChallanDate WHERE SampleOrderID=@SampleOrderID   
    
       SELECT @SampleOrderID = MIN (SampleOrderID) FROM #TempTable WHERE SampleOrderID > @SampleOrderID 
    
    END
    

    This code would replace the cursor stuff you have.

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

Sidebar

Related Questions

I have the following sniplet of a stored procedure in SQL Server Create PROCEDURE
i have the following stored procedure in sql server and i am trying to
Hi i have the following stored proc in SQL Server 2005: ALTER PROCEDURE [dbo].[slot_sp_EngineerTimeslots_Group]
I have this Stored procedure on my sql server : [InsertRecord] @param1 varchar(10), @Param2
I have the following stored procedure in an SQL Server 2005 database (meant simply
I have the following requirements I have relational content stored in a SQL Server
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id

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.