gpt4 book ai didi

vba - 在 Access 中使用 SQL Server 存储过程的输出参数

转载 作者:行者123 更新时间:2023-12-03 23:23:18 26 4
gpt4 key购买 nike

在从 Access 2013 VBA 执行过程后,我试图从 SQL Server 2008 中的存储过程获取输出变量(新标识列)。我并不完全理解所有的 adodb 内容,我尝试了不同文章中的许多内容,但无济于事。这就是我现在所处的位置......

存储过程:

PROCEDURE [proc_NewClient]
@Type INT,
@PhoneIntakeID INT,
@ClientID INT = NULL,
@NewPSID INT = null OUTPUT (2 possible ways to call the proc, one does not produce an output variable)

INSERT INTO tblClientDetails (ClientID, PhoneIntakeID, Client_Status)
VALUES (@ClientID, @PhoneIntakeID, 'Applicant')

DECLARE @NewClientDetailID int
SELECT @NewClientDetailID = SCOPE_IDENTITY()

INSERT INTO tblPS_Psychosocial (ClientDetailID, Client)
VALUES (@NewClientDetailID, @ClientID)

SELECT @NewPSID = SCOPE_IDENTITY()

INSERT INTO tblPS_AbuseHistory (PsychosocialID)
VALUES (@NewPSID)

INSERT INTO tblPS_FamilyHistory(PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_FinancialHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_LegalHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_MedicalHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_PsychiatricHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_SocialHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_SpiritualHistory (PsychosocialID) VALUES (@NewPSID)
INSERT INTO tblPS_SubstanceHistory (PsychosocialID) VALUES (@NewPSID)

INSERT INTO tblVocAssessment(ClientDetailID) VALUES (@NewClientDetailID)

从我得到的表格来看:
    Dim cnn As ADODB.Connection
Dim cmd As New ADODB.Command, rs As New ADODB.Recordset, param1 As New ADODB.Parameter, param2 As New ADODB.Parameter, param3 As New ADODB.Parameter, param4 As New ADODB.Parameter
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DRIVER=SQL Server;SERVER=SRV-DB01;DATABASE=TWHClientMgmt;Trusted_Connection=Yes"

cnn.Open cnn.ConnectionString

Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "[THEWOMENSHOME\jboyd].proc_NewClient"


Set param1 = cmd.CreateParameter("@Type", adinteger, adparamInput, , 2)
cmd.Parameters.Append param1
Set param2 = cmd.CreateParameter("@PhoneIntakeID", adinteger, adparamInput, , Me.PhoneIntakeID)
cmd.Parameters.Append param2
Set param3 = cmd.CreateParameter("@ClientID", adinteger, adparamInput, , intNewID)
cmd.Parameters.Append param3
Set param4 = cmd.CreateParameter("@NewPSID", adinteger, adParamOutput)
cmd.Parameters.Append param4

rs.open cmd

到目前为止,代码有效,新记录生成等。当我从 SQL Server 运行存储过程时,它返回正确的标识列号,但我尝试了多种方法来引用 VBA 中的输出,但最终总是会出现一个空值。我如何在这里正确引用它?

最佳答案

要检索存储过程的 OUTPUT 参数的值,您只需 .Execute ADODB.Command 然后检索 .Value对应的 ADODB.Parameter,像这样:

cmd.Execute
' retrieve and display the returned OUTPUT parameter value
Debug.Print cmd.Parameters("@NewPSID").Value

关于vba - 在 Access 中使用 SQL Server 存储过程的输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com