Clear OUTPUT buffer in stored procedure

Hello,

I have stored procedures which call other stored procedures for the purpose of reduced points of maintenance. The issue is all the sub stored procedures in the main one also return resultsets. I want to ignore these in my main SP and retrun a single resultselt at the end, which is an error code.

Is it possible to clear the pending resultsets at the end of a storedprocedure? I found how to access the OUTPUT buffer, however there doesn't seem to be a way of clearing it. Has anyone tried this, or got any ideas for how to overcome this?

Thanks,

Mike
LVL 2
beelineukAsked:
Who is Participating?
 
ShogunWadeConnect With a Mentor Commented:
" cannot have  transactions within the sprocs when you do this, so it didn't work."

Ah i can help you there.



@ the start of your procs   check @@NESTLEVEL    this will tell you how deep you are in the "Callstack"  thus if

SET @n=@@NESTLEVEL
IF @n=0
  BEGIN TRAN trnname
ELSE
  CHECKPOINT TRAN trnname


then @ end of proc

IF @n=0
   COMMIT TRAN



thus ising only 1 transaction but checkpointing the transaction at key stages to allow intermediate rollback.
0
 
ShogunWadeCommented:
Perhaps you could explain a bit more.    I dont understand why you are returning resultsets from your stored procs if you dont want them
0
 
beelineukAuthor Commented:
Because these stored procedures can be used independently and also return result codes to the client as required. When used within the main SP this handles the errors seperately and should just return the final result.  
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ShogunWadeCommented:
I think ultimately that you ned to rethink the design a bit.  a stored proc should only return what is required of it.    
0
 
beelineukAuthor Commented:
true, but I'm gaining single point of maintainence, and by solving this problem I will be doing exactly that. Anyway I've now found quite a neat solution.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
How about stuffing that outputs into (local) temp tables?

Example:

create table #one (a varchar(20));
insert into #one exec usp_example;
...
0
 
ShogunWadeCommented:
Kinda off the question a bit,  but there is often big discussions about whether stored procs should be reused or not.   Im of the opinion that in most cases it is better not to reuse.... why ?   mainly because of the amount of times ive seen disasterous results where someone changes a common stored proc without realizing the knock on effects.

But its one of those subjects where there will never be a consensus ;S

0
 
beelineukAuthor Commented:
That was my orginaly get out trick, however you cannot have transactions within the sprocs when you do this, so it didn't work.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I aggree
0
 
beelineukAuthor Commented:
Well it's not far off my solution I also used @@Nestlevel, however I used this to disable the select in the sub stored procedures, rather than the example above which still requires the use of temporary tables.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.