Alter every double fields of a table

橙三吉。 提交于 2019-12-12 05:47:16

问题


I need to change every double precision field of a table to numeric(15,3) type, how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ?


回答1:


following query should return query to update these tables... you can add table filter if you want. Copy the result and run it.

select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)'  
  from information_schema.columns 
 where data_type = 'double precision' 
       and table_name = 'YOUR_TABLE_NAME'

TEST IT BEFORE RUN IT. I DID NOT TEST THIS YET.




回答2:


While another question will do it for all columns; a simple "alter table [tablename] alter column [columnToAlter] type numeric(15,3). You shouldn't need to run them through a cursor; any value that's not going to be affected by this should remain unaffected.

If you can't do it by changing the datatype itself, a simple update [tablename] set [columnname] = cast(columnname as numeric(15,3) should also work.

Hope that helps!



来源:https://stackoverflow.com/questions/6431564/alter-every-double-fields-of-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!