简体   繁体   English

查看从存储过程创建的临时表

[英]View Temporary Table Created from Stored Procedure

I have a stored procedure in SQL 2005. The Stored Procedure is actually creating temporary tables in the beginning of SP and deleting it in the end. 我在SQL 2005中有一个存储过程。存储过程实际上是在SP的开头创建临时表并在最后删除它。 I am now debugging the SP in VS 2005. In between the SP i would want to know the contents into the temporary table. 我现在在VS 2005中调试SP。在SP之间,我想知道临时表中的内容。 Can anybody help in in viewing the contents of the temporary table at run time. 任何人都可以在运行时帮助查看临时表的内容。

Thanks Vinod T 谢谢Vinod T.

There are several kinds of temporary tables, I think you could use the table which is not dropped after SP used it. 有几种临时表,我认为你可以使用在SP使用它之后没有丢弃的表。 Just make sure you don't call the same SP twice or you'll get an error trying to create an existing table. 只要确保你没有两次调用相同的SP,否则你会在尝试创建现有表时遇到错误。 Or just drop the temp table after you see it's content. 或者只是在看到它的内容后删除临时表。 So instead of using a table variable ( @table ) just use #table or ##table 因此,不要使用表变量( @table ),而只需使用#table##table


From http://arplis.com/temporary-tables-in-microsoft-sql-server/ : 来自http://arplis.com/temporary-tables-in-microsoft-sql-server/

Local Temporary Tables 本地临时表

  • Local temporary tables prefix with single number sign (#) as the first character of their names, like (#table_name). 本地临时表前缀为单个数字符号(#)作为其名称的第一个字符,如(#table_name)。
  • Local temporary tables are visible only in the current session OR you can say that they are visible only to the current connection for the user. 本地临时表仅在当前会话中可见,或者您可以说它们仅对用户的当前连接可见。 They are deleted when the user disconnects from instances of Microsoft SQL Server. 当用户断开与Microsoft SQL Server实例的连接时,将删除它们。

Global temporary tables 全球临时表

  • Global temporary tables prefix with double number sign (##) as the first character of their names, like (##table_name). 全局临时表前缀为双号(##)作为其名称的第一个字符,如(## table_name)。
  • Global temporary tables are visible to all sessions OR you can say that they are visible to any user after they are created. 全局临时表对所有会话都可见,或者您可以说任何用户在创建后都可以看到它们。
  • They are deleted when all users referencing the table disconnect from Microsoft SQL Server. 当引用该表的所有用户与Microsoft SQL Server断开连接时,将删除它们。

Edit the stored procedure to temporarily select * from the temp tables (possibly into another table or file, or just to the output pane) as it runs..? 编辑存储过程以在临时表中临时选择*(可能在另一个表或文件中,或仅在输出窗格中),因为它运行..?

You can then change it back afterwards. 之后您可以将其更改回来。 If you can't mess with the original procedure, copy it and edit the copy. 如果您无法使用原始程序,请将其复制并编辑副本。

I built a few stored procedures which allow you to query the content of a temp table created in another session. 我构建了一些存储过程,允许您查询在另一个会话中创建的临时表的内容。

See sp_select project on github. 请参阅github上的sp_select项目。

The content of the table can be displayed by running exec sp_select 'tempdb..#temp' from no matter which session. 无论哪个会话,都可以通过运行exec sp_select 'tempdb..#temp'来显示表的内容。

Bottom line: the default Visual Studio Microsoft debugger is not in the same session as the SQL code being executed and debugged. 底线:默认的Visual Studio Microsoft调试器与正在执行和调试的SQL代码不在同一会话中。

So you can ONLY look at #temp tables by switching them to global ##temp tables or permanent tables or whatever technique you like best that works across sessions. 因此,您只能通过将#temp表切换到全局##临时表或永久表或任何您最喜欢的跨会话的技术来查看#temp表。

note: this is VERY different from normal language debuggers... and I suspect kept that way by Microsoft on purpose... I've seen third party SQL debugger tools decades ago that didn't have this problem. 注意:这与普通的语言调试器非常不同......我怀疑微软是故意这样做的......几十年前我见过第三方SQL调试工具没有这个问题。

There is no good technical reason why the debugger cannot be in the same session as your SQL code, thus allowing you to examine all produced contructs including #temp tables. 调试器不能与SQL代码在同一会话中,因此没有很好的技术原因,因此允许您检查所有生成的结构,包括#temp表。

This helped me. 这对我有帮助。

SELECT * FROM #Name

USE [TEMPDB]
GO

SELECT * FROM syscolumns 
   WHERE id = ( SELECT id FROM sysobjects WHERE [Name] LIKE '#Name%')

this gives the details of all the temp table 这给出了所有临时表的详细信息

To expand on previous suggestions that you drop the data into a permanent table, you could try the following: 要扩展先前将数据放入永久表中的建议,可以尝试以下操作:

-- Get rid of the table if it already exists
if object_id('TempData') is not null
  drop table TempData

select * into TempData from #TempTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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