有没有办法获取 SQL Server 中所有当前临时表的列表?

发布于 2024-11-29 12:32:40 字数 246 浏览 1 评论 0原文

我意识到临时表是会话/连接绑定的,并且在会话/连接之外不可见或不可访问。

我有一个长时间运行的存储过程,它在各个阶段创建临时表。

有没有办法可以查看当前临时表的列表? 我需要什么特权才能这样做?

或者,

有没有办法可以看到正在运行的存储过程中执行的特定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终难愈 2024-12-06 12:32:40

这就是你所追求的吗?

select * from tempdb..sysobjects
--for sql-server 2000 and later versions

select * from tempdb.sys.objects
--for sql-server 2005 and later versions

Is this what you are after?

select * from tempdb..sysobjects
--for sql-server 2000 and later versions

select * from tempdb.sys.objects
--for sql-server 2005 and later versions
萌无敌 2024-12-06 12:32:40

您可以通过以下查询获取临时表列表:

select left(name, charindex('_',name)-1) 
from tempdb..sysobjects
where charindex('_',name) > 0 and
xtype = 'u' and not object_id('tempdb..'+name) is null

You can get list of temp tables by following query :

select left(name, charindex('_',name)-1) 
from tempdb..sysobjects
where charindex('_',name) > 0 and
xtype = 'u' and not object_id('tempdb..'+name) is null
阿楠 2024-12-06 12:32:40
SELECT left(NAME, charindex('_', NAME) - 1)
FROM tempdb..sysobjects
WHERE NAME LIKE '#%'
    AND NAME NOT LIKE '##%'
    AND upper(xtype) = 'U'
    AND NOT object_id('tempdb..' + NAME) IS NULL

如果要包含全局临时表,可以删除 ## 行。

SELECT left(NAME, charindex('_', NAME) - 1)
FROM tempdb..sysobjects
WHERE NAME LIKE '#%'
    AND NAME NOT LIKE '##%'
    AND upper(xtype) = 'U'
    AND NOT object_id('tempdb..' + NAME) IS NULL

you can remove the ## line if you want to include global temp tables.

_蜘蛛 2024-12-06 12:32:40

对于 SQL Server 2000,这应该只告诉您会话中的#temp 表。 (改编自我的关于更现代的 SQL Server 版本的示例。)这假设您不使用三个来命名您的表连续的下划线,例如 CREATE TABLE #foo___bar

SELECT 
  name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
  t.id
FROM tempdb..sysobjects AS t
WHERE t.name LIKE '#%[_][_][_]%'
AND t.id = 
  OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));

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:

SELECT 
  name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
  t.id
FROM tempdb..sysobjects AS t
WHERE t.name LIKE '#%[_][_][_]%'
AND t.id = 
  OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
对你的占有欲 2024-12-06 12:32:40

如果您需要“查看”临时表的列表,您可以简单地记录所使用的名称。 (正如其他人所指出的,可以直接查询此信息)

如果您需要“查看”临时表的内容,则需要创建具有(唯一的)临时名称的真实表。

您可以使用 SQL Profiler 跟踪正在执行的 SQL:

[这些文章针对 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:

[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文