I'm using SQL Server 2008 R2 and I've created a User Defined Type:
create type dbo.UDT as table (FirstName varchar(50) null, LastName varchar(50) null)
I've create a stored procedure that has this UDT as a parameter:
create procedure dbo.InsertIntoMyTableUsingUDT (@udt dbo.UDT readonly)
as
begin
set nocount on;
insert into dbo.MyTable (FirstName, LastName)
select FirstName, LastName from @udt;
return;
end
I would like to use VBA's ADO (ADO.COM/OLE DB) to bulk upload 10,000+ records using this stored procedure.
I've tried passing an ADODB.Recordset as a parameter in an ADODB.Command:
<code that creates ADODB.Recordset 'rst' here...>
Dim com as ADODB.Command
Set com = new ADODB.Command
With com
.ActiveConnection = "Provider=SQLOLEDB.1;Data Source=localhost;Initial Catalog=TestDB;Integrated Security=SSPI;"
.CommandText = "dbo.InsertIntoMyTableUsingUDT"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters(1).Value = rst '<----the ADODB.Recordset created above
.Execute
End With
but I get the runtime error:
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Checking the parameter's type in the Immediate Window I get:
?com.Parameters(1).Type
143
which I can't see in MSFT's list of OLE DB DataTypeEnums:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms675318(v=vs.85).aspx
So I tried passing in a 2-dimensional array instead of the ADODB.Recordset but then I got the error:
Bad variable type.
Both attempts failed and it's got me stumped.
I know it can be done in VB.NET using ADO.NET and the SqlParameterCollection.AddWithValue method.
I would like to know if there is a way of doing it in VBA using ADO.COM. Has anyone done it?
Alternatively, is there a different way of achieving the same thing (bulk upload rather than calling an ADODB.Command 10,000+ times in a loop)?
Cheers