有没有办法获取 SQL Server 中所有当前临时表的列表?
我意识到临时表是会话/连接绑定的,并且在会话/连接之外不可见或不可访问。
我有一个长时间运行的存储过程,它在各个阶段创建临时表。
有没有办法可以查看当前临时表的列表? 我需要什么特权才能这样做?
或者,
有没有办法可以看到正在运行的存储过程中执行的特定 SQL 语句?该过程作为 SQL Server 中的计划作业运行。
我正在使用 SQL Server 2000。
感谢您的指导。
I realize that temporary tables are session/connection bound and not visible or accessible out of the session/connection.
I have a long running stored procedure that creates temporary tables at various stages.
Is there a way I can see the list of current temporary tables?
What privileges do I need to be able to do so?
Alternatively,
Is there a way I can see the particular SQL statement being executed inside a running stored procedure? The procedure is running as a scheduled job in SQL Server.
I am using SQL Server 2000.
Thanks for your guidance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是你所追求的吗?
Is this what you are after?
您可以通过以下查询获取临时表列表:
You can get list of temp tables by following query :
如果要包含全局临时表,可以删除 ## 行。
you can remove the ## line if you want to include global temp tables.
对于 SQL Server 2000,这应该只告诉您会话中的#temp 表。 (改编自我的关于更现代的 SQL Server 版本的示例。)这假设您不使用三个来命名您的表连续的下划线,例如
CREATE TABLE #foo___bar
:For SQL Server 2000, this should tell you only the #temp tables in your session. (Adapted from my example for more modern versions of SQL Server here.) This assumes you don't name your tables with three consecutive underscores, like
CREATE TABLE #foo___bar
:如果您需要“查看”临时表的列表,您可以简单地记录所使用的名称。 (正如其他人所指出的,可以直接查询此信息)
如果您需要“查看”临时表的内容,则需要创建具有(唯一的)临时名称的真实表。
您可以使用 SQL Profiler 跟踪正在执行的 SQL:
使用 SQL Server Profiler< /a>
如何使用 SQL Profiler 识别运行缓慢的查询
[这些文章针对 2000 年以后的 SQL Server 版本,但大部分建议是相同的。]
如果您有一个对您的业务很重要的冗长流程,那么最好记录各个步骤(步骤名称/编号,过程中的开始和结束时间)。这样,您就有了一个基线,可以在事情表现不佳时进行比较,并且可以更快地查明哪些步骤导致了问题。
If you need to 'see' the list of temporary tables, you could simply log the names used. (and as others have noted, it is possible to directly query this information)
If you need to 'see' the content of temporary tables, you will need to create real tables with a (unique) temporary name.
You can trace the SQL being executed using SQL Profiler:
Using SQL Server Profiler
How to Identify Slow Running Queries with SQL Profiler
[These articles target SQL Server versions later than 2000, but much of the advice is the same.]
If you have a lengthy process that is important to your business, it's a good idea to log various steps (step name/number, start and end time) in the process. That way you have a baseline to compare against when things don't perform well, and you can pinpoint which step(s) are causing the problem more quickly.