TAILIEUCHUNG - Using Cursors

Using Cursors When you execute a SELECT statement, all the rows are returned in one go. This might not always be appropriate. For example, you might want to take some action based on the column values retrieved for a particular row | Using Cursors When you execute a SELECT statement all the rows are returned in one go. This might not always be appropriate. For example you might want to take some action based on the column values retrieved for a particular row. To do this you can use a cursor to process rows retrieved from the database one row at a time. A cursor allows you to step through the rows returned by a particular SELECT statement. You follow these steps when using a cursor 1. Declare variables to store the column values from the SELECT statement. 2. Declare the cursor specifying your SELECT statement. 3. Open your cursor. 4. Fetch the rows from your cursor. 5. Close your cursor. You ll learn the details of these steps in the following sections. Step 1 Declare Variables to Store the Column Values from the SELECT Statement These variables must be compatible with the column types for the retrieved rows. For example you ll want to use an int variable to store the value from an int column and so on. The following example declares three variables to store the ProductID ProductName and UnitPrice columns from the Products table DECLARE @MyProductID int DECLARE @MyProductName nvarchar 40 DECLARE @MyUnitPrice money Step 2 Declare the Cursor A cursor declaration consists of a name that you assign to the cursor and the SELECT statement that you want to execute. This SELECT statement is not actually run until you open the cursor. You declare your cursor using the DECLARE statement. The following example declares a cursor named ProductCursor with a SELECT statement that retrieves the ProductID ProductName and UnitPrice columns for the first 10 products from the Products table DECLARE ProductCursor CURSOR FOR SELECT ProductID ProductName UnitPrice FROM Products WHERE ProductID 10 Step 3 Open the Cursor Now it s time to open your cursor which runs the SELECT statement previously defined in the DECLARE statement. You open a cursor using the OPEN statement. The following example opens ProductCursor and

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.