SELECT 查询中的 OLEDB CASE WHEN

发布于 2024-08-11 23:05:45 字数 146 浏览 2 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-08-18 23:05:46

查询 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.

谁许谁一生繁华 2024-08-18 23:05:46

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文