sql-server - 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”。
谢谢
解决方案
转变
computerdefaultprinter.PrinterGUID IS NOT NULL AS isDefaultPrinter
到
CASE
WHEN computerdefaultprinter.PrinterGUID IS NOT NULL THEN 1
ELSE 0
END AS isDefaultPrinter
另请记住,BOOLEAN
SQL Server 中没有类型。BIT
改为使用类型。
最后
WHERE computermapping.ComputerGUID = "5bec3779-b002-46ba-97c4-19158c13001f"
应转换为
WHERE computermapping.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
因为单引号字符用于分隔 SQL Server 中的字符串
推荐阅读
- javascript - 如何在浏览器控制台中检测按键事件?
- java - 使用流的 BlockRealMatrix 对象的平均对应行
- python-3.x - 动态 SQLAlchemy ORM 关系生成
- ruby-on-rails - 使用 sunspot_rails 在 solr 中为日期时间字段的输入字符串添加字段“created_at_d”时出错
- c# - Azure 平台/Dotnet 核心上的可靠图像处理
- c - fgets 存储来自文本文件的未知数据
- django - Django - Django 如何访问 sqlite 中的数据?
- pandas - 我应该如何在分类列中估算 Nan 值?
- jquery - 选项/选择菜单不显示 HTML/Bootstrap
- css - 电子商务网站的产品定制