如何将MS SQL查询转换为MySQL查询

时间:2022-05-18 15:47:03

I have a MSSQL queries file(.sql), now I need to convert it to MYSQL queries. Please help me. The script like this:

我有一个MSSQL查询文件(.sql),现在我需要将其转换为MYSQL查询。请帮帮我。像这样的脚本:

CREATE TABLE [dbo].[Artist](
    [ArtistId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](120) NULL,
PRIMARY KEY CLUSTERED 

(
    [ArtistId] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

1 个解决方案

#1


3  

If you want to convert the DDL by hand, then you can do this by building up rules on a case by case basis, e.g. as follows:

如果您想手动转换DDL,那么您可以通过逐个建立规则来实现这一点,例如:如下:

  • [] need to be replaced with backticks
  • []需要用反引号替换

  • IDENTITY(1,1) can be replaced with AUTO_INCREMENT
  • IDENTITY(1,1)可以替换为AUTO_INCREMENT

  • Most of the ANSI options and Device settings can be ignored (these seem to be present only because the table has been rescripted)
  • 大多数ANSI选项和设备设置都可以忽略(这些似乎只是因为表已被重新编写而存在)

  • w.r.t. dbo, MySQL doesn't implement schemas in the same way as SQL Server - you will either need to separate schemas into databases, or drop the schema, or mangle the schema name into the tablename (e.g. as a Prefix)
  • w.r.t. dbo,MySQL没有像SQL Server一样实现模式 - 你需要将模式分离到数据库中,或者删除模式,或者将模式名称变成表名(例如作为前缀)

This will leave you with something like the following:

这将为您留下如下内容:

CREATE TABLE `Artist`(
    `ArtistId` int NOT NULL AUTO_INCREMENT,
    `Name` nvarchar(120) NULL,
    PRIMARY KEY CLUSTERED 
    (
       `ArtistId` ASC
    )
);

Fiddle here

However, it is usually much easier to do this migration with a migration tool - search for the section on How to Transition from SQL Server to MySQL

但是,使用迁移工具进行此迁移通常要容易得多 - 搜索有关如何从SQL Server转换到MySQL的部分

#1


3  

If you want to convert the DDL by hand, then you can do this by building up rules on a case by case basis, e.g. as follows:

如果您想手动转换DDL,那么您可以通过逐个建立规则来实现这一点,例如:如下:

  • [] need to be replaced with backticks
  • []需要用反引号替换

  • IDENTITY(1,1) can be replaced with AUTO_INCREMENT
  • IDENTITY(1,1)可以替换为AUTO_INCREMENT

  • Most of the ANSI options and Device settings can be ignored (these seem to be present only because the table has been rescripted)
  • 大多数ANSI选项和设备设置都可以忽略(这些似乎只是因为表已被重新编写而存在)

  • w.r.t. dbo, MySQL doesn't implement schemas in the same way as SQL Server - you will either need to separate schemas into databases, or drop the schema, or mangle the schema name into the tablename (e.g. as a Prefix)
  • w.r.t. dbo,MySQL没有像SQL Server一样实现模式 - 你需要将模式分离到数据库中,或者删除模式,或者将模式名称变成表名(例如作为前缀)

This will leave you with something like the following:

这将为您留下如下内容:

CREATE TABLE `Artist`(
    `ArtistId` int NOT NULL AUTO_INCREMENT,
    `Name` nvarchar(120) NULL,
    PRIMARY KEY CLUSTERED 
    (
       `ArtistId` ASC
    )
);

Fiddle here

However, it is usually much easier to do this migration with a migration tool - search for the section on How to Transition from SQL Server to MySQL

但是,使用迁移工具进行此迁移通常要容易得多 - 搜索有关如何从SQL Server转换到MySQL的部分