What is the use of a cursor in SQL Server?

后端 未结 6 602
滥情空心
滥情空心 2020-12-13 18:47

I want to use a database cursor; first I need to understand what its use and syntax are, and in which scenario we can use this in stored procedures? Are there different synt

6条回答
  •  暖寄归人
    2020-12-13 18:52

    Cursors are a mechanism to explicitly enumerate through the rows of a result set, rather than retrieving it as such.

    However, while they may be more comfortable to use for programmers accustomed to writing While Not RS.EOF Do ..., they are typically a thing to be avoided within SQL Server stored procedures if at all possible -- if you can write a query without the use of cursors, you give the optimizer a much better chance to find a fast way to implement it.

    In all honesty, I've never found a realistic use case for a cursor that couldn't be avoided, with the exception of a few administrative tasks such as looping over all indexes in the catalog and rebuilding them. I suppose they might have some uses in report generation or mail merges, but it's probably more efficient to do the cursor-like work in an application that talks to the database, letting the database engine do what it does best -- set manipulation.

提交回复
热议问题