Pass tenant id via sql server connection

会有一股神秘感。 提交于 2019-11-28 02:02:17

问题


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 connection. I don't want to create separate user account for each tenant.

Currently I see two ways: via ApplicationName or WorkstationID

Best regards, Alexey Zakharov


回答1:


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


来源:https://stackoverflow.com/questions/2873104/pass-tenant-id-via-sql-server-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!