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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:13:59+00:00 2026-06-05T08:13:59+00:00

I am a very beginner with sql and I need to write a SP

  • 0

I am a very beginner with sql and I need to write a SP in order to increment products views count. When user searches on site we want to increment the counter for all products that were returned by search. I see two problems with my SP:

  1. it uses cursor
  2. it starts a lot of transactions

The SP is called simultaneous by many threads. After I implemented it I got many timeout exceptions. My count table looks like this:

ProductsViewsCount(ProductId int, Timestamp datetime, ViewType int, Count int)

Tiemstamp column is rounded to the closest hour in .net code that calls the SP. Basically I will count the views by hour.

The SP looks like this:

    CREATE PROCEDURE [dbo].[IncrementProductsViews]
    -- Add the parameters for the stored procedure here
    @ProductsIds as varchar(max) = '', --CSV ids of products that were returned by search
    @ViewType int,
    @Timestamp datetime
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @id int 
    DECLARE idsCursor CURSOR FOR 
        SELECT Data FROM dbo.Split(@ProductsIds,',')

    OPEN idsCursor
    FETCH NEXT FROM idsCursor INTO @id
    WHILE @@FETCH_STATUS = 0
    BEGIN
        BEGIN TRAN
            UPDATE dbo.ProductsViewsCount SET Count = Count + 1 
                WHERE ProductId = @id AND ViewType = @ViewType AND Timestamp = @Timestamp
            if @@rowcount = 0
            BEGIN
                INSERT INTO dbo.ProductsViewsCount (ProductId, Timestamp, ViewType, Count) 
                    VALUES (@id, @Timestamp, @ViewType, 1)
            END
        COMMIT TRAN     
        FETCH NEXT FROM idsCursor INTO @id
    END     
    CLOSE idsCursor   
    DEALLOCATE idsCursor
    select 1
END

Can I do this in a more efficient manner?

  • 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-05T08:14:00+00:00Added an answer on June 5, 2026 at 8:14 am

    You can do it on a set operation instead of a cursor:

    CREATE PROCEDURE [dbo].[IncrementProductsViews]
        -- Add the parameters for the stored procedure here
        @ProductsIds as varchar(max) = '', --CSV ids of products that were returned by search
        @ViewType int,
        @Timestamp datetime
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        ;WITH CTE AS
        (
            SELECT *
            FROM dbo.ProductsViewsCount
            WHERE ViewType = @ViewType AND [Timestamp] = @Timestamp
        )
    
    
        MERGE CTE AS A
        USING (SELECT * FROM dbo.Split(@ProductsIds,',')) B
        ON A.ProductId = B.Data 
        WHEN MATCHED THEN UPDATE SET A.[Count] = B.[Count] + 1
        WHEN NOT MATCHED BY TARGET THEN
        INSERT(ProductId, [Timestamp], ViewType, [Count])
        VALUES(Data, @Timestamp, @ViewType, 1);
    
        SELECT 1 -- I don't know why this is here
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very much a beginner to rails and I have a specific need
I am very beginner With Programming...(unfortunately) I want to remove Any added QueryString To
I'm a very beginner in C. However, I need a program that solves a
i am very beginner in visual basic and i have 2010 express. i want
I am only a very beginner user of git and have a C# Visual
I am very beginner in SQL Server 2005 and I am learning it from
very beginner question. I am using Rails 3's query interface as shown: class User
Very beginner's question: I noticed that there's no .to_d (such as order.price.to_d to convert
I'm very beginner for developping. environment is .. Windows XP Professional SP3 Microsoft Visual
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET

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.