January 6, 2014 at 11:28 pm
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...
January 7, 2014 at 12:22 am
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.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
January 7, 2014 at 8:57 am
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).
January 7, 2014 at 10:19 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply