Excel VBA:错误3704执行SQL语句时

时间:2018-03-27 06:45:28

标签: sql-server excel vba excel-vba

我有以下代码来执行sql语句并从服务器获取记录并将其粘贴到一个范围内:

Option Explicit
Const conString As String = "Provider = sqloledb;Server=dbsrv;Database=xxxx;User Id=xxxx;Password=xxxx;"
Public Function execSql(ByVal sql As String, ByVal pasteRange As Range) As Integer
On Error GoTo line

Dim rs  As ADODB.Recordset
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open conString

Set rs = New ADODB.Recordset

rs.Open sql, cn

pasteRange.CopyFromRecordset rs

rs.Close
cn.Close

execSql = 0
Exit Function
line:
execSql = 1
End Function

这是我尝试执行的SQL语句:

DECLARE @startDate date,@endDate date
SET @startDate = [sgdb].[GNR].sgfn_ShamsiDateToDate(1397,1,1)
SET @endDate = [sgdb].[GNR].sgfn_ShamsiDateToDate(1397,1,5)

DECLARE @tblSold as TABLE (hdrID bigint,vchNo int,vchDate date,cstmrRef int, sold money)
INSERT INTO @tblSold
SELECT H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef,SUM(I.Price) AS Sold FROM [sgdb].[SLE].[SLEFactHdr] AS H JOIN [sgdb].[SLE].[SLEFactItm] AS I
ON I.VchHdrRef = H.VchHdrId
WHERE H.VchDate BETWEEN @startDate AND @endDate AND H.[Status] = 1
GROUP BY H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef

DECLARE @tblRet as TABLE (hdrID bigint,vchNo int,vchDate date,cstmrRef int, ret money)
INSERT INTO @tblRet
SELECT  H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef,SUM(I.Price) AS Ret FROM [sgdb].[SLE].[SLERetFactHdr] H JOIN [sgdb].[SLE].[SLERetFactItm] I ON I.VchHdrRef = H.VchHdrId
WHERE H.VchDate BETWEEN @startDate AND @endDate AND H.[Status] = 1
GROUP BY H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef

DECLARE @tblTax as TABLE (hdrID bigint, taxPrice money,taxType int,sr bit)
INSERT INTO @tblTax
SELECT I.VchHdrRef,SUM(TaxPrice),T.TaxType,0 FROM [sgdb].[SLE].[SLEFactItm] I JOIN [sgdb].[SLE].[SLEFactTax] FT ON FT.VchItmRef = I.VchItmId JOIN
[sgdb].[SLE].[SLETaxes] T ON T.TaxID = FT.TaxRef JOIN @tblSold TS ON TS.hdrID = I.VchHdrRef
GROUP BY I.VchHdrRef,T.TaxType

INSERT INTO @tblTax
SELECT I.VchHdrRef,SUM(TaxPrice),T.TaxType,1 FROM [sgdb].[SLE].[SLERetFactItm] I JOIN [sgdb].[SLE].[SLEFactTax] FT ON FT.VchItmRef = I.VchItmId JOIN
[sgdb].[SLE].[SLETaxes] T ON T.TaxID = FT.TaxRef JOIN @tblSold TS ON TS.hdrID = I.VchHdrRef
GROUP BY I.VchHdrRef,T.TaxType

DECLARE @tblFinalT as Table (cstmrRef int, cstmrName nvarchar(250), sold money,ret money,s_add money,s_dis money,r_add money,r_dis money)
INSERT INTO @tblFinalT
SELECT cstmrRef,'',sold,0,0,0,0,0 FROM @tblSold
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,ret,0,0,0,0 FROM @tblRet
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,T.taxPrice,0,0,0 FROM @tblTax T JOIN @tblSold S ON S.hdrID = T.hdrID WHERE T.sr = 0 AND T.taxType = 0
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,T.taxPrice,0,0 FROM @tblTax T JOIN @tblSold S ON S.hdrID = T.hdrID WHERE T.sr = 0 AND T.taxType = 1
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,0,T.taxPrice,0 FROM @tblTax T JOIN @tblRet R ON R.hdrID = T.hdrID WHERE T.sr = 1 AND T.taxType = 0
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,0,0,T.taxPrice FROM @tblTax T JOIN @tblRet R ON R.hdrID = T.hdrID WHERE T.sr = 1 AND T.taxType = 1

UPDATE @tblFinalT SET cstmrName = C.CstmrName FROM [sgdb].[SLE].[vwSLECstmrCrspnd] C WHERE cstmrRef = C.CstmrCode

DECLARE @tblFinal as Table (cstmrRef int, cstmrName nvarchar(250), sold money,ret money,s_add money,s_dis money,r_add money,r_dis money, total money)

INSERT INTO @tblFinal
SELECT T.cstmrRef,T.cstmrName,SUM(T.sold),SUM(T.ret),SUM(T.s_add),SUM(T.s_dis),SUM(T.r_add),SUM(T.r_dis),0 FROM @tblFinalT T
GROUP BY T.cstmrRef,T.cstmrName

UPDATE @tblFinal SET total = sold - ret + s_add - s_dis - r_add + r_dis

SELECT * FROM @tblFinal

当我尝试执行上述语句时,我收到错误

  

' 3704' :应用程序定义的错误或对象定义的错误。

通常当发生这种情况时,语句必定有问题,但我运行了SQL Server Profiler,看起来代码很好。我在SQL Server本身中使用相同的代码,没有问题。事实上,Server实际上发回了数据,但Excel VBA正在收到错误。

我尝试了很多代码,当我尝试在我的sql语句中声明一个临时表时,弹出这个错误。如果我将此声明转换为存储过程,那么代码就可以了,但我不想创建SP。

这是一个糟糕的字符串连接吗?或者只是excel无法处理这个?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用

启动SQL查询

set nocount on;

在第一行SQL代码之前,它将起作用。