Replace some content of all fields in all tables in the SQLServer database-method 2

Source: Internet
Author: User
Replace some content of all fields in all tables in the SQLServer database with the table field content declare @ tvarchar (255), @ cvarchar (255) values, B. namefromsysobjectsa, syscolumnsb, policypescwherea. uandc

Replace some content of all fields in all tables in the SQL Server database with declare @ t varchar (255), @ c varchar (255) declare table_cursor cursor for select. name, B. name from sysobjects a, syscolumns B, policypes c where. id = B. id and. xtype = 'U' and c

Replace some content of all fields in all tables in the SQL Server database with the table field content
Declare @ t varchar (255), @ c varchar (255) declare table_cursor cursor for select. name, B. name from sysobjects a, syscolumns B, policypes c where. id = B. id and. xtype = 'U' and c. name in (-- here is the replacement type 'Char ', 'nchar', 'nvarchar ', 'varchar', 'text ', 'text' -- if your text (ntext) type does not exceed 8000 (4000) length, you can use) declare @ str varchar (500), @ str2 varchar (500) -- Here is the original character you want to replace: set @ str = 'A' -- here is the new character you want to replace: set @ str2 = 'bb' open table_cursor fetch next from table_cursor @ t, @ c while (@ fetch_status = 0) begin exec ('Update ['+ @ t +'] set ['+ @ c +'] = replace (cast (['+ @ c +'] as varchar (8000) ), ''' + @ str + ''', ''' + @ str2 + ''') ') fetch next from table_cursor into @ t, @ c end close table_cursor deallocate table_cursor;

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.