sql-server - 'AvayaSBCCRT' 附近的语法不正确

标签 sql-server vba excel

我真的很抱歉问这个问题,我确信回答起来非常简单,但是每当我尝试在下面的 excel 中运行宏时,我都会收到标题中所述的错误消息:

Sub CallsMacro()

Dim ConData As ADODB.Connection
Dim rstData As ADODB.Recordset
Dim wsSheet As Worksheet
Dim strServer As String
Dim strDatabase As String
Dim strFrom As String
Dim strto As String
Dim intCount As Integer

Set wsSheet = ActiveWorkbook.Worksheets("Refresh")
With wsSheet
strServer = "TNS-CCR-02"
strDatabase = "AvayaSBCCRT"
strFrom = .Range("C$2")
strto = .Range("C$3")
End With

Set ConData = New ADODB.Connection
With ConData
.ConnectionString = "Provider=SQLOLEDB;Data Source=" & strServer & ";" & "Initial Catalog=" & ";" & "persist security info=true;" & "User Id=dashboard; Password=D4$hboard;"
.CommandTimeout = 1800
.Open
End With

''Create the recordset from the SQL query
Set rstData = New ADODB.Recordset

Set wsSheet = ActiveWorkbook.Worksheets("Calls")

With rstData
.ActiveConnection = ConData
.Source = "SELECT DISTINCT CAST(c.createdate AS date) as [Date]," & _
"CASE WHEN c.[CategoryID] = 1 then 'Outbound' WHEN c.[CategoryID] = 2 then         'Inbound' Else 'Internal' end as [Direction], c.cli as [Number], c.ddi, 'CallCentre' as [Queue], '' as [Queue Time], u.username as [Agent], cast((c.DestroyDate - c.CreateDate) as TIME) as [Duration], 'Connected' as [Status], c.callID as [Reference]" & _
"FROM [AvayaSBCCRT].[dbo].[tblAgentActivity] as a" & _
"JOIN [AvayaSBCCRT].[dbo].[tblCallList] as c on c.calllistid = a.calllistid" & _
"JOIN [AvayaSBCCRT].[dbo].[tblUsers] as u on u.userid = a.AgentID" & _
"WHERE c.createdate between '" & strFrom & "' and '" & strto & "'" & _
"AND a.[ActivityID] = 3 "
.CursorType = adOpenForwardOnly
.Open
End With

wsSheet.Activate

Dim Lastrow As Long

Lastrow = Range("A" & Rows.Count).end(xlUp).Row

Range("A2:J" & Lastrow).ClearContents

If rs.EOF = False Then wsSheet.Cells(2, 1).CopyFromRecordset rsData


rs.Close
Set rs = Nothing
Set cmd = Nothing


con.Close
Set con = Nothing


End Sub

我从高处和低处看都找不到原因。有人有什么想法吗?

最佳答案

您在行尾缺少空格。您的 SQL 包含例如:

[tblAgentActivity] as aJOIN [AvayaSBCCRT].[dbo].[tblCallList]

关于sql-server - 'AvayaSBCCRT' 附近的语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439604/

相关文章:

sql-server - 临时表和存储过程编译

sql-server - 禁止的架构名称 : SYS, DBO (MsSQL)

vba - 使用 VBA Selection.AutoFill 提高性能

Excel 使用宏复制公式

vba - 在Excel VBA中查找单元格引用的简单方法

sql-server - SQL Server Management Studio 中 JOIN 的智能感知

sql-server - 给定 Azure 中 SQL 数据库上的 SQL 查询的查询 ID,是否有办法返回并跟踪谁发起了查询?

c# - 解析excel文件的好资源?

excel - 使用 AutoFilter 过滤列

excel - 获取列中第一个空单元格的行号并将该值存储在其他单元格中