Pass tenant id through sql server connection

I am creating a multi-tenant application with a common table structure using Microsoft SQL Server.

I wonder if the tenantID parameter can be passed through a connection to the sql server. I don't want to create a separate account for each tenant.

I currently see two ways, via ApplicationName or WorkstationID

Best regards, Alexey Zakharov

+2


a source to share


1 answer


I would use the Application Name of the connection string , which is then easily retrieved in TSQL with APP_NAME (Transact-SQL) .

However, you can also use 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

      

+3


a source







All Articles