[英]sql query and concat is not working, error throwing on every change
[英]SQL Query not working in VBA. throwing Automation Error
有人可以抬頭告訴我下面的VB代碼有什么問題嗎
mssql1 = "select TOP 1 [Test Date]from [AllSamples].[dbo].[Results](select TOP 2[Test Date] from [AllSamples].[dbo].[Results] where ([Sample ID] = '" & Split(Sheet1.Cells(i, 6), "-")(0) & "' And [Sample No.] = '" & Split(Sheet1.Cells(i, 6), "-")(1) & "') Order BY [Test Date] DESC) Order BY [Test Date] ASC"
它獲取標簽號並將其相應地分為樣品ID和樣品號。 請更正查詢
在您的SQL中,您缺少TOP 1之后的字段列表,然后是子查詢之前的缺失字段和子查詢的別名。 應該是這樣的:
mssql1 = "select TOP 1 * from " & _
" (select TOP 2 [Test Date] from [AllSamples].[dbo].[Results] " & _
" ... Order BY [Test Date] DESC) tmp Order BY [Test Date] ASC"
並且,不要忘記使用參數來代替那些字符串連接,以防止SQL注入攻擊和其他字符串問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.