循環是殺死數據庫性能的最糟糕方法之一!
我建議你嘗試處理在單個命令select語句,像這樣:
UPDATE t2
SET Cola=t1.col1, Colb=t1.col2
FROM UserInput t1
JOIN YourTable t2 ON t1.id=t2.id
但是如果你必須循環做這種方式:
DECLARE @Current int, @LastRow int
DECLARE @Col1 datatype, @Col2 datatype ....
DECLARE @Results table (RowId int identity1,1) primary key, col1 ...,col2 ...)
INSERT INTO @Results SELECT * FROM UserImport
SELECT @Current=0, @[email protected]@ROWCOUNT
WHILE @Current<@LastRow
BEGIN
SET @[email protected]+1
SELECT @Col1=Col1, @Col2=col2 FROM @Results WHERE [email protected]
/* Do Stuff */
END
,如果你正在處理超過100行,替換表變量:@Results
與臨時表:#Results
,如:
CREATE TABLE #RESULTS (RowId int identity1,1) primary key, col1 ...,col2 ...)
,因爲這會更快。
你能在你想在/發生什麼擴大*做的東西* /?可能有更好的解決方案。 – BradBrening 2010-05-03 00:37:18