From dfe8e099f9419124b9800eca79b89c8bc75564d9 Mon Sep 17 00:00:00 2001 From: Danny Hollandmoritz Date: Tue, 15 Mar 2022 00:48:02 +0100 Subject: [PATCH 1/2] Send PLP_NULL for VARBINARY(MAX) too to avoid exceptions --- src/pytds/tds_types.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pytds/tds_types.py b/src/pytds/tds_types.py index b9a2a06..9f8d526 100644 --- a/src/pytds/tds_types.py +++ b/src/pytds/tds_types.py @@ -1155,7 +1155,18 @@ def write(self, w, val): if val is None: w.put_uint8(tds_base.PLP_NULL) else: - w.put_uint8(len(val)) + # Putting the actual length here causes an error when bulk inserting: + # + # While reading current row from host, a premature end-of-message + # was encountered--an incoming data stream was interrupted when + # the server expected to see more data. The host program may have + # terminated. Ensure that you are using a supported client + # application programming interface (API). + # + # See https://github.com/tediousjs/tedious/issues/197 + # It is not known why this happens, but Microsoft's bcp tool + # uses PLP_UNKNOWN for nvarchar(max) as well. + w.put_uint8(tds_base.PLP_NULL) if val: w.put_uint(len(val)) w.write(val) From 9cdd781c218d8e141e35df2b572b38f342cc3e91 Mon Sep 17 00:00:00 2001 From: Danny Hollandmoritz Date: Tue, 15 Mar 2022 01:00:35 +0100 Subject: [PATCH 2/2] Fix typo --- src/pytds/tds_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytds/tds_types.py b/src/pytds/tds_types.py index 9f8d526..3c47a9a 100644 --- a/src/pytds/tds_types.py +++ b/src/pytds/tds_types.py @@ -1166,7 +1166,7 @@ def write(self, w, val): # See https://github.com/tediousjs/tedious/issues/197 # It is not known why this happens, but Microsoft's bcp tool # uses PLP_UNKNOWN for nvarchar(max) as well. - w.put_uint8(tds_base.PLP_NULL) + w.put_uint8(tds_base.PLP_UNKNOWN) if val: w.put_uint(len(val)) w.write(val)