2023年7月13日发(作者:)
更新mysql表列的类型_将数据库某种类型的字段更新为另⼀种类型有时,我们可能会遇到这样的情况,当我们数据表的float类型精度不够时,可能需要把它统⼀调整成decimal或者money,⽽这时你⼀个⼀个去修改可能会崩溃,因为你⽆法从⼏千张表⾥确实找到所有的float类型的字段,⽽这时我们就需要⾃动的,批量的去处理它们。实现思路:从系统表中查询所有⽤户建⽴的表,然后查询指定类型的所有字段,最后使⽤alter table alter column去更新这个字段.知识点游标execSYSOBJECTS表和SYSCOLUMNS表常⽤类型的typeid值xtype= 35 'text'xtype=36 'uniqueidentifier'xtype=48 'tinyint'xtype=52 'smallint'xtype=56 'int'xtype=58 'smalldatetime'xtype=59 'real'xtype=60 'money'xtype=61 'datetime'xtype=62 'float'xtype=98 'sql_variant'xtype=99 'ntext'xtype=104 'bit'xtype=106 'decimal'xtype=108 'numeric'xtype=122 'smallmoney'xtype=127 'bigint'xtype=165 'varbinary'xtype=167 'varchar'xtype=173 'binary'xtype=175 'char'xtype=189 'timestamp'xtype=231 'nvarchar'xtype=239 'nchar'xtype=241 'xml'xtype=231 'sysname'实现代码DECLARE @tableName varchar(256)DECLARE @columnName varchar(256)DECLARE cursor2 CURSORFORSELECT NAME FROM SYSOBJECTS WHERE TYPE='U'OPEN cursor2FETCH NEXT FROM cursor2 INTO @tableNameWHILE @@fetch_status = 0BEGINDECLARE cursor3 CURSORFORSELECT name FROM SYSCOLUMNS WHERE ID=OBJECT_ID(@tableName) and xtype=60OPEN cursor3FETCH NEXT FROM cursor3 INTO @columnNameWHILE @@fetch_status = 0BEGINprint '更新表'[email protected]+',更新字段'[email protected]Exec('ALTER TABLE '[email protected]+' ALTER COLUMN '[email protected]+' [float] ')FETCH NEXT FROM cursor3 INTO @columnNameENDCLOSE cursor3DEALLOCATE cursor3FETCH NEXT FROM cursor2 INTO @tableNameENDCLOSE cursor2DEALLOCATE cursor2
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689250308a225772.html
评论列表(0条)