Common cursor looping usages in SQL Server stored procedures

Source: Internet
Author: User

Original: Common cursor looping usages in SQL Server stored procedures

With cursors, and while you can traverse every record in your query and pass the required fields to variables for appropriate processing

DECLARE @A1 VARCHAR(Ten),@A2 VARCHAR(Ten),@A3 INTDECLAREYoucurnameCURSOR  for SELECTA1,a2,a3 fromYoutablenameOPENYoucurnameFetch Next  fromYoucurname into @a1,@a2,@a3  while @ @fetch_status<>-1 begin --the action you want to perform is written hereFetch Next  fromYoucurname into @a1,@a2,@a3 End CloseYoucurnamedeallocateYoucurname

Together with the exception capture and transaction, the complete process is as follows:

Create PROCEDURE  [dbo].[Usp_createpanicbuyingcode]( @OrderNumber VARCHAR( -)) as  DECLARE @A1 VARCHAR(Ten),@A2 VARCHAR(Ten),@A3 INT  beginTrybegin Tran    DECLAREYoucurnameCURSOR   for SELECTA1,a2,a3 fromYoutablenameOPENYoucurnameFetch Next  fromYoucurname into @a1,@a2,@a3 while @ @fetch_status<>-1begin--the action you want to perform is written hereFetch Next  fromYoucurname into @a1,@a2,@a3EndCloseYoucurnamedeallocateYoucurnameCOMMIT TRAN  EndTrybeginCatchROLLBACK  EndCatch

Common cursor looping usages in SQL Server stored procedures

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.