question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function sequence error (0) while inserting NULL value into NULLable VARCHAR/NVARCHAR(MAX) Column

See original GitHub issue

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:

  • Python: 3.5.4 64-bit
  • pyodbc: 4.0.24
  • OS: Windows 10
  • DB: SQL Server 2017
  • driver: ODBC Driver 17 for SQL Server

Issue

Expected behavior: Insert NULL into nullable varchar/nvarchar(max) column with fast_executemany=True. What happens: pyodbc.Error: Function sequence error (0) (SqlParamData)

Reproduction:

CREATE TABLE TestTable
(
	LongerContent NVARCHAR(MAX) NULL,
)

Works:

sql = """
INSERT INTO TestTable (LongerContent) VALUES (?)
"""

params = (
    ('ajhdiwopwqlwdlsdlpwdlowde',), (None,),
)


con = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=server;DATABASE=db;Trusted_Connection=yes;')
cursor = con.cursor()
cursor.fast_executemany=False
cursor.executemany(sql, params)
con.commit()

Results in Function sequence error (0):

import pyodbc

sql = """
INSERT INTO TestTable (LongerContent) VALUES (?)
"""

params = (
    ('ajhdiwopwqlwdlsdlpwdlowde',), (None,),
)


con = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=server;DATABASE=db;Trusted_Connection=yes;')
cursor = con.cursor()
cursor.fast_executemany=True
cursor.executemany(sql, params)
con.commit()

so it seems to be a problem with the fast_executemany option.

ODBC Trace: SQL.LOG

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
v-makouzcommented, Oct 2, 2018

@gizmo93 I’ve made a pull request for a fix for this error. It seems that in python 3 the parameters are not bytes, but Unicode objects and to allow them to be used by SQLPutData I need to convert them into a bytes object.

Tested on Windows 10 and Ubuntu 17.04, I also ran sqlservertests.py, mysqltests.py and pgtests.py using Python 2 and 3 and didn’t see any new errors.

https://github.com/mkleehammer/pyodbc/pull/467

@mkleehammer Let me know if you need me to make any further changes

2reactions
v-chojascommented, Sep 20, 2018

@gordthompson Yes we are aware of the cause, and will be working on a solution to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inserting NULL in an nvarchar fails in MSAccess
I have a table on SQL Server 2008, say StockEvent that contains a Description field defined as nVarchar(MAX) . The field is set...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
node-mssql | Microsoft SQL Server client for Node.js
DB_NAME, server: 'localhost', pool: { max: 10, min: 0, idleTimeoutMillis: 30000 } ... e.g. Column names cannot be passed/set in statements using variables....
Read more >
SQL VARCHAR Data Type Do's and Don'ts for Faster Databases
Further, I use CHAR columns only if the columns will be non-nullable. Why? Because the size in bytes of CHAR columns when NULL...
Read more >
NULL semantics | Databricks on AWS
In order to compare the NULL values for equality, Databricks provides a null-safe equal operator ( <=> ), which returns False when one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found