Share via

set_session_context error

David Chase 681 Reputation points
2020-09-22T18:48:30.687+00:00

I am getting the error 'Cannot set key 'USERID' in the session context. The key has been set as read_only for this session.' when I run my stored procedure multiple times by looping through an asp.net GridView. I was wondering if there was a way around this error. For example is there a way to remove the set_session_context setting or something?

Below is my sp code that is failing.

ALTER PROCEDURE [dbo].[kd_updtblSupplementsPerPatient]
    (@PatientID     int,
     @SupplementID  int,
     @SuppleValue   real = 0,
     @UserLoginID   int
    )

AS 

    BEGIN

        --Put login userid into session context for use in trigger(s)
        EXEC sp_set_session_context 'USERID', @UserLoginID, @read_only = 1;

        UPDATE dbo.tblSupplementsPerPatient 
           SET [SuppleValue] = @SuppleValue
        WHERE ([PatientID] = @PatientID)
          AND ([SupplementID] = @SupplementID);

    END
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Guoxiong 8,221 Reputation points
2020-09-22T20:57:26.293+00:00

Since the parameter @Rudy Aira _only is set to 1, when you execute sp_set_session_context first time it works. But when you change the value and execute the sp, you will get that error.

-- It works  
EXEC sp_set_session_context 'MyName', 'Test1', @read_only = 1;  
SELECT SESSION_CONTEXT(N'MyName') MyName;  

But when you change the value to 'Test2' and execute it again in the same session, it will fail:

-- It fails  
EXEC sp_set_session_context 'MyName', 'Test2', @read_only = 1;  
SELECT SESSION_CONTEXT(N'MyName') MyName;  

If you set @Rudy Aira _only to 0 or remove that parameter (default 0), it should work.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2020-09-23T07:19:47.2+00:00

    Hi @David Chase ,

    If @Rudy Aira _only = 1, then the value for the specified key cannot be changed again on this logical connection. If 0 (default), then the value can be changed.
    Please try:

     ALTER PROCEDURE [dbo].[kd_updtblSupplementsPerPatient]  
         (@PatientID     int,  
          @SupplementID    int,  
          @SuppleValue     real = 0,  
          @UserLoginID    int  
         )  
          
     AS   
          
         BEGIN  
          
             --Put login userid into session context for use in trigger(s)  
             EXEC sp_set_session_context 'USERID', @UserLoginID, @read_only = 0;  
              
             UPDATE dbo.tblSupplementsPerPatient   
                SET [SuppleValue] = @SuppleValue  
             WHERE ([PatientID] = @PatientID)  
               AND ([SupplementID] = @SupplementID);  
              
         END  
    

    For more details, please refer to:sp_set_session_context (Transact-SQL)

    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. David Chase 681 Reputation points
    2020-09-22T21:34:25.38+00:00

    I will try again tomorrow.

    Was this answer helpful?

    0 comments No comments

  3. David Chase 681 Reputation points
    2020-09-22T21:14:34.34+00:00

    I am testing with my asp.net application.

    Was this answer helpful?


  4. David Chase 681 Reputation points
    2020-09-22T21:06:37.657+00:00

    Removed the parameter and it still failed on same error.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.