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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:07:33+00:00 2026-05-19T14:07:33+00:00

I have a stored procedure that looks up an article based on the article’s

  • 0

I have a stored procedure that looks up an article based on the article’s title. But I also need to increment a column in the same table that counts the number of times the article is viewed.

Trying to be as efficient as possible, I see two possible ways to approach this:

  1. Perform one SELECT to obtain the PK on the target row. Then use that PK to increment the number of views and, finally, another SELECT using the PK to return the article data.

  2. Perform one SELECT to return the article data to my application, and then use the returned PK to make another round trip to the database to increment the number of views.

I know #1 would be pretty fast, but it’s three lookups. And #2 requires two round trips to the database. Is there no way to optimize this task?

EDIT Based on feedback, I came up with the following. Thanks for any comments or constructive criticism.

DECLARE @Slug VARCHAR(250) -- Stored procedure argument

-- declare @UpdatedArticle table variable 
DECLARE @UpdatedArticle TABLE
(
    ArtID INT,
    ArtUserID UNIQUEIDENTIFIER,
    ArtSubcategoryID INT,
    ArtTitle VARCHAR(250),
    ArtHtml VARCHAR(MAX),
    ArtDescription VARCHAR(350),
    ArtKeywords VARCHAR(250),
    ArtLicenseID VARCHAR(10),
    ArtViews BIGINT,
    ArtCreated DATETIME2(7),
    ArtUpdated DATETIME2(7)
);

UPDATE Article
    SET ArtViews = ArtViews + 1
OUTPUT
    INSERTED.ArtID,
    INSERTED.ArtUserID,
    inserted.ArtSubcategoryID,
    INSERTED.ArtTitle,
    INSERTED.ArtHtml,
    INSERTED.ArtDescription,
    INSERTED.ArtKeywords,
    INSERTED.ArtLicenseID,
    INSERTED.ArtViews,
    INSERTED.ArtUpdated,
    INSERTED.ArtCreated
INTO @UpdatedArticle
WHERE ArtSlugHash = CHECKSUM(@Slug) AND ArtSlug = @Slug AND ArtApproved = 1

SELECT a.ArtID, a.ArtUserID, a.ArtTitle, a.ArtHtml, a.ArtDescription, a.ArtKeywords, a.ArtLicenseID,
    l.licTitle, a.ArtViews, a.ArtCreated, a.ArtUpdated, s.SubID, s.SubTitle, c.CatID, c.CatTitle,
    sec.SecID, sec.SecTitle, u.UsrDisplayName AS UserName
    FROM @UpdatedArticle a
    INNER JOIN Subcategory s ON a.ArtSubcategoryID = s.SubID
    INNER JOIN Category c ON s.SubCatID = c.CatID
    INNER JOIN [Section] sec ON c.CatSectionID = sec.SecID
    INNER JOIN [User] u ON a.ArtUserID = u.UsrID
    INNER JOIN License l ON a.ArtLicenseID = l.LicID
  • 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-19T14:07:33+00:00Added an answer on May 19, 2026 at 2:07 pm

    Here is a way using the OUTPUT statement (SQL Server 2005 onwards), in a single update statement:

    IF OBJECT_ID ('Books', 'U') IS NOT NULL
        DROP TABLE dbo.Books;
    
    CREATE TABLE dbo.Books
    (
      BookID int NOT NULL PRIMARY KEY,
      BookTitle nvarchar(50) NOT NULL,
      ModifiedDate datetime NOT NULL,
      NumViews int not null CONSTRAINT DF_Numviews DEFAULT (0)
    );
    
    INSERT INTO dbo.Books
      (BookID, BookTitle, ModifiedDate) 
    VALUES
      (106, 'abc', GETDATE()),
      (107, 'Great Expectations', GETDATE());
    
    
    -- declare @UpdateOutput1 table variable 
    DECLARE @UpdateOutput1 table
    (
      BookID int,
      BookTitle nvarchar(50),
      ModifiedDate datetime,
      NumViews int 
    );
    
    
    -- >>>>  here is the update of Numviews and the Fetch
    -- update Numviews in Books table, and retrive the row
    UPDATE Books
    SET 
      NumViews = NumViews + 1
    OUTPUT
        INSERTED.BookID,
        INSERTED.BookTitle,
        INSERTED.ModifiedDate,
        INSERTED.NumViews
      INTO @UpdateOutput1
    WHERE BookID = 106
    
    -- view updated row in Books table
    SELECT * FROM Books;
    
    -- view output row in @UpdateOutput1 variable
    SELECT * FROM @UpdateOutput1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have a stored procedure that looks something like: CREATE PROCEDURE my_procedure @val_1 INT,
I have a stored procedure in SQL server that looks something like this: insert
I have a stored procedure that returns a value, not a dataset, and I
I have a stored procedure that is called to validate a user during login.
I have a stored procedure that returns many columns and is very cumbersome to
All, I am very new to stored procedures in general but I am struggling
(In .NET) I have arbitrary binary data stored in in a byte[] (an image,
the procedure: i have a script which uploads images (via hmtl-form and php) for
So I am trying to work around the lack of stored procedure support in

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.