SQL 2005可以在case语句中使用关键字

前端之家收集整理的这篇文章主要介绍了SQL 2005可以在case语句中使用关键字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个允许部分匹配的case语句.我得到一个语法错误,但是我想知道是否有可能类似的事情.如果没有,我的第二个解决方案是重写为光标…
例:
SELECT CASE ColumnName
           WHEN 'value1' THEN 'answer1'
           WHEN 'value2' THEN 'answer2'
           WHEN LIKE '%TEST%' THEN 'answer3'
       END AS Answer
FROM TableName

解决方法

尝试这个
SELECT CASE 
           WHEN ColumnName = 'value1' THEN 'answer1'
           WHEN ColumnName = 'value2' THEN 'answer2'
           WHEN ColumnName LIKE '%TEST%' THEN 'answer3'
       END AS Answer
FROM TableName

你可以运行的例子

SELECT name,CASE 
           WHEN Name = 'sysobjects' THEN 'answer1'
           WHEN Name = 'syscols' THEN 'answer2'
           WHEN Name LIKE '%p%' THEN 'answer3'
           ELSE 'unknown'
       END AS Answer
FROM sysobjects
原文链接:https://www.f2er.com/mssql/75283.html

猜你在找的MsSQL相关文章