SQL Server cursor + deferred execution introduction

Source: Internet
Author: User

In a project test, we might use batch-generated data to test the program's performance.

Here is a question I encountered, because we bulk generated data is almost instantaneous, so the getdate () function is basically the same time, and we ask to generate each data time is different, then how to solve this problem?

Search the Internet a lot, here I use the cursor +waitfor to deal with:

First of all, the downstream target, because the individual is also using the cursor, but also not in-depth study.

So what is a cursor for? In general, we use cursors to process the resulting set of queries (modified, new ...). )。

The cursor definitions in T-SQL are as follows in MSDN:

DECLARE cursor_name cursor [LOCAL | GLOBAL]
[Forward_only | SCROLL]
[STATIC | KEYSET | DYNAMIC | Fast_forward]
[Read_Only | Scroll_locks | Optimistic]
[Type_warning]
For select_statement
[For UPDATE [of column_name [,... N]]
[;]


Simple definition of cursor: DECLARE CURSOR name cursor for result set

Open cursor: Open cursor name

Read down cursor: FETCH NEXT from cursor name into receive variable from data read from result set

Close cursor: Close cursor name

Release cursor: DEALLOCATE cursor name
Because we want to iterate over the result set using cursors, we have to introduce a global variable @ @Fetch_Status

The @ @FETCH_STATUS returns the state of the last cursor executed by the FETCH statement, not the state of any cursors currently open for the connection.

The return value describes the 0 FETCH statement success. The -1 FETCH statement failed or the row is not in the result set. -2 rows that were fetched do not exist

Here are just some simple ways to use cursors.

Let's discuss how to use latency in the database.

WAITFOR DELAY ' 00:00:01 '--executes the print statement after one second

print ' I was delayed execution '

WAITFOR DELAY ' 00:00:00.010 '--10 milliseconds after execution of print statement

print ' I am deferred execution '

waitfor time ' 10:00:00 '--10 point Execution Print statement

print ' I am deferred execution '

SQL Server cursor + deferred execution introduction

Related Article

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.