简体   繁体   English

在 SQL 服务器登录时创建临时表

[英]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:基本上,这是我想要支持的场景:

  • user connects用户连接
    • a temp table is created inside a particular DB inside SQL (by SQL, kicked off by establishing of the connection)在 SQL 内的特定数据库中创建了一个临时表(由 SQL,通过建立连接开始)
  • some specific information is populated inside the table表格内填充了一些特定信息
  • for the duration of the connection;在连接期间; the user has (Read) access to the contents in this table;用户具有(读取)此表中内容的访问权限; the information in this table is used by a sub-system inside a particular database此表中的信息由特定数据库内的子系统使用
  • user disconnects用户断开连接
    • the temp table and all its contents is dropped by SQL临时表及其所有内容被 SQL 删除

Thanks谢谢

First thoughts:第一个想法:

  • modify your client code to create the table on connection?修改您的客户端代码以创建连接表? Then it can be done only when needed not all the time然后它只能在需要时完成,而不是一直
  • use a common, persisted table with a SessionID based on a GUID?使用基于 GUID 的具有 SessionID 的通用持久表? This will provide some audit + troubleshooting information too这也将提供一些审计+故障排除信息
  • use table value parameters to send data on demand rather than have any server-side caching使用表值参数按需发送数据,而不是使用任何服务器端缓存

And what I'd probably do:我可能会做的事情:

  • create the table when it's populated when I need it.当我需要它时创建表格。 The user can connect to the database for a variety of reasons (I assume).用户可以出于多种原因连接到数据库(我假设)。 So "connection" should be decoupled from "CREATE TABLE".所以“连接”应该与“CREATE TABLE”分离。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM