我编写了以下过程来使用 openxml 进行批量插入。在我的情况下,表名和列名是动态的,因为它们将在下面插入之前从 .Net 应用程序创建。存储过程运行成功,但未向表中插入任何数据。
请帮我
预先感谢。
CREATE PROCEDURE [dbo].[BulkUpdate]
@XmlDoc text,
@TableName nvarchar(50),
@ColumnNames nvarchar(4000),
@ColumnDefinition nvarchar(4000)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @Idoc int;
Declare @Sql nvarchar(4000);
BEGIN TRANSACTION
Begin Try
Exec sp_xml_preparedocument @Idoc output, @XmlDoc;
Set @Sql = 'Insert into '+@TableName+'('+@ColumnNames+')
Select '+@ColumnNames+' From OpenXML(@Idoc,''/NewDataSet/Data'', 2)
With('+@ColumnDefinition+')';
Print @sql;
Exec @Sql;
Exec sp_xml_removedocument @Idoc;
Commit Transaction
End Try
Begin Catch
RollBack Transaction
End Catch
END