[英]Creating temp table on logon in SQL Server
Is it possible to have SQL Server create a temp table inside a particular database upon a user connecting to the database in such a way that the connecting user is the only one with access to the contents in this table (or even better, the connecting user is the only one that can even see the table)?是否可以让 SQL 服务器在用户连接到数据库时在特定数据库中创建一个临时表,以便连接用户是唯一可以访问该表中内容的用户(或者更好的是,连接用户是唯一一个甚至可以看到桌子的人)?
I tried using a logon trigger (including using a 'with execute as caller' clause) but although this creates the temp table, the connecting user can never see it/select from it.我尝试使用登录触发器(包括使用'with execute as caller'子句),但尽管这会创建临时表,但连接用户永远无法看到它/从中选择。 All of this has to run inside SQL Server and require no user interaction at all...
所有这些都必须在 SQL 服务器内部运行,并且根本不需要用户交互......
Basically, this is the scenario I want to support:基本上,这是我想要支持的场景:
Thanks谢谢
First thoughts:第一个想法:
And what I'd probably do:我可能会做的事情:
Using temp tables for this would not be the right approach if your data access is properly designed to open a connection-do an operation/query-close the connection.如果您的数据访问被正确设计为打开连接-执行操作/查询-关闭连接,则为此使用临时表将不是正确的方法。 The moment you closed the connection, the temp table would be destroyed.
关闭连接的那一刻,临时表将被破坏。 It would be better to use a view or stored procedure to filter the information to which the user should have access.
最好使用视图或存储过程来过滤用户应该有权访问的信息。 The structure of that view will depend greatly on how users connect to the database.
该视图的结构很大程度上取决于用户如何连接到数据库。 Do users connect to the database using their own personal windows authentication account or do they connect indirectly through another account like many web servers do?
用户是使用自己的个人 windows 身份验证帐户连接到数据库,还是像许多 web 服务器那样通过另一个帐户间接连接到数据库?
IMO, the better approach is the second bullet point of gbn's answer: a common persisted table with an indicator as to the session or user. IMO,更好的方法是 gbn 答案的第二个要点:一个带有关于 session 或用户的指示符的常见持久表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.