在简单的 SQL 字符串中使用 TOP (VB6)

Using TOP in a simple SQL string (VB6)

我确信这个问题有一个非常简单的解决方案,但我已经尝试了所有我知道的方法但没有成功。

我正在尝试修改这个完全可用的 SQL 字符串以仅产生前 25 个结果,而不是所有结果。在此 post 上更正了空格。 (我已经使用 TOP 25 等 Jet/Access 数据库多年没有问题)

"SELECT * FROM " & "data.CSV" & " ORDER BY " & "Elapsed" & " DESC "*

我测试过的前 25 个 SQL 语法:

1."SELECT TOP 25 FROM " & "data.CSV" & " ORDER BY " & "Elapsed" & " DESC "
2."SELECT TOP 25 * FROM " & "data.CSV" & "ORDER BY " & "ELAPSED" & " DESC "
3."SELECT TOP 25" & " ORDER BY " & "ELAPSED" & " DESC " & FROM" & " data.CSV"

None 这三个产生来自 45 行测试数据文件的任何结果。没有提示错误,全部通过。

编辑:更正了工作字符串的空格。实际使用的代码是正确的。更正了缺少的双引号行 2。

任何建议表示赞赏。

编辑:添加了代码。

Private Sub Command4_Click() '*** Print direct to report ***
Dim Cn1 As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim strData, iSQLStr As String
Dim sData() As String
Dim c, i, s As Integer

 On Error Resume Next

  Set Cn1 = New ADODB.Connection
  Cn1.ConnectionString = _
    "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    "DefaultDir=" & "C:\"  
  Cn1.Open

Set rs = New ADODB.Recordset
rs.Fields.Append "Room", adVarChar, 2
rs.Fields.Append "Procedure", adVarChar, 2
rs.Fields.Append "Group", adVarChar, 12
rs.Fields.Append "Label", adVarChar, 12
rs.Fields.Append "Time_In", adVarChar, 22
rs.Fields.Append "Time_Out", adVarChar, 22
rs.Fields.Append "Elapsed", adVarChar, 8
rs.Open

'iSQLStr = "Select TOP 25" & " ORDER BY " & "Elapsed" & " DESC " & "FROM"   & " data.csv"
'iSQLStr = "Select TOP 25 * FROM" & " data.csv" & " ORDER BY " &  "Elapsed" & " DESC "
iSQLStr = "Select * FROM " & "data.csv" & " ORDER BY " & "Elapsed" & " DESC "
Set rs = Cn1.Execute(iSQLStr)

Set DataReport1.DataSource = rs
DataReport1.Show vbModal
rs.Close
End Sub

只需在查询末尾添加 LIMIT 选项。如果它按您想要的方式排序或排序,则只需将 LIMIT 25 添加到末尾,它将 return 前 25 行。

SELECT * FROM" & "data.CSV" & "ORDER BY " & "Elapsed" & " DESC **LIMIT 25**

我认为您缺少想要 select 的列。

SELECT TOP number|percent column_name(s)
FROM table_name;
"SELECT TOP 25 * FROM " & "data.CSV" & "ORDER BY " & "ELAPSED" & DESC "

是看起来最接近工作的那个,只是缺少一些空格。

这是什么语言的?我不知道符号和引号的全部含义,但如果您可以像这样构建字符串,它应该可以工作:

SELECT TOP 25 * FROM data.CSV ORDER BY ELAPSED DESC

根据您使用的语法,我认为这具有正确的间距:

"SELECT TOP 25 * FROM " & "data.CSV " & "ORDER BY " & "ELAPSED " & DESC"