How to execute SP_SET_SESSION_CONTEXT from dataflow

Arunkumar 0 Reputation points
2023-03-22T11:49:13.5366667+00:00

Hi everyone,

In my database i have RLS setup using session variables. i need to execute "EXEC SP_SET_SESSION_CONTEXT" every time before getting data. based on the session variables values SQL server return the data. How can I execute "EXEC SP_SET_SESSION_CONTEXT" in ADF dataflow. Please anybody help me out on this.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
9,477 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KranthiPakala-MSFT 46,422 Reputation points Microsoft Employee
    2023-03-28T06:17:34.5566667+00:00

    Hi @Arunkumar ,

    Welcome to Microsoft Q&A forum and thanks for reaching out here.

    In order to execute "EXEC SP_SET_SESSION_CONTEXT" every time before getting data, you will need to create a parent store procedure which contains the EXEC SP_SET_SESSION_CONTEXT and your actual query using those variables from the session context as shown below.

    User's image

    Here is an example script for above:

    CREATE PROCEDURE sp_RLSSessionExecution @inputValue int
    AS
    Begin
    EXECUTE AS USER = 'AppUser';
    EXEC sp_set_session_context @key=N'UserId', @value=@inputValue;
    SELECT * FROM Sales;
    End
    

    Then In data flow source, go to Source options and for input select Stored procedure and in the drop down select the parent stored procedure you have created in the above step and if you have any procedure parameters you can hard code the values or you can use dynamic content option to pass values dynamically from dataflow parameters.

    User's image

    Hope this info helps.


    Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments