What's going on?

  • Comments posted to this topic are about the item What's going on?

  • Good question, thanks Steve.

    ...

  • My thought is that using substring to grab the relevant bytes is more appropriate than implicitly doing so via convert.
    😎

    Thanks for the question Steve.

    Here is a spanner in the works

    DECLARE @CI BIGINT = 1234567890
    SET CONTEXT_INFO @CI
    GO

  • Nice one to start the week on, thanks Steve

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    β€œlibera tute vulgaris ex”

  • Interesting is specified this question, a long time I decided to answer.
    If I run the code given in the Explanation, the result is number 12 and it looks like the answer that is mentioned in the possible answers.The result of this code:
    DECLARE @BinRepre BINARY (128) = 12;
    SET CONTEXT_INFO @BinRepre;
    SELECT CONVERT (INT, CONTEXT_INFO ()) AS MyContextInfo;
    GO

    is also number 12, so this may be considered as a second different possible answer.
    I was lucky, I made the right choice...πŸ˜‰

  • If that one is correct, then the first option is also correct πŸ˜‰

    The short lesson for all of these context_info questions is surely not to rely on any default behaviour but to explicitly set and read the values in binary format

  • George Vobr - Monday, April 9, 2018 8:01 AM

    The result of this code:
    DECLARE @BinRepre BINARY (128) = 12;
    SET CONTEXT_INFO @BinRepre;
    SELECT CONVERT (INT, CONTEXT_INFO ()) AS MyContextInfo;
    GO

    is also number 12, so this may be considered as a second different possible answer.

    Hi George. Yes, what you are seeing in your example is correct.

    I was going to recommend to Steve to update the explanation to be a little clearer as to the root cause of the behavior. However, as I was typing up what I had thought would be a brief explanation, I realized that it wasn't going to be quick and easy. The problem is really a combination of 3 different things, one of which is definitely the CONTEXT_INFO() built-in function and how it always returns 128 bytes, even though the returned datatype is still VARBINARY (and not BINARY).

    So, I just wrote it up as a blog post, which also made it easier to format the code, etc:

    Why Doesn’t CONTEXT_INFO() Return the Exact Value Set by SET CONTEXT_INFO?

    I originally had mentioned at the top of that post that the idea came from this question, with a link back to the question, but given that it came out today I didn't want to appear like I was trying to give some folks the answer. Hopefully that was the appropriate way to handle this. Eventually I will update it to include the link back to this question.

    Take care,
    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Solomon Rutzky - Monday, April 9, 2018 12:01 PM

    George Vobr - Monday, April 9, 2018 8:01 AM

    The result of this code:
    DECLARE @BinRepre BINARY (128) = 12;
    SET CONTEXT_INFO @BinRepre;
    SELECT CONVERT (INT, CONTEXT_INFO ()) AS MyContextInfo;
    GO

    is also number 12, so this may be considered as a second different possible answer.

    Hi George. Yes, what you are seeing in your example is correct.

    I was going to recommend to Steve to update the explanation to be a little clearer as to the root cause of the behavior. However, as I was typing up what I had thought would be a brief explanation, I realized that it wasn't going to be quick and easy. The problem is really a combination of 3 different things, one of which is definitely the CONTEXT_INFO() built-in function and how it always returns 128 bytes, even though the returned datatype is still VARBINARY (and not BINARY).

    So, I just wrote it up as a blog post, which also made it easier to format the code, etc:

    Why Doesn’t CONTEXT_INFO() Return the Exact Value Set by SET CONTEXT_INFO?

    I originally had mentioned at the top of that post that the idea came from this question, with a link back to the question, but given that it came out today I didn't want to appear like I was trying to give some folks the answer. Hopefully that was the appropriate way to handle this. Eventually I will update it to include the link back to this question.

    Take care,
    Solomon...

    Thanks Solomon for a very good detailed explanation. Today, I also very much appreciated your notes and examples of data conversion in a QotD-discussion on a similar issue on February 13, 2018.
    Thank you again.
    G. V.

  • George Vobr - Monday, April 9, 2018 5:20 PM

    Thanks Solomon for a very good detailed explanation. Today, I also very much appreciated your notes and examples of data conversion in a QotD-discussion on a similar issue on February 13, 2018.

    Thank you again.
    G. V.

    Hi George. You are quite welcome. And I had completely forgotten about that other QotD, thanks for mentioning it. I incorporated part of one of my comments from that other QotD into my blog post. Now the post includes two cases when this problem does not happen πŸ™‚ . I also added some explanation towards the end to show step-by-step by the initial outcome is `0`.

    Take care,
    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply