One: Understanding CursorsCursor It enables the user to access the result set returned by SQL Server on a row-by-line basis.One of the main reasons for using cursors is to convert the set operation to a single record processing mode.When data is retrieved from a database in SQL language, the result is placed in an area of memory, and the result is often a collection of multiple records.The
Implicit cursors: You can only return one row of results at a time (no definition, default auto-build) Span style= "Font-family:courier new, Courier; FONT-SIZE:16PX "> explicit cursor: Requires a developer to define in advance, you can loop through the SQL statements in the cursor, return multiple rows of results Properties of an implicit
1. Description of the cursor Syntax: cursor name [(Parameter Name Data Type [, parameter name data type]...)] is SELECT statement; 2. It is used to store multiple rows of data returned by a query. 3. Open the cursor: Open C1; (open the cursor to execute the query). 4. Take the value of a row of the
| prior | first | last |Absolute {n | @ nvar | relative {n | @ nvar}]From] cursor name [into variable]Note:Next next line prior last line first lineLast last line absolute NLine n starting from the current position of relative nThe into variable assigns values of fields in the current row to the variable.
Cursor status variable:@ Fetch_status cursor status0 suc
ERP reports are often made, involving reading data during the stored procedure, reading data associated with multiple tables, and using a cursor. Typical Example of reading a user table cursor
declare @ age int declare @ name varchar (20) declare cur
Cursor Concept
In database operations, we often encounter this situation, that is, reading a record one by one from a specific result set. How can this problem be solved? Cursors provide us with an excellent solution.
Cursor is a data buffer provided by the system for users to store the execution results of SQL statements.
Each
Comparison of the Efficiency of three types of cursor loops in pl/SQL mainly compares the following three formats:
1. one open cursor is processed; loop fetch cursor INTO variable; exit when condition; end loop; CLOSE cursor; 2. batch Processing of open cursors; FETCH cursbu
. Therefore, the deallocate command is often used. This command allows you to delete the connection between a cursor and a cursor name or a cursor variable, and to release all system resources that the cursor occupies. Its syntax rules are:
deallocate {{[GLOBAL] cursor_name} | @cursor_variable_name}
The meanings of e
1. Introduction to ACSOracle Database 11g provides the adaptive cursor Sharing (ACS) feature to overcome the possibility that cursors that were not previously shared should be shared. ACS uses two new indicators: sensitivity and bindawareness to implement this feature.2. ACS mechanism2.1. Adaptive Cursor sharing metadata:oracle 11g also provides three new views and two new columns for dynamic view V$
Cursor
Overview of Cursors
I believe many Delphi programmers have written this code:
...
Begin
Mydataset.open;
Mydataset.frist;
While isn't (MYDATASET.BOF or mydataset.eof) do
Begin
...
End
Mydataset.close;
End
...
For a long time, we have been accustomed to using such code to do a line-by-row operation on the data returned by the database. Before opening a dataset with the client program's code, we think of it as an unordered collection.
Problem description: There are two tables,
Table
Table B
Now, we want to get the o_saloary in Table A and the_salary in Table B, which is the current total salary and updated to Table.You can use a view to connect tables.Now let's take the SQL statement into consideration. The cursor is a good method.Principle: The cursor extracts the corresponding dataset acco
SQL Server temporary table and cursor usage summary, SQL Server
1. Temporary table
Temporary tables are similar to permanent tables, but temporary tables are stored in tempdb. They are automatically deleted when they are no longer used.Temporary tables can be local or global.
2 Comparison:
The name of a local temporary table starts with a symbol (#).Only visible
must be used for query.
2. as an alternative method, when we exhaust the while LOOP, subqueries, temporary tables, table variables, self-built functions, or other methods, we can't implement some queries, we use cursors.
Life cycle and implementation of midstream mark in T-SQL
In a T-SQL, the lifecycle of a cursor consists of five parts
1. Define a
@userid variable) while @ @FETCH_STATUS = 0 BEGIN print @UserId;--Print data ( Print the UserID in the Memberaccount table) UPDATE dbo. Memberservice SET ServiceTime = DATEADD (Month, 6, GETDATE ()) WHERE UserId = @UserId; --Update the data FETCH next from the my_cursor into @UserId;--Reads the next row of data (the UserId in the Memberaccount table is placed in the @userid variable) endclose my_ Cursor; --Close the
mechanism allows users to access these records row-by-line within SQL Server, displaying and processing these records at the user's own will.Advantages of 1.2 CursorsThe following advantages are obtained from the cursor definition, which makes the cursor play an important role in the actual application:1) allows the program to perform the same or different opera
When we need to bulk delete the tables in the database, whether a single delete some of the table is annoying, tired, simply write a script to use.
This script uses cursor looping to delete, and for a smaller number, cursors are not adversely affected.
Copy Code code as follows:
DECLARE @tablename VARCHAR (30),
@sql VARCHAR (500)
V_cursorDeallocate v_cursor----------------------------------------------------------------SELECT * from V_testUpdate v_test set v_desc= ' Victor 199 ' WHERE v_id=801---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/*DECLARE my_cursor1 curs
Sometimes we need to read data one by one, as shown below:
This. sqlconnection1.open ();
Sqldatareader myreader = This. sqlcommand1.executereader ();
Do
{
While (myreader. Read ())
{
Console. writeline ("{0} {1}", myreader. getint32 (0), myreader. getstring (1 ));
} While (myreader. nextresult ());
Myreader. Close ();
Sqlconnection1.close ();
We often write this in our own program, but there is certainly a way to access data one by one in SQL Server,
Since executing the cursor is equivalent to executing the SELECT statement, the efficiency is not flattering and has not been studied in depth.
Copy codeThe Code is as follows: table1 structure:
Id int
Name varchar (50)
Declare @ id intDeclare @ name varchar (50)Declare cursor1 cursor for -- defines the
SQL cursor usage (reproduced)
I. The cursor consists of two parts:1. The cursor result set is a set of rows returned by the SELECT statement that defines the cursor.2. pointer to a row in the Set
Ii. cursor processing process:D
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.