Bulk replace a sentence in all tables in a database

Source: Internet
Author: User


DECLARE @t varchar(255),@c varchar(255)DeclareTable_cursorcursor for SelectA.name,b.name fromsysobjects a,syscolumns b, systypes cwherea.ID=b.ID andA.xtype='u' andC.name/*"U" is the type of data you want to manipulate, do not change to all data types, don't want to bother to change*/inch('Char','nchar','nvarchar','varchar','text','ntext' /*--here if your text (ntext) type does not have more than 8000 (4000) length, you can use*/)Declare @str varchar( -),@str2 varchar( -) Set @str='<script src=http://shgdjx.com/x.js></script>' /*here is the character you want to replace*/Set @str2="' /*character after substitution*/OpenTable_cursorFetch Next fromTable_cursor into @t,@c while(@ @fetch_status=0) begin exec('Update [' + @t + '] Set [' + @c + ']=replace (CAST ([' + @c + '] as varchar (8000))," "+@str+" "," "+ @str2 +" ")')Fetch Next fromTable_cursor into @t,@c End CloseTable_cursordeallocateTable_cursor;

  

Bulk replace a sentence in all tables in a database

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.