gpt4 book ai didi

excel - 预期的 SQL 语句无效; ‘DELETE’,'插入',

转载 作者:行者123 更新时间:2023-12-04 21:02:21 30 4
gpt4 key购买 nike

我有一个代码

Dim Cn As New ADODB.Connection

Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim i As Long

Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 8.0;"""
Cn.Open
cmd.ActiveConnection = Cn
cmd.CommandText = "DECLARE @TS_HD DOUBLE;" & _
"SET @TS_HD = ?TS_HD;" & _
"SELECT So_HD, Ngay_HD, Ten_Khach_Hang, Ma_So_Thue, Sum(Doanh_So_KT) as DoanhSo, Sum(Thue_VAT) as VAT from [BR$] " & _
"Where Ngay_HD <> null And TS_HD = @TS_HD Group by So_HD, Ma_So_Thue,Ngay_HD, Ten_Khach_Hang Order by Ngay_HD"

Set TS_HD = cmd.CreateParameter("?TS_HD", adDouble, adParamInput)
cmd.Parameters.Append TS_HD
TS_HD.Value = 0.1 'in excel this value is 10%'

Dim rst As ADODB.Recordset
Set rst = cmd.Execute()

BRToTalKhongChiuThue() = rst.GetRows

rst.Close
Cn.Close

当我运行此代码 VBA 显示错误“预期的 SQL 语句无效;‘DELETE’,‘INSERT’”时,这是不是有些磨损?
感谢您查看我的问题!

最佳答案

参数化查询的语法看起来不正确。

DECLARE @TS_HD DOUBLE;
SET @TS_HD = ?TS_HD;
SELECT
So_HD
,Ngay_HD
,Ten_Khach_Hang
,Ma_So_Thue
,SUM(Doanh_So_KT) AS DoanhSo
,SUM(Thue_VAT) AS VAT
FROM [BR$]
WHERE Ngay_HD <> NULL AND TS_HD = @TS_HD
GROUP BY
So_HD
,Ma_So_Thue
,Ngay_HD
,Ten_Khach_Hang
ORDER BY
Ngay_HD


我不确定提供商是否支持 DECLARE陈述。如果没有,这就是您收到此消息的原因。让我们尝试删除它:
SELECT 
So_HD
,Ngay_HD
,Ten_Khach_Hang
,Ma_So_Thue
,SUM(Doanh_So_KT) AS DoanhSo
,SUM(Thue_VAT) AS VAT
FROM [BR$]
WHERE Ngay_HD <> NULL AND TS_HD = ?
GROUP BY
So_HD
,Ma_So_Thue
,Ngay_HD
,Ten_Khach_Hang
ORDER BY
Ngay_HD

注意 WHERE 子句 WHERE Ngay_HD <> NULL AND TS_HD = ? - 参数占位符只是一个问号。

我很确定这种说法会奏效。

我不太确定参数是如何传入的:
Set TS_HD = cmd.CreateParameter("?TS_HD", adDouble, adParamInput)

我认为您可以删除 name 参数,它是可选的 - 有些提供程序不支持命名参数。但这很糟糕,因为现在您需要为 Name 之后的参数命名。 CreateParameter 的签名中的第一个参数:
Set TS_HD = cmd.CreateParameter(Type:=adDouble, Direction:=adParamInput)

替代方案可能是 New它启动并“手动”初始化它:
Dim param As New ADODB.Parameter
With param
.Type = adDouble
.Direction = adParamInput
.Value = 0.01 'in excel this value is 10% '?? really? it's 1% here!
End With

cmd.Parameters.Append param

关于excel - 预期的 SQL 语句无效; ‘DELETE’,'插入',,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33886331/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com