Convert T-SQL to MySQL
Is there an easy way to convert Transact-SQL to MySQL?
I am attempting to convert a database archive of stock t开发者_JAVA百科icker symbols and company names.
The short answer: NO
The medium answer: MAYBE
The long answer: That depends on what's in your TSQL and how much time and effort you want to put into it.
TSQL is not a subset of MySQL's dialect. So there exists some TSQL for which there is no MySQL conversion. However, the overlap between the two languages is pretty significant, and to a certain degree the conversion is just a matter of syntax.
This isn't a new problem, and some people have tried to tackle it. A quick google search for "tsql to mysql" yields some promising results, notably this project here, which attempts to convert stored procedures from TSQL to MySQL:
http://sourceforge.net/projects/tsql2mysql/
This probably won't solve your problem completely, but it's at least a start.
This website converts to/from many sql database versions: http://www.sqlines.com/online
TSQL, MySql, Oracle, DB2, Sybase, Informix, Hive, MariaDB, PostgreSQL, RedShift, TeraData, Greenplum, Netezza
Are the others say, it depends how long your particular piece of string is.
But, I would suggest that you do NOT want to convert Transact-SQL to MySQL.
If you think about it, I think that you will find that you want to convert it to ODBC.
And then next year, when the company wants you to move it to Oracle, or Access ...
I've just achieved a tsql script.
My solution is :
- export table informations (schema pk)
- export table data
- export FK
I'm not very proud of my code, but it worked for me.
4 files wich are in the same folder:
Batch wich call sqlcmd
@echo off
set host= (local)
set schema=database
set user=user
set pass=pass
cd %cd%
rem tables
SQLCMD -S %host% -d %schema% -U %user% -P %pass% -s "" -h-1 -W -i "%CD%\mysql_export_table.sql" -o "%CD%\%schema%_tables.sql"
rem data
SQLCMD -S %host% -d %schema% -U %user% -P %pass% -s "" -h-1 -W -i "%CD%\mysql_export_data.sql" -o "%CD%\%schema%_data.sql"
rem fk
SQLCMD -S %host% -d %schema% -U %user% -P %pass% -s "" -h-1 -W -i "%CD%\mysql_export_fk.sql" -o "%CD%\%schema%_fk.sql"
then tsql script to export table schema mysql_export_table.sql
SET NOCOUNT ON;
SET QUOTED_IDENTIFIER ON;
DECLARE @table_name as varchar(max)
DECLARE view_cursor CURSOR FOR
SELECT Table_name FROM information_schema.tables where TABLE_TYPE = 'BASE TABLE' and table_name not like 'sys%'
OPEN view_cursor
FETCH NEXT FROM view_cursor
INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
select ''
select '/*** TABLE '+@table_name+' ***/ '
select 'DROP TABLE IF EXISTS ' + QUOTENAME(@table_name, '`') + ';'
select ''
select 'CREATE TABLE ' + QUOTENAME(@table_name, '`') + ' ('
-- column declaration
select
CHAR(9)
+ QUOTENAME(Column_Name, '`') + ' ' +
DATA_TYPE
+
coalesce( '(' + cast(coalesce(replace(CHARACTER_MAXIMUM_LENGTH, -1, 2500), null) as varchar) + ')', '')
+ ' ' +
case IS_NULLABLE WHEN 'NO' then 'NOT ' else '' end + 'NULL'
+ ' ' +
case when COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 then 'AUTO_INCREMENT' else '' end
--coalesce( 'DEFAULT ' + replace(replace(replace(COLUMN_DEFAULT, '(', ''), ')', ''), 'getdate', null), '')
+','
FROM information_schema.COLUMNS where TABLE_NAME = @table_name
-- PK
select coalesce('PRIMARY KEY (' +STUFF((
SELECT distinct ', ' + QUOTENAME(Col.Column_Name,'`') from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab inner join
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col on Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name
WHERE
Constraint_Type = 'PRIMARY KEY '
AND Col.Table_Name = @table_name
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '')+ ')', '')
select ') Engine InnoDB;'
FETCH NEXT FROM view_cursor
INTO @table_name
END
CLOSE view_cursor;
DEALLOCATE view_cursor;
then script for data mysql_export_data.sql
SET NOCOUNT ON;
SET QUOTED_IDENTIFIER ON;
DECLARE @table_name as varchar(max)
declare @column_names as varchar(max)
DECLARE view_cursor CURSOR FOR
SELECT Table_name FROM information_schema.tables where TABLE_TYPE = 'BASE TABLE' and table_name not like 'sys%'
OPEN view_cursor
FETCH NEXT FROM view_cursor
INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
select ''
select '/*** TABLE '+@table_name+' ***/ '
select @column_names = STUFF(( SELECT ', ' + QUOTENAME(Column_Name, '`') from INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = @table_name ORDER BY ORDINAL_POSITION FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '')
select 'REPLACE INTO '+ QUOTENAME(@table_name, '`') +'('+ @column_names+ ') VALUES '
select @column_names = 'SELECT DISTINCT ''('+ STUFF(( SELECT ', '','''''' + coalesce(replace(cast(' + QUOTENAME(Column_Name) +' as varchar(200)), '''''''',''''), '''') + ''''''''' from INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = @table_name ORDER BY ORDINAL_POSITION FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 4, '') + '+''),'' FROM ' + QUOTENAME(@table_name)
exec (@column_names)
FETCH NEXT FROM view_cursor
INTO @table_name
END
CLOSE view_cursor;
DEALLOCATE view_cursor;
Finally FK script
SET NOCOUNT ON;
SET QUOTED_IDENTIFIER ON;
-- FK
-- foreign keys
SELECT
'ALTER TABLE',
' '+ QUOTENAME(OBJECT_NAME(fkcol.[object_id]), '`'),
--ADD CONSTRAINT `recherche_ibfk_1` FOREIGN KEY (`notaire_compte_id`) REFERENCES `notaire_compte` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
' ADD CONSTRAINT',
' ' + QUOTENAME(fk.name, '`'),
' FOREIGN KEY',
' (' + QUOTENAME(fkcol.name, '`') +')',
' REFERENCES',
' ' + QUOTENAME(OBJECT_NAME(pkcol.[object_id]), '`'),
' (' + QUOTENAME(pkcol.name, '`') + ');',
CHAR(13)
FROM sys.foreign_keys AS fk
INNER JOIN sys.foreign_key_columns AS fkc
ON fk.[object_id] = fkc.constraint_object_id
INNER JOIN sys.columns AS fkcol
ON fkc.parent_object_id = fkcol.[object_id]
AND fkc.parent_column_id = fkcol.column_id
INNER JOIN sys.columns AS pkcol
ON fkc.referenced_object_id = pkcol.[object_id]
AND fkc.referenced_column_id = pkcol.column_id
ORDER BY fkc.constraint_column_id;
I know, i know... it's very ugly ...
The goal of this script is not to convert TSQL to Mysql but to export database from MSSQL to Mysql
On the table result you'll have to execute a regex replace (notepad++) replace ",\r\n\r\n)" by "\r\n\r\n)"
On the data result replace ",\r\n\r\n/" by ";\r\n\r\n/"
Execution order : Table -> Data -> FK
How about using SQL Server Linked Servers ? You can SELECT, INSERT, UPDATE and DELETE data from MySQL using TSQL
精彩评论