2

Is the following query an acceptable way to convert the fields' data_types?

UPDATE INFORMATION_SCHEMA.COLUMNS 
SET `COLUMNS`.`DATA_TYPE` = 'tinyint'
WHERE
`COLUMNS`.`TABLE_SCHEMA` = 'BAR' 
AND `COLUMNS`.`TABLE_NAME` = 'FOO'
AND `COLUMNS`.`DATA_TYPE` = 'bit'

Should I be concerned about data integrity or will data stored as bit move to tinyint without issue?

jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139

2 Answers2

1

This won't do. Operations UPDATE, DELETE, INSERT are not allowed on the INFORMATION_SCHEMA database.

Here's why.

Community
  • 1
  • 1
1

You can't directly modify the metadata tables (they're actually views but the distinction's not important for this case) but you can use them to generate your alter table statements, something like this:

SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.' + TABLE_NAME + ' ALTER COLUMN ' + COLUMN_NAME + ' TINYINT' + CASE WHEN IS_NULLABLE = 'YES' THEN ' NULL;' ELSE ' NOT NULL;' END
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE `COLUMNS`.`TABLE_SCHEMA` = 'BAR' 
AND `COLUMNS`.`TABLE_NAME` = 'FOO'
AND `COLUMNS`.`DATA_TYPE` = 'bit'
Gareth Lyons
  • 1,942
  • 12
  • 14