sql - 许多用户可以打开一个带有临时表的 SP 吗?

标签 sql sql-server database stored-procedures

用户登录到他的系统并打开我的程序后,他可以根据他的 Windows 用户名查看数据库中的数据(我为此编写了一个小存储过程)。

此数据来自带有临时表 (#temp) 的查询。我现在的问题是,如果很多用户使用这个程序,打开它后他们都会尝试在存储过程中构建#temp 表。这甚至可能吗?因为如果我尝试构建一个具有相同名称的 tempTable,服务器会给我一个错误。我是否必须根据登录的用户提供动态 TempTable 名称???或者还有其他更好的选择吗? MS-SQL服务器

最佳答案

  • 每个 session /连接都有一个本地临时表(一个#TableName)
  • 许多用户无法共享 session /连接

因此,存储过程中的本地临时表对于许多并发用户来说是安全的

另一方面,您使用全局临时表 (##TableName) 然后它对 world+dog 可见

来自 MSDN (我的粗体)

There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

关于sql - 许多用户可以打开一个带有临时表的 SP 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16893739/

相关文章:

sql - 规范化 SQL 表

java - java中rs.next只返回一个结果

c# - 使用 Linq 和 SQL 时如何防止将新记录插入表中?

sql - 'multi-part identifier'是什么,为什么不能绑定(bind)?

c# - SQL返回列中具有最大值的行,在特定范围内

sql-server - 使用 Select 语句和聚合函数从另一个表更新表

sql-server - 如何检测和获取表列加密信息

sql - 从不同模式的现有表创建表?

mysql - 比较 Mysql 上的 2 个数据库模式

database - 在数据库中建模 1 到 1..n 关系