如何将值从VBA传递到SQL Server 2008中的表变量?

时间:2022-08-24 14:52:07

I have a user form in Excel 2007 that writes data into a table on SQL Server 2008 using a stored procedure.

我在Excel 2007中有一个用户表单,使用存储过程将数据写入SQL Server 2008上的表。

The inserted row then gets an Identity which I report back to VBA. I then use this number to populate several rows of data into another table.

然后插入的行获取一个身份,我向VBA报告。然后我使用这个数字将几行数据填充到另一个表中。

However, instead of having the stored procedure executed first and then writing into another table, I want to have the stored procedure do both things.

但是,我希望让存储过程完成这两件事,而不是先执行存储过程然后再写入另一个表。

Problem is, that the entry for the other table consists is bigger than one row and the parameter that I send from VBA to SQL can only contain one value.

问题是,其他表的条目大于一行,而我从VBA发送到SQL的参数只能包含一个值。

So, I believe I need to create a table variable in SQL. However, how do I pass the values from VBA over to my table variable in SQL? Remember, everything should be done in one stored procedure. This is what I have so far.

所以,我相信我需要在SQL中创建一个表变量。但是,如何将值从VBA传递到SQL中的表变量?请记住,一切都应该在一个存储过程中完成。这就是我到目前为止所拥有的。

Thanks for your help!!!

谢谢你的帮助!!!

--SQL Stored Procedure
ALTER procedure [dbo].[Audit_InsertAuditFinding]

 @AuditRID as int 
,@EnteredBy as varchar(10)  
,@AuditItemNumber as int 
,@AuditFindingShort as varchar(50) 
,@AuditFindingLong as varchar(500)  
,@AuditDocsSaved as bit
,@AuditIncident as int output

as
BEGIN
SET NOCOUNT ON
Insert Into Audit_Findings 
Values (@AuditRID,@EnteredBy,GETDATE(),@AuditItemNumber,@AuditFindingShort,@AuditFindingLong,@AuditDocsSaved)

declare @AuditFindingNumber as int
select @AuditFindingNumber = MAX(@@IDENTITY)

select @AuditIncident = @AuditFindingNumber
END

Here is my VBA code so far.

这是我到目前为止的VBA代码。

--VBA Code
cnn.CursorLocation = adUseClient
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "InternalReporting.dbo.Audit_InsertAuditFinding"
cmd.NamedParameters = True
Set prm = cmd.CreateParameter("@AuditRID", adInteger, adParamInput, 50, Audit_Topic.Value * 1)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@EnteredBy", adVarWChar, adParamInput, 10, Environ("UserName"))
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@AuditItemNumber", adInteger, adParamInput, 10, Audit_ItemNumber.Value * 1)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@AuditFindingShort", adVarWChar, adParamInput, 50, Audit_ShortDescr.Value)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@AuditFindingLong", adVarWChar, adParamInput, 500, Audit_LongDescr.Value)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@AuditDocsSaved", adVarWChar, adParamInput, 500, Doc_Saved)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@AuditIncident", adVarWChar, adParamOutput, 10, Incident_Number.Value)
cmd.Parameters.Append prm

cmd.Execute

Incident_Number.Value = cmd.Parameters("@AuditIncident") * 1
SQL_String = "Delete from InternalReporting.dbo.Audit_Findings_Notifications where Audit_Incident = " & Incident_Number.Value * 1

rst.Open SQL_String, cnn, adOpenStatic
   Set rst = Nothing

   With AUD_02_Item_Mgmt.Notify_Overview
    For I = 1 To .ListCount
        SQL_String = "Insert Into InternalReporting.dbo.Audit_Findings_Notifications " & _
                     "Values (" & Incident_Number.Value & ",'" & .List(I - 1, 0) & _
                     "','" & .List(I - 1, 2) & "')"
        rst.Open SQL_String, cnn, adOpenStatic
        Set rst = Nothing
    Next I
End With`

1 个解决方案

#1


1  

I don't think this is possible with normal / classic ADO as there is no Table Variable Datatype.

我不认为这对于普通/经典ADO是可行的,因为没有表变量数据类型。

An alternative could be to build an XML data string and then parse it in your stored proc.

另一种方法是构建XML数据字符串,然后在存储过程中解析它。

#1


1  

I don't think this is possible with normal / classic ADO as there is no Table Variable Datatype.

我不认为这对于普通/经典ADO是可行的,因为没有表变量数据类型。

An alternative could be to build an XML data string and then parse it in your stored proc.

另一种方法是构建XML数据字符串,然后在存储过程中解析它。