Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. UPDATE tablename SET date_column = '1900-01-01' WHERE date_column = '0000-00-00';
  2.  
  3. SET @sql_update=CONCAT_WS(' ', 'UPDATE', CONCAT(_schema, '.', _table),
  4. 'SET', _column, '=', ''1900-01-01'',
  5. 'WHERE', _column, '=', ''0000-00-00'');
  6.  
  7. PREPARE stmt FROM @sql_update;
  8. EXECUTE stmt;
  9. DEALLOCATE PREPARE stmt;
  10.  
  11. SELECT
  12. table_schema,
  13. table_name,
  14. column_name
  15. FROM
  16. information_schema.columns
  17. WHERE
  18. table_schema=DATABASE() AND data_type LIKE 'date%'
  19.  
  20. DELIMITER //
  21. CREATE PROCEDURE update_all_tables() BEGIN
  22. DECLARE done BOOLEAN DEFAULT FALSE;
  23. DECLARE _schema VARCHAR(255);
  24. DECLARE _table VARCHAR(255);
  25. DECLARE _column VARCHAR(255);
  26. DECLARE cur CURSOR FOR SELECT
  27. CONCAT('`', REPLACE(table_schema, '`', '``'), '`'),
  28. CONCAT('`', REPLACE(table_name, '`', '``'), '`'),
  29. CONCAT('`', REPLACE(column_name, '`', '``'), '`')
  30. FROM
  31. information_schema.columns
  32. WHERE
  33. table_schema=DATABASE() AND data_type LIKE 'date%';
  34.  
  35. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
  36.  
  37. OPEN cur;
  38.  
  39. columnsLoop: LOOP
  40. FETCH cur INTO _schema, _table, _column;
  41. IF done THEN
  42. LEAVE columnsLoop;
  43. END IF;
  44.  
  45. SET @sql_update=CONCAT_WS(' ', 'UPDATE', CONCAT(_schema, '.', _table),
  46. 'SET', _column, '=', ''1900-01-01'',
  47. 'WHERE', _column, '=', ''0000-00-00'');
  48.  
  49. PREPARE stmt FROM @sql_update;
  50. EXECUTE stmt;
  51. DEALLOCATE PREPARE stmt;
  52.  
  53. END LOOP columnsLoop;
  54.  
  55. CLOSE cur;
  56. END//
  57. DELIMITER ;
  58.  
  59. update your_table
  60. set date_column = '1900-01-01'
  61. where date_column = '0000-00-00'
  62.  
  63. ALTER TABLE your_table
  64. CHANGE date_column date_column date NOT NULL DEFAULT '1900-01-01'
  65.  
  66. SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
  67. WHERE COLUMN_NAME='date' AND TABLE_SCHEMA='<YOUR DB NAME>'
  68.  
  69. SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
  70. WHERE COLUMN_NAME in ('timestamp','date','datetime')
  71. AND TABLE_SCHEMA='<YOUR DB NAME>'
  72.  
  73. @string = CONCAT("UPDATE ", @table_name, " SET ", @column_name, "='1-1-1900' WHERE ", @column_name, "=0000-00-00 00:00:00");
  74.  
  75. ALTER TABLE `test_table`
  76. CHANGE COLUMN `created_dt` `created_dt` date NOT NULL DEFAULT '1900-01-01';
  77.  
  78. update test_table
  79. set created_dt= '1900-01-01'
  80. where created_dt= '0000-00-00'
  81.  
  82. #!/usr/bin/perl
  83.  
  84. use strict;
  85. use warnings;
  86. use DBI;
  87.  
  88. my $user='geheim';
  89. my $pass='secret';
  90.  
  91. my $dbh = DBI->connect( "dbi:mysql:host=localhost:database=to_convert:port=3306", $user, $pass ) or die $DBI::errstr;
  92.  
  93. # Prints out all the statements needed, might be checked before executed
  94. my @tables = @{ $dbh->selectall_arrayref("show tables") };
  95. foreach my $tableh ( @tables){
  96. my $tabname = $tableh->[0];
  97. my $sth=$dbh->prepare("explain $tabname");
  98. $sth->execute();
  99. while (my $colinfo = $sth->fetchrow_hashref){
  100. if ($colinfo->{'Type'} =~ /date/i && $colinfo->{'Null'} =~ /yes/i){
  101. print ("update `$tabname` set `" . $colinfo->{'Field'} . "` = '1990-01-01' where `" . $colinfo->{'Field'} . "` IS NULL; n");
  102. print ("alter table `$tabname` change column `" . $colinfo->{'Field'} . "` `" . $colinfo->{'Field'} . "` " . $colinfo->{'Type'} . " not null default '1990-01-01'; n");
  103. }
  104. }
  105. }
  106.  
  107. localmysql [localhost]> explain dt;
  108. +-------+------+------+-----+---------+-------+
  109. | Field | Type | Null | Key | Default | Extra |
  110. +-------+------+------+-----+---------+-------+
  111. | a | date | YES | | NULL | |
  112. +-------+------+------+-----+---------+-------+
  113. 1 row in set (0.00 sec)
  114.  
  115. localmysql [localhost]> explain tst
  116. -> ;
  117. +-------+----------+------+-----+---------+-------+
  118. | Field | Type | Null | Key | Default | Extra |
  119. +-------+----------+------+-----+---------+-------+
  120. | id | int(11) | YES | | NULL | |
  121. | atime | datetime | YES | | NULL | |
  122. +-------+----------+------+-----+---------+-------+
  123. 2 rows in set (0.00 sec)
  124.  
  125. update `dt` set `a` = '1990-01-01' where `a` IS NULL;
  126. alter table `dt` change column `a` `a` date not null default '1990-01-01';
  127. update `tst` set `atime` = '1990-01-01' where `atime` IS NULL;
  128. alter table `tst` change column `atime` `atime` datetime not null default '1990-01-01';
  129.  
  130. CREATE VIEW migration_mytable AS
  131. SELECT field1, field2,
  132. CASE field3
  133. WHEN '0000-00-00 00:00:00'
  134. THEN '1900-01-01 00:00:00'
  135. ELSE field3
  136. END CASE AS field3
  137. FROM mytable;
  138.  
  139. INSERT INTO sqlserver.mytable SELECT * FROM mysql.migration_mytable;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement