显式删除临时表或让 SQL Server 处理它 [英] Explicitly drop temp table or let SQL Server handle it

查看:30
本文介绍了显式删除临时表或让 SQL Server 处理它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理临时表删除的最佳实践是什么.我读过你应该明确地处理丢弃,而且 sql server 应该处理丢弃......什么是正确的方法?我一直认为您应该自己清理在 sproc 中创建的临时表,等等.但是,我发现其他一些建议并非如此.

What is best practice for handling the dropping of a temp table. I have read that you should explicitly handle the drop and also that sql server should handle the drop....what is the correct method? I was always under the impression that you should do your own clean up of the temp tables you create in a sproc, etc. But, then I found other bits that suggest otherwise.

任何见解将不胜感激.我只是担心我没有遵循我创建的临时表的最佳实践.

Any insight would be greatly appreciated. I am just concerned I am not following best practice with the temp tables I create.

谢谢,

S

推荐答案

我的观点是,首先看看您是否真的需要临时表 - 或者 - 您是否可以使用通用表表达式 (CTE).其次,我总是会删除我的临时表.有时您需要有一个临时表范围到连接(例如##temp),所以如果您第二次运行查询,并且您有显式代码来创建临时表,您将收到一个错误,指出该表已经存在.在自己之后进行清理始终是一种很好的软件做法.

My view is, first see if you really need a temp table - or - can you make do with a Common Table Expression (CTE). Second, I would always drop my temp tables. Sometimes you need to have a temp table scoped to the connection (e.g. ##temp), so if you run the query a second time, and you have explicit code to create the temp table, you'll get an error that says the table already exists. Cleaning up after yourself is ALWAYS a good software practice.

这篇关于显式删除临时表或让 SQL Server 处理它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆