首页 > 解决方案 > IS NOT NULL AS ... - 将查询从 MySQL 转换为 MS-SQL

问题描述

我有一个使用 MySQL/MariaDB 数据库运行良好的应用程序。
我确实让它更灵活,现在我基本上可以使用 Microsoft SQL-Server 数据库了。

我发现,一些 SQL 查询不再起作用。
我没有使用 MS-SQL 的经验,我正在寻求支持以转换以下查询以使其与 MS-SQL 一起使用。如果查询可以转换为在 MS-SQL 和 MySQL 中工作,那就太好了……

我用一些示例数据创建了一个 SQL-Fiddle。
链接:http ://sqlfiddle.com/#!18/5fb718/2

查询本身如下所示:

SELECT computermapping.PrinterGUID, computerdefaultprinter.PrinterGUID IS NOT NULL AS isDefaultPrinter
FROM computermapping 
LEFT JOIN computerdefaultprinter ON computerdefaultprinter.ComputerGUID = computermapping.ComputerGUID 
AND computerdefaultprinter.PrinterGUID = computermapping.PrinterGUID
WHERE computermapping.ComputerGUID = "5bec3779-b002-46ba-97c4-19158c13001f"

当我在 SQL-Fiddle 上运行它时,我收到以下错误:

关键字“IS”附近的语法不正确。

当我在 Microsoft SQL Server Management Studio 中运行此查询时,我得到了同样的错误。我有一个德语安装...

Meldung 156, Ebene 15, Status 1, Zeile 1
Falsche Syntax in der Nähe des IS-Schlüsselworts。

我在 Internet 上查找有关如何IS NOT NULL AS在 MS-SQL 中使用的信息。也许我使用了错误的关键字,但我自己无法找到解决方案。

如果它确实重要,我目前正在使用“SQL-Server 2014 SP3”。

谢谢

标签: sql-server

解决方案


转变

computerdefaultprinter.PrinterGUID IS NOT NULL AS isDefaultPrinter

CASE 
   WHEN computerdefaultprinter.PrinterGUID IS NOT NULL THEN 1 
   ELSE 0 
END AS isDefaultPrinter

演示在这里

另请记住,BOOLEANSQL Server 中没有类型。BIT改为使用类型。

最后

WHERE computermapping.ComputerGUID = "5bec3779-b002-46ba-97c4-19158c13001f"

应转换为

WHERE computermapping.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'

因为单引号字符用于分隔 SQL Server 中的字符串


推荐阅读