Pass tenant id via sql server connection

后端 未结 1 1569
面向向阳花
面向向阳花 2020-12-18 15:01

I\'m building multi tenant application with shared table structure using Microsoft SQL Server.

I wonder if it possible to pass tenantID parameter via sql server conn

1条回答
  •  悲哀的现实
    2020-12-18 15:39

    I would use the Application Name of the connect string, which is then easy to get at in TSQL with APP_NAME (Transact-SQL).

    However, you could also consider using CONTEXT_INFO (Transact-SQL).

    --to set value
    DECLARE @CONTEXT_INFO  varbinary(128)
    SET @CONTEXT_INFO =cast('Anything Here!!'+REPLICATE(' ',128) as varbinary(128))
    SET CONTEXT_INFO @CONTEXT_INFO
    
    
    --to use value
    IF CAST(CONTEXT_INFO() AS VARCHAR(128))='Anything Here!'
    BEGIN
        --do something
    END
    

    0 讨论(0)
提交回复
热议问题