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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:55:56+00:00 2026-05-29T19:55:56+00:00

I’m getting started with my first use of a cursor in a stored procedure

  • 0

I’m getting started with my first use of a cursor in a stored procedure in sql server 2008. I’ve done some preliminary reading and I understand that they have significant performance limitations. In my current case I think they’re necessary (I want to run multiple stored procedures for each stock symbol in a symbols table.

Edit:
The sprocs I’ll be calling on each symbol will for the most part be insert operations to calculate symbol- dependent values, such as 5 day moving average, average daily volume, ATR (average true range). Most of these values will be calculated from data from a daily pricing and volume table… I’d like to streamline the retrieval of data values that would be retrieved redundantly otherwise… for example, I’d like to get for each symbol the daily pricing and volume data into a table variable… that temp table will then be passed in to the stored procedure that calls each of the aggregated functions I just mentioned. Hope that makes sense…

So my initial “outer loop” cursor- based stored procedure is below.. it times out after several minutes, without returning anything to the output window.

ALTER PROCEDURE dbo.sprocSymbolDependentAggsDriver2

    AS

    DECLARE @symbol nchar(10)
    DECLARE symbolCursor CURSOR
    STATIC FOR
    SELECT Symbol FROM tblSymbolsMain ORDER BY Symbol

    OPEN symbolCursor
    FETCH NEXT FROM symbolCursor INTO @symbol
    WHILE @@FETCH_STATUS = 0
        SET @symbol = @symbol + ': Test.'
        FETCH NEXT FROM symbolCursor INTO @symbol

    CLOSE symbolCursor
    DEALLOCATE symbolCursor

When I run it without the @symbol local variable and eliminate the assignment to it in the while loop, it seems to run ok. Is there a clear violation of performance best- practices within that assignment? Thanks..

  • 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-29T19:56:02+00:00Added an answer on May 29, 2026 at 7:56 pm

    You don’t really need all that explicit cursor jazz to build a string. Here is probably a more efficient way to do it:

    DECLARE @symbol NVARCHAR(MAX) = N'';
    
    SELECT @symbol += ': Test.'
        FROM dbo.tblSymbolsMain 
        ORDER BY Symbol;
    

    Though I suspect you actually wanted to see the names of the symbol, e.g.

    DECLARE @symbol NVARCHAR(MAX) = N'';
    
    SELECT @symbol += N':' + Symbol
        FROM dbo.tblSymbolsMain 
        ORDER BY Symbol;
    

    One caveat is that while you will typically observe the order to be observed, it is not guaranteed. So if you want to stick to the cursor, at least declare the cursor as follows:

    DECLARE symbolCursor CURSOR
    LOCAL STATIC READ_ONLY FORWARD_ONLY
    FOR
    ...
    

    Also it seems to me like NCHAR(10) is not sufficient to hold the data you’re trying to stuff into it, unless you only have one row (which is why I chose NVARCHAR(MAX) above).

    And I agree with Abe… it is quite possible you don’t need to fire a stored procedure for every row in the cursor, but to suggest ways around that (which will almost certainly be more efficient), we’d have to understand what those stored procedures actually do.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.