SQL server temporary tables vs cursors

前端 未结 2 720
梦毁少年i
梦毁少年i 2021-02-05 06:55

In SQL Server stored procedures when to use temporary tables and when to use cursors. which is the best option performance wise?

2条回答
  •  遥遥无期
    2021-02-05 07:48

    If ever possible avoid cursors like the plague. SQL Server is set-based - anything you need to do in an RBAR (row-by-agonizing-row) fashion will be slow, sluggish and goes against the basic principles of how SQL works.

    Your question is very vague - based on that information, we cannot really tell what you're trying to do. But the main recommendation remains: whenever possible (and it's possible in the vast majority of cases), use set-based operations - SELECT, UPDATE, INSERT and joins - don't force your procedural thinking onto SQL Server - that's not the best way to go.

    So if you can use set-based operations to fill and use your temporary tables, I would prefer that method over cursors every time.

提交回复
热议问题