SELECT 查询中的 OLEDB CASE WHEN
如何将 case when
语句与 oledb 一起使用到 Excel 文件?
就像select prodid,case prodid when 1 then 'fine' when 2 then 'good' end
How can use case when
statement with oledb to excel file ?
Like select prodid, case prodid when 1 then 'fine' when 2 then 'good' end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询 Excel 时,必须使用 IIF 而不是 CASE,例如
SELECT prodid, IIF(prodid = 1, 'fine', IIF(prodid = 2, 'good', ''))
FROM MyExcel
如您所见,它很快就会变得混乱。如果您只有双向评估,这还不算太糟糕,但在您的代码中,不清楚 prodid 是否只有 2 个可能的值或更多。我的示例假设还有其他值,因此是嵌套的 IIF。
You'll have to use IIF instead of CASE when querying Excel, eg
SELECT prodid, IIF(prodid = 1, 'fine', IIF(prodid = 2, 'good', ''))
FROM MyExcel
As you can see, it can get messy quickly. It's not too bad if you've only got a two-way evaluation, but in your code it's not clear whether you only have 2 possible values for prodid or more than that. My example assumes there are other values, hence the nested IIF.
OLEDB 只是某种“中继”,仅重复对底层源的查询。
我不相信 Excel 支持 CASE WHEN 构造,因此您无法使用它,即使通过 OLEDB 也是如此。
您可以改用 Excel 特定的习惯用法,即 IIF() 函数,如 CodeByMoonlight 的答案中所述。如前所述,对于需要超过 3 或 4 个案例的情况,需要嵌套 IIF() 调用是一个相当混乱的建议。
OLEDB is only a "relay" of sort and merely repeats the query to the underlying source.
I don't believe Excel supports the CASE WHEN construct, and thence you cannot use it, even through OLEDB.
You can instead use an Excel-specific idiom, the IIF() function, as described in CodeByMoonlight's answer. As noted the need to nest IIF() calls makes is a rather messy proposal for situations that require more than 3 or 4 cases.