简体   繁体   English

SQL Server处理表中的每一行-游标或While循环

[英]SQL Server process each row in table - Cursor or While loop

I am dumping all the file names in directory into a temp table using xp_dirtree. 我正在使用xp_dirtree.目录中的所有文件名转储到临时表中xp_dirtree.

CREATE TABLE #TempTable (Subdirectory VARCHAR(512), Depth INT, xFile INT);

INSERT INTO #TempTable (Subdirectory, Depth, xFile)
   EXEC xp_dirtree 'D:\log\Day_20140301',0,1

SELECT Subdirectory AS Filename 
FROM #TempTable 
WHERE subdirectory LIKE '%.log';

SELECT * FROM #TempTable

I use each file names in this table for Bulk insert. 我使用此表中的每个文件名进行批量插入。

Would a cursor be good or a while loop? 游标是好的还是一会儿循环?

Currently using while loop to loop through each record. 当前使用while循环遍历每条记录。

Heard many negative things about cursor. 听到有关游标的许多负面信息。 Open to all suggestion for the same. 公开征求所有相同建议。

In this case, "No matter" is probably the right answer. 在这种情况下,“不管”可能是正确的答案。 While or cursor over 1k records will cost you approximately nothing comparing to the IO latency and CPU used by bulk insert. 与IO延迟和大容量插入所使用的CPU相比,在1k记录上游历或游标都不会花费多少。 I suppose, optimizing loaded files structure and corresponding tables should take the lead. 我想,优化加载的文件结构和相应的表应该带头。

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

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