Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to tell if your code is executing inside sp_ExecuteSQL? Expand / Collapse
Author
Message
Posted Monday, January 6, 2014 11:28 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:36 AM
Points: 190, Visits: 442
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.
Post #1528356
Posted Tuesday, January 7, 2014 12:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:04 AM
Points: 5,255, Visits: 12,180
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1528366
Posted Tuesday, January 7, 2014 8:57 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:36 AM
Points: 190, Visits: 442
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.
Post #1528532
Posted Tuesday, January 7, 2014 10:19 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 4:53 AM
Points: 3,422, Visits: 5,368
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!
Post #1528758
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse