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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:33:17+00:00 2026-06-12T09:33:17+00:00

I’m taking over some code that is trying to store a list of IDs

  • 0

I’m taking over some code that is trying to store a list of IDs at one time and I find this code to be running quite slow for the actions we are trying to complete. Plus, in certain occasions resulting in deadlocks due to high amounts ids.

USE [store]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateImagePriority]

   @separator CHAR(1),
   @filename  varchar(50),
   @parentId   int,
   @slaveIds varchar(8000)

   AS
   BEGIN      
      SET NOCOUNT ON
      DECLARE @SLAPriorityint
      DECLARE @separator_position INT 
      DECLARE @array_value VARCHAR(50)

      SET @slaveIds = @slaveIds + @separator
      SET @SLAPriority= 0

      WHILE PATINDEX('%' + @separator + '%', @slaveIds ) <> 0
           BEGIN

               SET @SLAPriority= @SLAPriority+ 1

               SELECT  @separator_position = PATINDEX('%' + @separator + '%',@slaveIds )
               SELECT  @array_value = LEFT(@slaveIds , @separator_position - 1)

               SELECT  Array_Value = @array_value

               SELECT  @slaveIds = STUFF(@slaveIds , 1, @separator_position, '')

               UPDATE image_info
               SET SLA_PRIORITY = @SLAPriority
               WHERE FILE=@filename and EXT_PAR_ID=@parentId   and SLA_ID=@array_value
           END
      SET NOCOUNT OFF
   END

This is a sample of what we would pass in:

e.g.

separator = ','
filename = 'burgerking'
parentId = '1859'
slaveIds = '15,16,19,20,21,25,28,29,30,38,99'

Any suggestions on how to improve the speed of this code.

Thanks in advance!

  • 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-12T09:33:18+00:00Added an answer on June 12, 2026 at 9:33 am

    What you are looking for is a table-valued function to split your values into a table. Then all you need is a single UPDATE .. FROM .. JOIN statement.

    CREATE PROCEDURE [dbo].[UpdateImagePriority]
       @separator CHAR(1),
       @filename  varchar(50),
       @parentId   int,
       @slaveIds varchar(8000)
    AS
    
    set @slaveIds = @slaveIds + @separator
    ;WITH SplitString AS
    (
        SELECT
            1 ID,LEFT(@slaveIds,CHARINDEX(',',@slaveIds)-1) AS Part,RIGHT(@slaveIds,LEN(@slaveIds)-CHARINDEX(',',@slaveIds)) AS Remainder
        UNION ALL
        SELECT
            ID+1,LEFT(Remainder,CHARINDEX(',',Remainder)-1),RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
            FROM SplitString
            WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
    )
    update i
    SET SLA_PRIORITY = s.ID
    from splitstring s
    join image_info i on i.[FILE]=@filename and i.EXT_PAR_ID=@parentId and i.SLA_ID= s.Part
    where s.Part > ''
    

    For SQL Server 2000, or just to make the string splitting re-usable, I lifted this function from another question.

    create function dbo.SplitString 
        (
            @str varchar(8000), 
            @separator char(1)
        )
        returns table
        AS
        return (
            with tokens(p, a, b) AS (
                select 
                    1, 
                    1, 
                    charindex(@separator, @str)
                union all
                select
                    p + 1, 
                    b + 1, 
                    charindex(@separator, @str, b + 1)
                from tokens
                where b > 0
            )
            select
                p Id,
                substring(
                    @str, 
                    a, 
                    case when b > 0 then b-a ELSE 8000 end) 
                AS Part
            from tokens
          )
    GO
    

    Then your SP becomes

    CREATE PROCEDURE [dbo].[UpdateImagePriority]
       @separator CHAR(1),
       @filename  varchar(50),
       @parentId   int,
       @slaveIds varchar(8000)
    AS
    
    update i
    SET SLA_PRIORITY = s.ID
    from dbo.splitstring(@slaveIds,@separator) s
    join image_info i on i.[FILE]=@filename and i.EXT_PAR_ID=@parentId and i.SLA_ID= s.Part
    where s.Part > ''
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.