This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmssql2mysql.php
224 lines (190 loc) · 5.97 KB
/
mssql2mysql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* SOURCE: MS SQL
*/
define('MSSQL_HOST','mssql_host');
define('MSSQL_USER','mssql_user');
define('MSSQL_PASSWORD','mssql_password');
define('MSSQL_DATABASE','mssql_database');
/*
* DESTINATION: MySQL
*/
define('MYSQL_HOST', 'mysql_host');
define('MYSQL_USER', 'mysql_user');
define('MYSQL_PASSWORD','mysql_password');
define('MYSQL_DATABASE','mysql_database');
/*
* SOME HELPER CONSTANT
*/
define('CHUNK_SIZE', 1000);
/*
* STOP EDITING!
*/
set_time_limit(0);
function addQuote($string)
{
return "'".$string."'";
}
function addTilde($string)
{
return "`".$string."`";
}
// Connect MS SQL
$mssql_connect = mssql_connect(MSSQL_HOST, MSSQL_USER, MSSQL_PASSWORD) or die("Couldn't connect to SQL Server on '".MSSQL_HOST."'' user '".MSSQL_USER."'\n");
echo "=> Connected to Source MS SQL Server on '".MSSQL_HOST."'\n";
// Select MS SQL Database
$mssql_db = mssql_select_db(MSSQL_DATABASE, $mssql_connect) or die("Couldn't open database '".MSSQL_DATABASE."'\n");
echo "=> Found database '".MSSQL_DATABASE."'\n";
// Connect to MySQL
$mysql_connect = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die("Couldn't connect to MySQL on '".MYSQL_HOST."'' user '".MYSQL_USER."'\n");
echo "\n=> Connected to Source MySQL Server on ".MYSQL_HOST."\n";
// Select MySQL Database
$mssql_db = mysql_select_db(MYSQL_DATABASE, $mysql_connect) or die("Couldn't open database '".MYSQL_DATABASE."'\n");
echo "=> Found database '".MYSQL_DATABASE."'\n";
$mssql_tables = array();
// Get MS SQL tables
$sql = "SELECT * FROM sys.Tables;";
$res = mssql_query($sql);
echo "\n=> Getting tables..\n";
while ($row = mssql_fetch_assoc($res))
{
array_push($mssql_tables, $row['name']);
//echo ($row['name'])."\n";
}
echo "==> Found ". number_format(count($mssql_tables),0,',','.') ." tables\n\n";
// Get Table Structures
if (!empty($mssql_tables))
{
$i = 1;
foreach ($mssql_tables as $table)
{
echo '====> '.$i.'. '.$table."\n";
echo "=====> Getting info table ".$table." from SQL Server\n";
$sql = "select * from information_schema.columns where table_name = '".$table."'";
$res = mssql_query($sql);
if ($res)
{
$mssql_tables[$table] = array();
$mysql = "DROP TABLE IF EXISTS `".$table."`";
mysql_query($mysql);
$mysql = "CREATE TABLE `".$table."`";
$strctsql = $fields = array();
while ($row = mssql_fetch_assoc($res))
{
//print_r($row); echo "\n";
array_push($mssql_tables[$table], $row);
switch ($row['DATA_TYPE']) {
case 'bit':
case 'tinyint':
case 'smallint':
case 'int':
case 'bigint':
$data_type = $row['DATA_TYPE'].(!empty($row['NUMERIC_PRECISION']) ? '('.$row['NUMERIC_PRECISION'].')' : '' );
break;
case 'money':
$data_type = 'decimal(19,4)';
break;
case 'smallmoney':
$data_type = 'decimal(10,4)';
break;
case 'real':
case 'float':
case 'decimal':
case 'numeric':
$data_type = $row['DATA_TYPE'].(!empty($row['NUMERIC_PRECISION']) ? '('.$row['NUMERIC_PRECISION'].(!empty($row['NUMERIC_SCALE']) ? ','.$row['NUMERIC_SCALE'] : '').')' : '' );
break;
case 'date':
case 'datetime':
case 'timestamp':
case 'time':
$data_type = $row['DATA_TYPE'];
case 'datetime2':
case 'datetimeoffset':
case 'smalldatetime':
$data_type = 'datetime';
break;
case 'nchar':
case 'char':
$data_type = 'char'.(!empty($row['CHARACTER_MAXIMUM_LENGTH']) && $row['CHARACTER_MAXIMUM_LENGTH'] > 0 ? '('.$row['CHARACTER_MAXIMUM_LENGTH'].')' : '(255)' );
break;
case 'nvarchar':
case 'varchar':
$data_type = 'varchar'.(!empty($row['CHARACTER_MAXIMUM_LENGTH']) && $row['CHARACTER_MAXIMUM_LENGTH'] > 0 ? '('.$row['CHARACTER_MAXIMUM_LENGTH'].')' : '(255)' );
break;
case 'ntext':
case 'text':
$data_type = 'text';
break;
case 'binary':
case 'varbinary':
$data_type = $data_type = $row['DATA_TYPE'];
case 'image':
$data_type = 'blob';
break;
case 'uniqueidentifier':
$data_type = 'char(36)';//'CHAR(36) NOT NULL';
break;
case 'cursor':
case 'hierarchyid':
case 'sql_variant':
case 'table':
case 'xml':
default:
$data_type = false;
break;
}
if (!empty($data_type))
{
$ssql = "`".$row['COLUMN_NAME']."` ".$data_type." ".($row['IS_NULLABLE'] == 'YES' ? 'NULL' : 'NOT NULL');
array_push($strctsql, $ssql);
array_push($fields, $row['COLUMN_NAME']);
}
}
$mysql .= "(".implode(',', $strctsql).");";
echo "======> Creating table ".$table." on MySQL... ";
$q = mysql_query($mysql);
echo (($q) ? 'Success':'Failed!'."\n".$mysql."\n")."\n";
echo "=====> Getting data from table ".$table." on SQL Server\n";
$sql = "SELECT * FROM ".$table;
$qres = mssql_query($sql);
$numrow = mssql_num_rows($qres);
echo "======> Found ".number_format($numrow,0,',','.')." rows\n";
if ($qres)
{
echo "=====> Inserting to table ".$table." on MySQL\n";
$numdata = 0;
if (!empty($fields))
{
$sfield = array_map('addTilde', $fields);
while ($qrow = mssql_fetch_assoc($qres))
{
$datas = array();
foreach ($fields as $field)
{
$ddata = (!empty($qrow[$field])) ? $qrow[$field] : '';
array_push($datas,"'".mysql_real_escape_string($ddata)."'");
}
if (!empty($datas))
{
//$datas = array_map('addQuote', $datas);
//$fields =
$mysql = "INSERT INTO `".$table."` (".implode(',',$sfield).") VALUES (".implode(',',$datas).");";
//$mysql = mysql_real_escape_string($mysql);
//echo $mysql."\n";
$q = mysql_query($mysql);
$numdata += ($q ? 1 : 0 );
}
if ($numData % CHUNK_SIZE == 0) {
echo "===> ".number_format($numdata,0,',','.')." data inserted so far\n";
}
}
}
echo "======> ".number_format($numdata,0,',','.')." data inserted total\n\n";
}
}
$i++;
}
}
echo "Done!\n";
mssql_close($mssql_connect);
mysql_close($mysql_connect);