SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to tell if your code is executing inside sp_ExecuteSQL?


How to tell if your code is executing inside sp_ExecuteSQL?

Author
Message
Peter E. Kierstead
Peter E. Kierstead
SSC-Addicted
SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)

Group: General Forum Members
Points: 426 Visits: 453
I've got a development scenario where stored procedures (sprocs) will output diagnostic/debug messages to the SSMS console during execution via a common dbo.DBPrint sproc. As some sprocs may call other sprocs it would be nice, for readability, to indent, in dbo.DBPrint, the diagnostic/debug messages according to the call nesting level. The problem I'm running into can be elucidated by running the following code and observing its unexpected, yet documented, results:


Print @@NestLevel;
Exec('Print @@NestLevel;');
Exec sp_ExecuteSQL N'Print @@NestLevel;';

/* Output results
0
1
2
*/



I've even tried running the sp_ExecuteSQL N'Print @@NestLevel' by itself to see if the Exec was generating the extra nesting level; nope... sp_ExecuteSQL is just being difficult...

Ideally, the dbo.DBPrint code would somehow need to "know" that it was running inside an sp_ExecuteSQL context so as not to double-indent its output relative to the previous calling level's output. Call-level indentation works fine with scripts & sprocs that don't use sp_ExecuteSQL (i.e., Exec(...) only generates the expected additional nesting level).

Most of you will correctly state that "this ain't that big of a deal" and I'd logically say that you are correct, however, this has turned out to be one of those things that is "keeping me up at night" so to speak, for an elegant any nasty kludge I can find to get it to work!

Any possible kludge solution you guys can come up with must work entirely from within the dbo.DBPrint sproc, i.e., you can't pass anything in to give it a hint as its not always possible to tell what environment its being called from (so you'd know to pass the hint in).

Baring a solution I only hope that my there's-gotta-be-a-kudgy-T-SQL-trick insomnia isn't contagious...



PeteK
I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18676 Visits: 20451
I can't see any way of doing this directly, without using an additional param.

But logically, within your proc, can you assume that the nesting level can either:

1) Remain the same
2) Decrement by one to a minimum of zero
3) Increment by one

?

If yes, maybe you can handle it by restricting the magnitude of the jumps in the nesting level using brute-force coding.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Peter E. Kierstead
Peter E. Kierstead
SSC-Addicted
SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)

Group: General Forum Members
Points: 426 Visits: 453
The sproc only has access to its value of @@NestLevel; there is no "external" or other context to compare it to so it doesn't know about any differential (magnitude of the jumps). With the exception of using some form of table MSSQL goes out of its way to prevent the user from maintaining any context info across sproc/function calls and batch transitions.

Even if dbo.DBPrint could store some persistent level-related context it still wouldn't solve my problem as there may be intervening call levels that don't use dbo.DBPrint so it wouldn't have a chance to "know" and "remember" those.

The real solution is for sp_ExecuteSQL to handle @@NestLevel logically (or at least explain why it apparently doesn't in BOL!).

At this point I've grudgingly settled for there being a two-level indent jump at some points in the output stream (where something has used sp_ExecuteSQL to continue the call chain).



PeteK
I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7381 Visits: 6431
Does this give you the output you're looking for?


Print REPLICATE(' ', @@NestLevel) + CAST(@@NestLevel AS VARCHAR);
Exec('Print REPLICATE('' '', @@NestLevel) + CAST(@@NestLevel AS VARCHAR);');
Exec sp_ExecuteSQL N'Print REPLICATE('' '', @@NestLevel) + CAST(@@NestLevel AS VARCHAR);';





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search