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