How to determine whether a cursor exists in SQL Server

Source: Internet
Author: User
-- Test declare @ sname varchar (20) Declare dd cursor for select sname from sopen DD. you need to determine whether the cursor exists select * from master. DBO. syscursors where cursor_name = 'dd' -- perform the following operations to check whether a cursor exists and check the cursor status. Select (case when cursor_status ('global', 'dd') = 1 then' the result set of the cursor must have at least one line 'when cursor_status ('global', 'dd ') = 0 then' the cursor result set is empty. 'When cursor_status ('global', 'dd') =-1 then' the cursor is closed. 'When cursor_status ('global ', 'dd') =-2 then' cursor does not apply to 'when cursor_status ('global', 'dd') =-3 then' cursor does not exist 'end) as result/* returned results: 1. The result set of the cursor must have at least one row. For non-differentiated cursors and keyset cursors, The result set must have at least one row. For dynamic cursors, The result set can have zero rows, one row, or multiple rows. The cursor assigned to the variable has been opened. For non-differentiated cursors and keyset cursors, The result set must have at least one row. For dynamic cursors, The result set can have zero rows, one row, or multiple rows. The result set of the 0 cursor is null. * The cursor assigned to the variable has been opened, but the result set must be empty. *-1 The cursor is closed. The cursor assigned to the variable is closed. -2 is not applicable. It can be: the previously called process does not allocate the cursor to the output variable. A cursor is assigned to the output variable during the previous call process. However, the cursor is closed at the end of the process. Therefore, the cursor is released and no call process is returned. The cursor is not allocated to declared cursor variables. -3 The cursor with the specified name does not exist. A cursor variable with the specified name does not exist, or even if such a cursor variable exists, it is not allocated a cursor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.