Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

stored procedures - SQL Server SELECT INTO and Blocking With Temp Tables

So, recently a DBA is trying to tell us that we cannot use the syntax of

SELECT X, Y, Z
INTO #MyTable
FROM YourTable

To create temporary tables in our environment, because that syntax causes a lock on TempDB for the duration of the stored procedure executing. Now, I've found a number of things that detail how temporary tables work, scope of execution, cleanup and the like. But for the life of me, I don't see anything about blocking because of their use.

We are trying to find proof that we shouldn't have to go through and do CREATE TABLE #MyTable... for all of our temporary tables, but neither side can find proof. I'm looking for any insight SO people have.

Additional Information

Currently working with SQL Server 2005, and soon to be SQL Server 2008 (Enterprise editions)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That advice has been floating around for a long time:

Bottlenecks in SQL Server 6.5

Many people use a SELECT...INTO query to create a temporary table, something like this:

SELECT * INTO #TempTable FROM SourceTable

While this works, it creates locks against the tempdb database for the duration of the SELECT statement (quite a while if you are trawling through a lot of data in the source table, and longer still if the SELECT...INTO is at the start of a longer-running explicit transaction) While the lock is in place, no other user can create temporary tables. The actual location of the bottleneck is a lock on tempdb system tables. In later versions of SQL Server, the locking model has changed and the problem is avoided.

Fortunately, it was only a problem for SQL 6.5. It was fixed in 7.0 and later.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...