0
我想在2008 sql服務器上運行存儲過程。我創建了一行proc,dbo.sp_test_proc。 我可以通過EXEC在服務器上運行proc,並返回。 但是,當我嘗試通過ADO在excel/vba中運行時,出現錯誤消息'找不到存儲過程sp_test_proc。 我可以通過ADO運行sys模式存儲過程,但不是任何dbo。我的角色是db_owner,並且vba代碼正在與在服務器管理工作室中創建存儲過程的相同windows用戶下運行。 任何人有任何想法?我很難過。找不到存儲過程
Sub storedProcedure()
'This sub-routine defines an ADOBD command to return variables to VBA from SQL Stored Procedures.
'The ADODB command executes a Stored Procedure on the SQL Server (cmd.CommandText = [Stored Procedure Name])
'Input requirements from the Stored procedure are declared as variants at the start of the sub-routine.
Dim cnt As ADODB.connection
Dim rst As ADODB.recordset
Dim cmd As ADODB.Command
Dim stCon As String 'SQL Connection string
Dim stProcName As String 'Stored Procedure name
'Declare variables for Stored Procedure
Dim myVariable As Variant
Dim myReturn As String
'Set ADODB requirements
Set cnt = New ADODB.connection
Set rst = New ADODB.recordset
Set cmd = New ADODB.Command
'Define database connection string
stCon = "Provider=SQLOLEDB.1;"
stCon = stCon + "Data Source=myserver;"
stCon = stCon + "Initial Catalogue=Mmydb;"
stCon = stCon + "Integrated Security=SSPI;"
stCon = stCon + "Persist Security Info=True;"
'Open database connection
cnt.ConnectionString = stCon
cnt.Open
' Defines the stored procedure commands
stProcName = "dbo.my_sp" 'Define name of Stored Procedure to execute."
'stProcName = "sys.sp_stored_procedures" 'Define name of Stored Procedure to execute.
cmd.CommandType = adCmdStoredProc 'Define the ADODB command
cmd.ActiveConnection = cnt 'Set the command connection string
cmd.CommandText = stProcName 'Define Stored Procedure to run
'Execute stored procedure and return to a recordset
Set rst = cmd.Execute()
'myReturn = rst.Fields("procedure_name").Value
myReturn = rst.Fields("mycolumn").Value
'Call Sub-Routine_That_Uses_The_Returned_Data
MsgBox (myReturn)
'Close database connection and clean up
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
字謹慎:你應該不** **使用存儲過程名稱的'sp_'前綴;微軟保留自己使用的前綴;將其更改爲其他內容 – 2011-05-16 16:11:25
同意,只是想盡一切辦法來獲取存儲過程。嘗試了不同的名字。 – destructurer 2011-05-16 16:25:53
你確定「初始目錄= Mmydb」是正確的數據庫嗎? – RThomas 2011-05-16 18:07:40