Skip to content

Commit 1f11602

Browse files
committed
Fix: support xml type for bulkcopy
1 parent da9f18c commit 1f11602

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

bulkcopy.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ func (b *Bulk) sendBulkCommand(ctx context.Context) (err error) {
8282
}
8383
}
8484
if bulkCol != nil {
85+
// Note that for INSERT BULK operations, XMLTYPE is to be sent as NVARCHAR(N) or NVARCHAR(MAX) data type.
86+
// An error is produced if XMLTYPE is specified.
87+
//
88+
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/ab4a7d62-cd1f-4db1-b67d-ecae58f493e3
89+
if bulkCol.ti.TypeId == typeXml {
90+
bulkCol.ti.TypeId = typeNVarChar
91+
}
8592

8693
if bulkCol.ti.TypeId == typeUdt {
8794
//send udt as binary
@@ -617,7 +624,6 @@ func (b *Bulk) makeParam(val DataValue, col columnStruct) (res param, err error)
617624
err = fmt.Errorf("mssql: invalid type for Guid column: %T %s", val, val)
618625
return
619626
}
620-
621627
default:
622628
err = fmt.Errorf("mssql: type %x not implemented", col.ti.TypeId)
623629
}

bulkcopy_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ func testBulkcopy(t *testing.T, guidConversion bool) {
202202
{"test_intnvarchar", 1234, "1234"},
203203
{"test_image", []byte("1"), nil},
204204
{"test_imagen", nil, nil},
205+
{"test_xml", "<root><child>value</child></root>", nil},
206+
{"test_xmln", nil, nil},
205207
}
206208

207209
columns := make([]string, len(testValues))
@@ -417,7 +419,8 @@ func setupTable(ctx context.Context, t *testing.T, conn *sql.Conn, tableName str
417419
[test_intf32] [int] NULL,
418420
[test_geom] [geometry] NULL,
419421
[test_geog] [geography] NULL,
420-
[text_xml] [xml] NULL,
422+
[test_xml] [xml] NOT NULL,
423+
[test_xmln] [xml] NULL,
421424
[test_uniqueidentifier] [uniqueidentifier] NULL,
422425
[test_nulluniqueidentifier] [uniqueidentifier] NULL,
423426
[test_decimal_18_0] [decimal](18, 0) NULL,

0 commit comments

Comments
 (0)