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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:29+00:00 2026-05-15T06:31:29+00:00

Okay so I’m writing a SQL Server 2008 Stored Procedure (maintenance script). In doing

  • 0

Okay so I’m writing a SQL Server 2008 Stored Procedure (maintenance script).

In doing so, being a good boy I’ve done plenty of error handling, checking rowcounts, printing output messages, etc

But in doing this, I’ve found myself writing over and over again something like this:

SELECT @RowsAffected = @@ROWCOUNT
IF @RowsAffected > 0
BEGIN
   PRINT CAST(@RowsAffected, NVARCHAR(2)) + 'rows updated.'
END

Or debug messages like this:

PRINT 'User ' + CAST(@UserId AS NVARCHAR(5)) + ' modified successfully'

Is there a way I can create a kind of ‘subroutine’ inside the stored procedure (like a private method) that can accept something as a parameter (doesn’t have to though) and do some logic?

I want to be able to do something like this:

CheckRowCounts

Or this:

PrintUserUpatedMessage(@UserId)

Which would then perform the above logic (check rowcount, print message, etc)

And yes obviously I can create a UDF, but then i would need to create/drop it etc as this logic is only required for the life of the execution of this stored procedure.

Getting sick and tired of writing the same code over and over again, and changing all the different areas I’ve used it when I get an error =)

Can anyone help?

EDIT

Ok so I ended up creating a scalar UDF function (seems only way).

However, I have awarded the correct answer to Fredrik as although I don’t plan to implement this, it is both a correct answer and a creative one at that.

Thanks for all the advice/help.

  • 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-15T06:31:30+00:00Added an answer on May 15, 2026 at 6:31 am

    I first tried to create another, temporary SP, from within an existing SP – which didn’t work, but after experimenting a bit I think you could go with something like this (if you don’t mind dynamic SQL):

    CREATE PROCEDURE sp_myTest_v1_0(@firstName NVARCHAR(255)) AS
    BEGIN
        -- declare private method
        DECLARE @privateMethod NVARCHAR(255), @privateMethodSig NVARCHAR(255)
        SELECT @privateMethod = 
            'DECLARE @x INT' + CHAR(10) +
            'WHILE ISNULL(@x,0) < 10 BEGIN' + CHAR(10) +
                'PRINT @param1 + CAST(@x AS VARCHAR)' + CHAR(10) +
                'SET @x = ISNULL(@x,0)+1' + CHAR(10) +
            'END', @privateMethodSig = '@param1 NVARCHAR(255)'
    
        -- call privateMethod
        EXEC sp_executesql @privateMethod, @privateMethodSig, @param1 = @firstName
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have an extra colon ';' after your if. if… May 16, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer Actually the apk file is just a zip archive, so… May 16, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer Let the form submit to a Servlet which sends a… May 16, 2026 at 11:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Okay, I've looked all over the internet for a good solution to get PHP
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay, not sure what I'm doing here, other than it's not right. Trying to
Okay, so I'm running a small test webserver on my private network. I've got
Okay so im working on this php image upload system but for some reason
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay, so I'm making a table right now for Box Items. Now, a Box
Okay. I know this looks like the typical Why didn't he just Google it

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.