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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:02:50+00:00 2026-05-23T00:02:50+00:00

I have a proc that inserts records to a temp table. In pseudocode it

  • 0

I have a proc that inserts records to a temp table. In pseudocode it looks like this:

Create temp table 
a. Insert rows into temp table based on stringent criteria
b. if no rows were inserted, insert based on less stringent criteria 
c. if there are still no rows, try again with even less stringent criteria
select from temp table

There are a lot of IF @@rowcount = 0 checks in the code to see if the table has any rows. The issue is that the proc isn’t really doing what it looks like it should be doing and it’s inserting the same row twice (steps a and c are being executed). However, if I change it to check this way IF ( (select count(*) from @temp) = 0) the proc works exactly as expected.

Which makes me think that @@rowcount isn’t the best solution to this problem. But I’m adding in extra work via the SELECT COUNT(*). Which is the better solution?

  • 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-23T00:02:51+00:00Added an answer on May 23, 2026 at 12:02 am

    @@rowcount is the better solution. The work is already done. Selecting count(*) causes the database to do more work.

    You need to make sure you are not doing something that will affect the value of @@rowcount before checking the value of @@rowcount. It is usually best to check @@rowcount immediately after performing the insert statement. If necessary assign the value to a variable so you can check it later:

    DECLARE @rows int 
    ...
    [insert or update a table]
    SET @rows = @@rowcount
    

    Storing the row count immediately after any operation that changes row count will allow you to use the value more than once.

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

Sidebar

Related Questions

Say i have a stored proc that inserts some data into a table. If
I have a SQL Insert query inside a stored proc, for inserting rows into
I have a stored proc that does inserts of people. I have an xml
I have a stored procedure (see below) which inserts data into a physical table
I have a stored proc that processes a large amount of data (about 5m
I have a Stored Proc that do a validation on a parameter ex. IF
I have a main TCL proc that sources tons of other tcl procs in
So I have a Sybase stored proc that takes 1 parameter that's a comma
Working in SQL Server 2005, I have a stored procedure that inserts a record
I have a rails model with this validation: validates_numericality_of(:insert_oz, :allow_blank=>true, :greater_than=>0, :if=>(proc do |note|

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.