将T-SQL转换为MySQL

时间:2010-06-06 23:46:37

标签: mysql tsql

有没有一种简单的方法可以将Transact-SQL转换为MySQL?


我正在尝试转换股票代码和公司名称的数据库存档。

5 个答案:

答案 0 :(得分:20)

答案简短:

中等回答: MAYBE

答案很长:这取决于您的TSQL中的内容以及您希望投入多少时间和精力。

TSQL不是MySQL方言的子集。所以存在一些没有MySQL转换的TSQL。但是,两种语言之间的重叠非常重要,在某种程度上,转换只是语法问题。

这不是一个新问题,有些人试图解决它。快速谷歌搜索“tsql to mysql”产生一些有希望的结果,特别是这个项目,它试图将存储过程从TSQL转换为MySQL:

http://sourceforge.net/projects/tsql2mysql/

这可能不会完全解决你的问题,但它至少是一个开始。

答案 1 :(得分:2)

此网站转换为多个sql数据库版本: http://www.sqlines.com/online

TSQL,MySql,Oracle,DB2,Sybase,Informix,Hive,MariaDB,PostgreSQL,RedShift,TeraData,Greenplum,Netezza

答案 2 :(得分:1)

我刚刚获得了一个tsql脚本。

我的解决方案是:

  1. 导出表格信息(schema pk)
  2. 导出表格数据
  3. export FK
  4. 我对我的代码并不感到骄傲,但它对我有用。

    4个文件位于同一个文件夹中:

    批量调用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" 
    

    然后tsql脚本导出表模式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;
    

    然后是数据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;
    

    最后FK脚本

    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;
    

    我知道,我知道......它非常难看......

    此脚本的目标不是将TSQL转换为Mysql,而是将数据库从MSSQL导出到Mysql

    在表格结果上你必须执行正则表达式替换(记事本++) 替换",\ r \ n \ r \ n)" by" \ r \ n \ r \ n)"

    在数据结果上替换",\ r \ n \ r \ n / " by"; \ r \ n \ r \ n / "

    执行顺序:表格 - >数据 - > FK

答案 3 :(得分:0)

其他人是否说,这取决于你的特定字符串的长度。

但是,我建议您不要将Transact-SQL转换为MySQL。

如果您考虑一下,我认为您会发现要将其转换为ODBC

然后明年,公司希望您将其转移到Oracle或Access ...

答案 4 :(得分:0)

如何使用SQL Server链接服务器?您可以使用TSQL从MySQL中选择,插入,更新和删除数据