...... N words are omitted here ...DROP PROCEDURE Test_proc; CREATE PROCEDURE Test_proc as DECLARE @u_id INT;D eclare @u_name VARCHAR (255); DECLARE @age INT;--Declares the parameter in the cursor--select, must be the same as the variable name that the cursor takes out DECLARE
There are four basic steps to use a cursor: declare the cursor, open the cursor, extract data, and close the cursor..
As shown in the following SQL example:
--
========================================================== ==
How to use SQL Server Tour banner sentences:
Copy Code code as follows:
--Declares a cursor
DECLARE MyCursor CURSOR
For the SELECT Top 5 fbookname,fbookcoding The from tbookinfo//defines a cursor called MyCursor that holds the data after the fo
To be honest, the cursor has not been used for a long time using MS SQL Server. Similar functions were previously implemented using a while loop plus a temporary table. Just had a reference to the online example practice to write. Learn about cursor concepts and syntax.
In the following code example,
First declare th
Method 1:CREATE TABLE #tabTmp (id int)INSERT #tabTmp EXECUTE (' SELECT ID from ' [email protected])DECLARE curone cursor FORSELECT ID from #tabTmpOpen CuroneFETCH NEXT from Curone to @ID
Method 2EXEC ('DECLARE curone cursor GlobalFor ' + @aSql)Open CuroneFetch Curone
Method 3:
To be honest, the cursor has never been used since ms SQL Server was used for such a long time. Previously, similar functions were implemented using a WHILE loop and a temporary table. I wrote a reference to the online example exercise just now. Learn about the cursor concept and syntax.In the following code example,First, de
The resolution operation saves a parent cursor and child cursor to the library cache and saves them to sga to reuse them and avoid hard parsing. Under what circumstances can I reuse the parent cursor and child cursor?The key information of the parent cursor is the
A simple cursor deletes an SQL SERVER table.
Use databaseNameDeclare @ tblname char (100)Declare @ SQL char (5000)Declare table_cursor cursor for select name from sysobjects where name like 'tb _ card [_] % 'and name And name Open
Tags: style blog http tar ext comTo be honest, using MS SQL Server for such a long time, cursors have not been used. Previous implementations of similar functionality were implemented using while loops plus temporary tables. Just a reference to the online sample exercise to write a bit. Learn about cursor concepts and syntax.In the following code example,First, declare
A SQL Server cursor and a loop nested stored procedure use case Preparation code
--创建表CREATE TABLE everyoneAVG( ID int primary key identity(1,1), AccNo INT, AVGTime INT, AddTimes INT);DROP TABLE everyoneAVG;SELECT ID,AccNo AS ‘用户号‘,AVGTime/60 AS ‘平均充值时间间隔(小时)‘,AddTimes AS ‘总充值次数‘ FROM everyoneAVGORDER BY AVGTime/60 DESC;DELETE everyoneAVG;Functional RequirementsFind out the average recharg
Number
Standard parcel Code (LANDCODE)
Section Code (SECTCODE)
1
131001bg001
G001
2
131001bg002
G001
3
131001bg003
G001
4
131001bg004
G002
5
131001bg005
G003
Now you need to convert the data in the table to the results shown in the following table:
Number
Section coding
Included Standard parcel
1
G001
131001bg001,131001bg002,131001bg003
-- 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 chec
Cursors provide a flexible way to retrieve and operate data from tables. cursors are mainly used on servers to process SQL statements sent from clients to servers, or data processing requests in batch processing, stored procedures, and triggers.
How can I use a cursor?First declare the cursor:
SQL Server trigger cursor note today receives a requirement to use a trigger to update other tables through conditions. I haven't touched SQLSERVER for a long time, and there is no SQLSERVER installation package in my computer. My colleague sent an installation package. I was shocked, 3.6 GB !!!! After a long wait, start the job. Requirements: 1. When field a is updated to 2 or 3, and field B is
Tags:beginusecourseend data creatproc read for Use NewTest1---Declaration view---CREATE VIEW newviewte as select Studentinfo.name as name, studentinfo.sex as gender, Course.name as course fr Om studentinfo join Course on studentinfo.cid= course.id---Call view select * FROM newviewte---declaration trigger---CREATE TRIGGER NewT RI on ClassInfo for insert as print ' data insert test ' go create trigger NewTri1 on ClassInfo for delete as print ' data delete test ' Go inser T into ClassInfo Values
Summary
This article describes varous methods that you canUse to simulate a cursor-like fetch-Next logic in a stored procedure,Trigger, or transact-SQL batch. Use Transact-SQL statements to iterate through a result set
There are three methods you can use to iterate through a result set by using Transact-SQL statement
The data block cursor example is as follows:
Begindeclare @ item_code varchar (32) -- defines the variable declare @ item_name varchar (32) Declare @ invest_money_sum float -- defines the cursor declare my_cursor cursor forselect
[SQL]
-- 1. Update the salary of each teacher to the original salary + bonus
-- Defines two variables to store ttid and reward
Declare @ TID intDeclare @ reward money
-- 1. Create a cursor Based on the bonus table
Declare cur_reward cursor fast_forward for select ttid
In (1), we will explain how to input a String array and an int array. Here, we will explain how to output a String array. After reading the Code, some friends may wonder: why do we need to transfer the String array? Is it difficult to transmit a cursor directly?
Here we will briefly describe the original intention: The cursor can only be transmitted from the table, or, from the custom array, select as a
[SQL]
--1. Update each teacher's salary to the original salary + bonus
--defines two variables that are used to store ttid and reward
DECLARE @tid int
DECLARE @reward Money
--1. Create a cursor based on the bonus table
Declare cur_reward
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.