简体   繁体   English

临时表问题并插入 VBA 中的语句(ADODB 查询)

[英]Issue with temp table and insert into statement in VBA (ADODB query)

I use VBA and the ADODB to query against a SQL Server server and having trouble with INSERT INTO statement in the following string:我使用 VBA 和 ADODB 来查询 SQL Server 服务器,并且在使用以下字符串中的 INSERT INTO 语句时遇到问题:

StrQuery = "INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28)," & _
" ('RRA','Care: Quality',29)"

The error message is "Incorrect syntax near ','. I'm sure something is wrong with the above statement but I'm struggling to figure our what might be wrong.错误消息是“','附近的语法不正确。我确定上面的语句有问题,但我正在努力弄清楚我们可能有什么问题。

I would like to insert the following values into the table (it does work perfectly in SQL):我想将以下值插入表中(它在 SQL 中运行良好):

INSERT INTO #temp_ImprtSampVol 
VALUES 
('RBM','Care: Admin',28),
('RBM','Care: Exit',31),
('RBM','Care: Other',22),
('RRA','Care: Quality',29),
('RRA','Care: Finance',37),

My code is as follows:我的代码如下:

Sub VBAFromSQL()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String

ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=HIDDEN_Outputs;Data Source=HIDDEN;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"

cnn.Open ConnectionString
cnn.CommandTimeout = 900

StrQuery = "IF OBJECT_ID('tempdb..#temp_MthToDt') IS NOT NULL drop table 
#temp_MthToDt" & _
" IF OBJECT_ID('tempdb..#temp_ImprtSampVol') IS NOT NULL drop table 
#temp_ImprtSampVol" & _
" IF OBJECT_ID('tempdb..#temp_T1') IS NOT NULL drop table #temp_T2AuditData"

StrQuery = "DECLARE @Last_Day_Loaded VARCHAR(30) = '2018-10-03';" & _
" DECLARE @Start_of_Sample VARCHAR(30) = '2018-10-03';" & _
" DECLARE @End_of_Sample VARCHAR(30) = '2018-10-03';" & _
" DECLARE @Days_Data_so_far INT = 03;" & _
" DECLARE @First_Day_Month VARCHAR(30) = '2018-10-01';" & _
" DECLARE @Working_Days_in_Month INT = 23;" & _
" DECLARE @Days_in_Month INT = 31;" & _
" DECLARE @BBE_Sample_Minimum INT = 10;" & _
" DECLARE @BBE_Sample_adjustment_multiplier decimal(5,2) = 1.0;"

StrQuery = "CREATE TABLE #temp_ImprtSampVol (" & _
" CIS VARCHAR(10) NOT NULL," & _
" RD VARCHAR(40) NOT NULL," & _
" Sample_to_send INT NOT NULL );"

StrQuery = "INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28)," & _
" ('RRA','Care: Quality',29)"

rst.Open StrQuery, cnn
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub

Can you please advise how to rectify this?你能建议如何纠正这个问题吗? Thanks in advance.提前致谢。

As Sean Lange commented an insert statement doesn't return anything usefull but a confirmation of the query result state, so it errors or not.正如肖恩兰格评论的那样,插入语句不会返回任何有用的东西,而是对查询结果状态的确认,因此它是否出错。

According to the sql-code, I assume you are querying against a SQL-Server and having no access to a DB with necessary access rights to it you are using the tempdb :-) fair enough.根据 sql-code,我假设您正在查询 SQL-Server 并且无法访问具有必要访问权限的数据库,您正在使用 tempdb :-) 足够公平。

But, the problem in your code is that only the last part starting from the last StrQry, namely the one with your "Insert into..." until "Sheets(1).Range...." has any effect.但是,您的代码中的问题是只有从最后一个 StrQry 开始的最后一部分,即“插入...”直到“Sheets(1).Range....”有任何影响。 The other previous StrQry assignments are simply overwritten by the last one.其他先前的 StrQry 分配只是被最后一个覆盖。

Your problem with these many continuations can be worked around by simply removing them where possible according to the T-SQL-syntax and make long lines as the SQL-Server handles the codelines on its own.您可以通过根据 T-SQL 语法在可能的情况下简单地删除它们来解决这些许多延续的问题,并在 SQL-Server 自行处理代码行时制作长行。

So, the code may look like this所以,代码可能看起来像这样

Sub VBAFromSQL()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String

ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=HIDDEN_Outputs;Data Source=HIDDEN;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"

cnn.Open ConnectionString
cnn.CommandTimeout = 900

StrQuery = "IF OBJECT_ID('tempdb..#temp_MthToDt') IS NOT NULL drop table #temp_MthToDt; IF OBJECT_ID('tempdb..#temp_ImprtSampVol') IS NOT NULL drop table #temp_ImprtSampVol; IF OBJECT_ID('tempdb..#temp_T1') IS NOT NULL drop table #temp_T2AuditData; DECLARE @Last_Day_Loaded VARCHAR(30) = '2018-10-03', @Start_of_Sample VARCHAR(30) = '2018-10-03',@End_of_Sample VARCHAR(30) = '2018-10-03', @Days_Data_so_far INT = 03,@First_Day_Month VARCHAR(30) = '2018-10-01',@Working_Days_in_Month INT = 23,@Days_in_Month INT = 31,@BBE_Sample_Minimum INT = 10,@BBE_Sample_adjustment_multiplier decimal(5,2) = 1.0; CREATE TABLE #temp_ImprtSampVol (CIS VARCHAR(10) NOT NULL,RD VARCHAR(40) NOT NULL,Sample_to_send INT NOT NULL );INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28),('RRA','Care: Quality',29);"

rst.Open StrQuery, cnn
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub

What is not clear to me is what you expected from the insert statement to be delivered and shown in A2 of Sheet1.我不清楚的是您期望从插入语句中交付并显示在 Sheet1 的 A2 中。 If you wanted to show the the inserted lines you have to append either如果你想显示插入的行,你必须附加

;select * from #temp_ImprtSampVol;

or或者

output inserted.*;

at the end of the StrQry.在 StrQry 的末尾。 Mind the missing ";"注意缺少的“;” in front of "output" as it belongs to the insert statement.在“输出”前面,因为它属于插入语句。

Excuse me.打扰一下。 I had to be more precise with this.我必须更精确地解决这个问题。 Before attaching the last mentioned parts you have to remove the ";"在连接最后提到的部件之前,您必须删除“;” at the end of the statement in your StrQry = ... .在 StrQry = ... 中的语句末尾。 So it the looks like this:所以它看起来像这样:

StrQry = "... ;INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28),('RRA','Care: Quality',29) output inserted.*;"

or或者

StrQry = "... ;INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28),('RRA','Care: Quality',29);select * from #temp_ImprtSampVol;"

I'm wondering why you are dropping these other tables that have never been used anyhow and why there are declared all this variables, but I think you want to extend the query.我想知道您为什么要删除这些从未使用过的其他表以及为什么声明了所有这些变量,但我认为您想扩展查询。 Be advised, even this way has it's limitations and you might better ask your DBA for some space with let's say, a small database where you can create SPs on your own.请注意,即使这种方式也有其局限性,您最好向您的 DBA 询问一些空间,例如,一个小型数据库,您可以在其中创建自己的 SP。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM