sql - 对 Excel 单元格的 SQL 脚本引用
问题描述
Sub SQL()
'Clearing the existing cells
Application.DisplayAlerts = False
Sheet1.Range("A1:D10000").SpecialCells(xlCellTypeVisible).Clear
Application.DisplayAlerts = True
'Setting up the connection
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim m As New ADODB.Recordset
Dim DBPath As String, sconnect As String
sconnect = "Provider=SQLOLEDB; Data Source=backoffice.db.mid.dom,4161; Initial Catalog=BackOffice; Integrated Security=SSPI;"
Conn.Open sconnect
"'SQL Script
'DB1 = ThisWorkbook.Sheets("Import to SQL").Range(c2").Value
sSQLString = ThisWorkbook.Sheets("Scripts").Range("C2:C3").Value
m.Open sSQLString, Conn
'Paste Date and Close Connection
Sheet1.Range("A21").CopyFromRecordset m
m.Close
'Set/name the column header
Sheet1.Cells(20, 1) = "ProductCode"
Sheet1.Cells(20, 2) = "Nr of Trades"
Sheet1.Cells(20, 3) = "Trade_date"
Sheet1.Cells(20, 4) = "Date_Retrieved"
Conn.Close
End Sub
大家好, 当前放置在 1 个单元格 (C2) 中的 SQL 脚本有问题。我想要做的是传播脚本,以便每行脚本有 1 个单元格。当我将脚本分成 2 个单元格并且它位于 C2 和 C3 中时,VBA 会给出错误 3001。
C2 ="Select ProductCode, FORMAT(count(distinct(tradeid)),'#,#') as Nr_Trades, TradeDate, GETDATE() From BackOffice.dbo.trades
where 1 = 1
and TradeDate ="&E2&"group by ProductCode, TradeDate"
C3 ="order by count(distinct(tradeid)) asc"
解决方案
推荐阅读
- java - If the database table column is set with auto_increment then why are we using @generatedValue
- sql-server - Azure CLI - 如何将标签添加到 Azure Sql 服务器(逻辑服务器)?
- hyperlink - guidewire 政策中心的外部超链接
- c# - IContextChannel client.InnerChannel 来自这个 WSDL?
- node.js - 如何使用 nightwatch node js 在浏览器堆栈上自动上传文件
- python-3.x - 合并两个名称/或仅从 CSV 文件中提取名称
- python - cv2.imshow() 在自定义函数中不起作用
- c++ - 如何修复柠檬中的“错误:调用'YYMINORTYPE'的隐式删除的默认构造函数”?
- javascript - 如何将String转换为int数组
- reactjs - 将数据推送到数组时反应功能组件不更新