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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:33:44+00:00 2026-05-17T23:33:44+00:00

I am almost finished changing a cursor-based stored procedure over to set-based. Almost, because

  • 0

I am almost finished changing a cursor-based stored procedure over to set-based. Almost, because I have only one thing left to figure out.

They use a stored procedure called GetSequence to query a table, update it with a new sequence number (old + 1) and return the new sequence number value. This wasn’t an issue when they used cursors because they assigned the output value to a variable, then used the variable.

The only way I can think of to keep the new stored procedure set based is to execute GetSequence in an INSERT or UPDATE statement. However, I get that wonderfully specific error, “Incorrect syntax near the keyword ‘EXEC'”, when I try that.

This is the old code:

  DECLARE @new_UD_campaignID BIGINT -- Get the new ud_lead_id for the new lead set
  EXEC ppGlobal.dbo.Getsequence
    'ud_campaign_id',
    @new_UD_campaignID OUTPUT
  DECLARE @OrderNum VARCHAR(9);
  IF @corpCamp LIKE '%LEP%'
    BEGIN
        SELECT @OrderNum = ( 'L' + RIGHT('00000000' + CAST(@new_UD_campaignID AS VARCHAR(8)), 8) )
    END
  ELSE
    BEGIN
        SELECT @OrderNum = ( 'C' + RIGHT('00000000' + CAST(@new_UD_campaignID AS VARCHAR(8)), 8) )
    END

This works, but is really slow because it is in a cursor and updating over two million rows.

The new code I am trying looks like this:

  UPDATE @List
  SET   OrderNumBigInt = EXEC (ipCore.dbo.Getsequence
                                     'ud_campaign_id',
                                     @new_UD_campaignID OUTPUT)

I can’t find any specific documentation indicating that you cannot execute a stored procedure within a SELECT or UPDATE statement to set a column value.

Has anyone tried something similar, but with success?

  • 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-17T23:33:45+00:00Added an answer on May 17, 2026 at 11:33 pm

    What you’re suggesting can’t be done in MSSQL (AFAIK). In fact, I doubt the suggestions to convert GetSequence into a function probably won’t work either as the latest ud_campaing_id probably is stored in some “global” table…

    Assuming the GetSequence stored procedure is called by different processes “simultaneously”, I’d suggest you’d either

    • need to adapt said sp so you can ask for a bunch of codes at once (extra parameter, eg. @number_of_ids which defaults to 1) so that the output parameter returns the first id requested but internally also reserves the next n ones for you which you then can use to update your @list
    • need to create a tight loop that gets you the number of a id’s and then apply these in one go to your target table.

    Although I’m most certainly in favour of the former solution, it requires changes to what seems to be a very core stored procedure, something the dba’s might not like or allow. Nevertheless, it would make things MUCH faster.
    The second solution still requires some looping, and also has some serious indexing-requirements when applying the resulting data to the end-table so it’s far from perfect but might at least be a bit faster than looping directly over the target table and fetching and applying the new data record by record.

    Judging on the UPDATE @list approach you’re using I think you’re already on track for the second suggestion.
    Assuming you have an identity field in @list (with a UNIQUE OR PK constraint on it and no gaps), you might try something along these lines :

    DECLARE @RecordID, @LastRecordID int
    DECLARE @new_UD_campaignID bigint
    
    SELECT @RecordID = Min(RecordID), 
           @LastRecordID = Max(RecordID)
      FROM @list
    
    DECLARE @newCampaingIDs TABLE (RecordID int PRIMARY KEY, new_UD_campaignID varchar(8))
    
    WHILE @RecordID <= @LastRecordID
        BEGIN
    
            EXEC ppGlobal.dbo.Getsequence 'ud_campaign_id', @new_UD_campaignID OUTPUT
    
            INSERT @newCampaingIDs (RecordID, new_UD_campaignID) VALUES (@RecordID, RIGHT('00000000' + CAST(@new_UD_campaignID AS VARCHAR(8)), 8))
    
            SELECT @RecordID = @RecordID + 1
        END
    
    UPDATE @list
       SET OrderNum = (CASE WHEN corpCamp LIKE '%LEP%' THEN 'L' ELSE 'C' END) + new_UD_campaignID
      FROM @list upd
      JOIN @newCampaingIDs new
        ON new.RecordID = upd.RecordID
    

    The reason I think this will be faster is because the sequential inserts will have (a lot?) less overhead than updating the original table record by record. Then again, you’re still stuck behind the repeatedly calling of the GetSequence stored proc which might be your major time consumer.

    Anyway, the only way to know for sure is by testing it =)

    Good luck.

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

Sidebar

Related Questions

I'm almost finished with my program, but I have one problem that I can't
I have been almost finished (well i thought i finished) writing this login page
I'm almost finished with the book Head First Java. The reason I'm studying this
I've almost finished my Data Mapper, but now I'm at the point where it
I'm almost finished working on a useful (at least, in my opinion) JavaScript plugin.
I am almost finished a contract right now, and it appears at the last
I've almost finished my Java p2p file sharing application and given that it's pretty
I'm almost finished developing my large project, however I would love it if I
Almost every Java book I read talks about using the interface as a way
Almost 5 years ago Joel Spolsky wrote this article, The Absolute Minimum Every Software

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.