Example of re-adding a data storage process after clearing table data in SQL

Source: Internet
Author: User

Copy codeThe Code is as follows:
Alter procedure [dbo]. [sp_add_Jurisdiction]
@ CTableName varchar (20), -- the table to be deleted and added
@ FiledkeyValue varchar (20), -- Value of the field to be deleted
@ FiledName varchar (20), -- Name of the field to be deleted
@ FiledNameAdd1 varchar (20), -- field name 1 to be added
@ FiledNameAdd2 varchar (20), -- the field name to be added 2
@ SQL varchar (6000) -- new value-added string, for example ,;
AS
Begin
Declare @ strSQL_1 varchar (6000) -- delete
Declare @ strSQL_2 varchar (6000) -- added
BEGIN try
BEGIN TRANSACTION
Set @ strSQL_1 = 'delete from ['+ @ CTableName +'] where' + @ filedName + '=' + @ filedkeyValue +''
Exec (@ strSQL_1)

DECLARE @ Run bit -- mark of continued Loop
Declare @ dotIndex as int
Declare @ doIndex as int
Declare @ strValue1 as varchar (100)
Declare @ strValue2 as varchar (100)
Declare @ strText as varchar (100)
Declare @ ReturnValue int
Set @ Run = 1
Set @ ReturnValue = 0;
While (@ Run = 1)
Begin
If (len (@ SQL)> 0) -- if the string is not empty, perform the following operations:
Begin
Set @ dotIndex = CharIndex (';', @ SQL)
If (@ dotIndex = 0) -- determines whether the ';' symbol is found.
Begin
Set @ doIndex = CharIndex (',', @ SQL)
Set @ strValue1 = substring (@ SQL, 1, @ doIndex-1)
Set @ strValue2 = substring (@ SQL, @ doIndex + 1, LEN (@ SQL ))
Set @ strSQL_2 = 'insert into ['+ @ CTableName +'] ('+ @ filedName +', '+ @ filedNameAdd1 +', '+ @ filedNameAdd2 + ') values ('+ @ filedkeyValue +', '+ @ strValue1 +', '+ @ strValue2 + ')'
Exec (@ strSQL_2 );
Set @ ReturnValue = @ ReturnValue + 1;
Set @ Run = 0
End;
Else
Begin
Set @ strText = substring (@ SQL, 1, @ dotIndex-1) -- truncate ',' before the string
Set @ SQL = substring (@ SQL, @ dotIndex + 1, len (@ SQL) -- shorten the string length to the next one
Set @ doIndex = CharIndex (',', @ strText)
Set @ strValue1 = substring (@ strText, 1, @ doIndex-1)
Set @ strValue2 = substring (@ strText, @ doIndex + 1, LEN (@ strText ))
Set @ strSQL_2 = 'insert into ['+ @ CTableName +'] ('+ @ filedName +', '+ @ filedNameAdd1 +', '+ @ filedNameAdd2 + ') values ('+ @ filedkeyValue +', '+ @ strValue1 +', '+ @ strValue2 + ')'
Exec (@ strSQL_2)
Set @ ReturnValue = @ ReturnValue + 1;
End
End
Else
Begin
Set @ Run = 0
Set @ ReturnValue = @ ReturnValue + 1;
End
End
Commit Transaction
Return @ ReturnValue;
End try
Begin catch
Set @ ReturnValue = 0;
Rollback Transaction
Return @ ReturnValue;
End catch
End

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.