Usage of the cursor, transaction, and replace methods in SQL stored procedures

Source: Internet
Author: User

 

Code
Set ansi_nulls on
Go
Set quoted_identifier on
Go

------------------------------------
-- Purpose: delete a record.
-- Project name:
-- Note:
-- Time: 2008/7/23 9:23:41
------------------------------------
Alter procedure [DBO]. [deleteinfo]
@ Userid int,
@ Username varchar (20)
As
Begin
Declare @ studentid varchar (20)
Declare @ users varchar (max)
Declare @ usercd int
Declare @ Type tinyint
Declare @ users_new varchar (max)
-- Transaction
Begin transaction Trans
-- Get ID number
Select @ studentid = studentid from tbl_inner_studentinfo where stunumber = @ username
Print (@ studentid)

Declare @ _ cursor_ SQL cursor
Set @ _ cursor_ SQL = cursor
Select [userid]
, [Users]
, [Appraisetype]
From Table1
Where ',' + users + ', 'like' %,' + @ studentid + ', %'
Or ',' + users + ', 'like' %, A' + @ studentid +', %'

-- Clear rating Information
Open @ _ cursor_ SQL
Fetch next from @ _ cursor_ SQL into @ usercd, @ users, @ Type
While @ fetch_status = 0
Begin
-- Repair value
Set @ users_new = Replace (@ users, 'A' + @ studentid + ',','')
Set @ users_new = Replace (@ users_new, @ studentid + ',','')
-- Modify
Update Table1
Set users = @ users_new
Where [userid] = @ usercd and [appraisetype] = @ Type
-- Reloop
Fetch next from @ _ cursor_ SQL into @ usercd, @ users, @ Type
End
Close @ _ cursor_ SQL
Deallocate @ _ cursor_ SQL

If (@ error <> 0)
Begin
Rollback transaction Trans
End
Else
Begin
Commit transaction Trans
End
End
Go

Set ansi_nulls off
Go
Set quoted_identifier off
Go

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.