SQL server temporary tables vs cursors

前端 未结 2 719
梦毁少年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:30

    Cursors work row-by-row and are extremely poor performers. They can in almost all cases be replaced by better set-based code (not normally temp tables though)

    Temp tables can be fine or bad depending on the data amount and what you are doing with them. They are not generally a replacement for a cursor.

    Suggest you read this: http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题