使用`SELECT *`时计算列的位置错误

编程入门 行业动态 更新时间:2024-10-28 12:24:38
本文介绍了使用`SELECT *`时计算列的位置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑以下使用SELECT *并附加"计算列的查询:

Consider this query that uses SELECT * and 'appends' a calculated column:

SELECT *, IIF(TRUE, 1, 0) AS calculated_col FROM Orders;

我希望calculated_col是结果集中最右边的列.但是,实际上它是最左侧的列.例如,在SQL Server中执行等效查询时,它是最右边的.

I would expect calculated_col to be the rightmost column in the resultset. However, it is in fact the leftmost column. It is the rightmost when executing the equivalent query in SQL Server, for example.

现在,由于这是Access(ACE,Jet等),因此SQL标准不适用,并且Access Help将不会指定期望的结果,因为它不够详细(礼貌地说).所以我的问题是:

Now, because this is Access (ACE, Jet, whatever), the SQL Standards don't apply and the Access Help will not specify the expected result because it is not detailed enough (to put it politely). So my questions are:

Access总是这样运行还是它是我的环境(ADO,OLE DB提供程序等)的功能"?

Does Access always behaved this way or is it a 'feature' of my environment (ADO, OLE DB provider, etc)?

在给定的环境中,Access总是表现出这种方式吗? (即为什么我以前没有注意到这一点?)

Has Access always behaved this way in the given environment? (i.e. Why haven't I noticed this before?)

P.S.我当然知道SELECT *被广泛嘲笑,并且如果列的顺序对我很重要,那么我应该明确地将它们全部写出.但是,我真的为所遇到的实际行为感到惊讶,并且对我的问题的解答很感兴趣.

P.S. I know of course that SELECT * is widely derided and that if the order of columns is important to me then I should write them all out explicitly. However, I was genuinely suprised at the actual behaviour encountered and am interested in any answers to my questions.

这里有一些VBA可重现此行为:只需将+复制粘贴到任何VBA模块中,无需设置引用,也不需要安装Access.使用Excel的VBA编辑器:

Here's some VBA to reproduce the behaviour: just copy+paste into any VBA module, no references need to be set and Access need not be installed e.g. use Excel's VBA editor:

Sub ColumnOrderWrong() On Error Resume Next Kill Environ$("temp") & "\DropMe.mdb" On Error GoTo 0 Dim cat Set cat = CreateObject("ADOX.Catalog") With cat .Create _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & _ Environ$("temp") & "\DropMe.mdb" With .ActiveConnection Dim Sql As String Sql = _ "CREATE TABLE Orders" & vbCr & _ "(" & vbCr & " ID INTEGER, " & vbCr & _ " customer_id" & _ " INTEGER" & vbCr & _ ");" .Execute Sql Sql = _ "INSERT INTO Orders (ID, customer_id) VALUES" & _ " (1, 2);" .Execute Sql Sql = _ "SELECT *, " & vbCr & _ " IIF(TRUE, 55, -99) AS calculated_col" & vbCr & _ " FROM Orders;" Dim rs Set rs = .Execute(Sql) MsgBox _ "Fields(0).Name = " & rs.Fields(0).Name & vbCr & _ "Fields(1).Name = " & rs.Fields(1).Name & vbCr & _ "Fields(2).Name = " & rs.Fields(2).Name End With Set .ActiveConnection = Nothing End With End Sub

推荐答案

从您已经完成的调查看来,您好像被塞满了.

From the investigations you've already done, it looks like you're stuffed.

您可能必须按名称而不是位置来引用记录集的列.

You may have to reference the record set's column's by name rather than position.

更多推荐

使用`SELECT *`时计算列的位置错误

本文发布于:2023-10-21 19:51:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1515249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时计   错误   位置   SELECT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!