简体   繁体   English

存储过程临时表导致超时错误

[英]Stored procedure temporary table causing timeout error

I have a temporary table in the stored procedure which is causing the time out for the query as it is doing a complex calculation. 我在存储过程中有一个临时表,这在进行复杂的计算时导致查询超时。 I want to drop it after it is used. 我要在使用后将其删除。 It was created like 它像

DECLARE @SecondTable TABLE

Now I cannot drop it using 现在我不能使用

drop @SecondTable

in fact I have to use 实际上我必须使用

drop #SecondTable

Does somebody know why? 有人知道为什么吗?

I'm by no means a SQL guru, but why is the drop even necessary? 我绝对不是SQL专家,但是为什么还要删除它呢?

If it's a table variable, it will no longer exist once the stored proc exits. 如果它是一个表变量,则一旦存储的proc退出,它将不再存在。

I'm actually surprised that DROP #SecondTable doesn't error out on you; 实际上,让我感到惊讶的是DROP #SecondTable没有对您出错。 since you're dropping a temporary table there; 因为您要在此处放置临时表; not a table variable. 不是表变量。

EDIT 编辑

So based on your comment, my updates are below: 因此,根据您的评论,我的更新如下:

1.) If you're using a table variable ( @SecondTable ); 1.)如果您使用的是表变量( @SecondTable ); then no drop is necessary. 那么就没必要掉落 SQL Server will take care of this for you. SQL Server将为您解决此问题。

2.) It sounds like your timeout is caused by the calculations using the table, not the dropping of the table itself. 2)听起来您的超时是由使用表进行的计算引起的,而不是表本身的删除引起的。 In this case; 在这种情况下; I'd probably recommend using a temporary table instead of a table variable; 我可能会建议使用临时表而不是表变量。 since a temporary table will let you add indexes and such to improve performance; 因为临时表可以让您添加索引等以提高性能; while a table variable will not. 而表变量不会。 If this still isn't sufficient; 如果还不够的话; you might need to increase the timeout duration on the query. 您可能需要增加查询的超时时间。

3.) In SQL; 3.)在SQL中; a table variable ( @SecondTable ) and temporary table ( #SecondTable ) are two completely different things. 表变量( @SecondTable )和临时表( #SecondTable )是完全不同的两件事。 I'd refer to the MSDN documentation for Table Variables and Temporary Tables 我会参考MSDN文档中的表变量临时表

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

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