我尝试在Excel中创建用户表单,允许用户向现有SQL服务器表添加新列和说明。
我在VBA中使用ADO库。要向创建的列添加描述,我需要运行系统存储过程sp_addextendedproperty。
我无法将参数传递给它,我总是收到错误 错误-2147217900:为过程' sp_addextendedproperty'指定了无效的参数或选项。
Private Sub InsertField_bt_Click()
Set con = New ADODB.Connection
con.Provider = "sqloledb"
sConnectionString = "Server=rumossql12;Database=PharmNet;UID=(login here);Pwd=(password here)"
con.Open sConnectionString
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = con
cmd.CommandText = "sp_addextendedproperty"
Dim name As ADODB.Parameter
Dim value As ADODB.Parameter
Dim level0type As ADODB.Parameter
Dim level0name As ADODB.Parameter
Dim level1type As ADODB.Parameter
Dim level1name As ADODB.Parameter
Dim level2type As ADODB.Parameter
Dim level2name As ADODB.Parameter
Set name = cmd.CreateParameter("@name", adVarWChar, adParamInput, 128, "N'MS_Description'")
cmd.Parameters.Append name
Set value = cmd.CreateParameter("@value", adVariant, adParamInput, , "N'" & InsertField.TextBox2.value & "'")
cmd.Parameters.Append value
Set level0type = cmd.CreateParameter("@level0type", adVarChar, adParamInput, 128, "N'SCHEMA'")
cmd.Parameters.Append level0type
Set level0name = cmd.CreateParameter("@level0name", adVarWChar, adParamInput, 128, "N'dbo'")
cmd.Parameters.Append level0name
Set level1type = cmd.CreateParameter("@level1type", adVarChar, adParamInput, 128, "N'TABLE'")
cmd.Parameters.Append level1type
Set level1name = cmd.CreateParameter("@level1name", adVarWChar, adParamInput, 128, "N'tbConsolidator'")
cmd.Parameters.Append level1name
Set level2type = cmd.CreateParameter("@level2type", adVarChar, adParamInput, 128, "N'COLUMN'")
cmd.Parameters.Append level2type
Set level2name = cmd.CreateParameter("@level2name", adVarWChar, adParamInput, 128, "N'" & InsertField.TextBox1.value & "'")
cmd.Parameters.Append level2name
On Error GoTo ErrHandler
sSQL = "ALTER TABLE PharmNet.dbo.tbConsolidator ADD " & InsertField.TextBox1.value & " NVARCHAR(255) NULL"
'con.Execute sSQL
cmd.Execute
InsertField.Hide
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "ErrorThingy"
Sheet2.Cells(1, 1).value = "Error " & Err.Number & ": " & Err.Description
con.Close
Set con = Nothing
End Sub
为什么会发生? 预先感谢您的帮助! :)
答案 0 :(得分:0)
当我在MSDN上查看sp_addextendedproperty()
的示例代码时,我看到输入的levelxtype
参数仅以首字母大写:
USE AdventureWorks2012;
GO
EXEC sp_addextendedproperty
@name = N'Caption',
@value = 'Postal code is a required column.',
@level0type = N'Schema', @level0name = 'Person',
@level1type = N'Table', @level1name = 'Address',
@level2type = N'Column', @level2name = 'PostalCode';
GO
示例here
也许你可以尝试一下?
修改
当我将代码放在示例旁边时,在我看来,你正在颠倒名称和类型。试试这个:
Set name = cmd.CreateParameter("@name", adVarWChar, adParamInput, 128, "N'MS_Description'")
cmd.Parameters.Append name
Set value = cmd.CreateParameter("@value", adVariant, adParamInput, , "N'" & InsertField.TextBox2.value & "'")
cmd.Parameters.Append value
Set level0type = cmd.CreateParameter("@level0type", adVarWChar, adParamInput, 128, "N'Schema'")
cmd.Parameters.Append level0type
Set level0name = cmd.CreateParameter("@level0name", adVarChar, adParamInput, 128, "'dbo'")
cmd.Parameters.Append level0name
Set level1type = cmd.CreateParameter("@level1type", adVarWChar, adParamInput, 128, "N'Table'")
cmd.Parameters.Append level1type
Set level1name = cmd.CreateParameter("@level1name", adVarChar, adParamInput, 128, "'tbConsolidator'")
cmd.Parameters.Append level1name
Set level2type = cmd.CreateParameter("@level2type", adVarWChar, adParamInput, 128, "N'Column'")
cmd.Parameters.Append level2type
Set level2name = cmd.CreateParameter("@level2name", adVarChar, adParamInput, 128, "'" & InsertField.TextBox1.value & "'")
cmd.Parameters.Append level2name