Find fields in all tables in a database contain specific values, then modify specific values

Source: Internet
Author: User


DECLARE @cloumns varchar( +)Declare @tablename varchar( +)Declare @str varchar( +)Declare @counts intDeclare @sql nvarchar( -)Declare @str2 nvarchar( -)DeclareMyCursorCursor for SelectA.name asColumns, B.name asTableName fromsyscolumns a,sysobjects b,systypes cwherea.ID=b.id andB.type= 'U' andA.xtype=C.xtype andC.name like '%varchar%'
---old values in the databaseSet @str='a392ab5a-9972-46e2-8a32-4afe78ca1076'
---new values that need to be updatedSet @str2 = 'a392ab5a-9972-46e2-8a32-4afe78ca1076-00'OpenMyCursorFetch Next fromMyCursor into @cloumns,@tablename while(@ @Fetch_Status = 0)Begin Set @sql='Select @tmp_counts =count (*) from' +@tablename+ 'where' +@cloumns+' = " " +@str+ " '"Executesp_executesql@sqlN'@tmp_counts int out',@counts outif @counts>0 begin ---print ' table name: ' [email protected]+ ', field name ' [Email protected] Set @sql = 'Update' + @tablename + 'Set' + @cloumns + '=" " + @str2 + " "where' +@cloumns+' = " " +@str+ " '" Executesp_executesql@sql ---print @sql EndFetch Next fromMyCursor into @cloumns,@tablenameEndCloseMyCursordeallocateMyCursor

This example is to modify that there are many tables in the database that contain specific fields, and now the value of a particular field needs to be changed. To implement batch updates.

Find fields in all tables in a database contain specific values, then modify specific values

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.