Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSSQL] Identity insert not working #93

Open
dirkjonker opened this issue May 19, 2017 · 1 comment
Open

[MSSQL] Identity insert not working #93

dirkjonker opened this issue May 19, 2017 · 1 comment
Labels

Comments

@dirkjonker
Copy link
Contributor

To be able to insert a value in an identity column (like an auto-increment column) you need to explicitly set IDENTITY_INSERT on that specific table to ON. Since turbodbc closes the statement handle after execute, this setting is not remembered, and a second execute that tries to insert will fail.

Related to #90

cur.execute("SET IDENTITY_INSERT ##test_some ON")
Out[38]: <turbodbc.cursor.Cursor at 0x111a0efd0>

In [39]: cur.execute("INSERT INTO ##test_some (val) VALUES (?)", (1,))
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
/Users/Dirkjonker/.pyenv/versions/3.5.2/envs/turbo/lib/python3.5/site-packages/turbodbc/exceptions.py in wrapper(*args, **kwds)
     34         try:
---> 35             return f(*args, **kwds)
     36         except InternError as e:

/Users/Dirkjonker/.pyenv/versions/3.5.2/envs/turbo/lib/python3.5/site-packages/turbodbc/cursor.py in execute(self, sql, parameters)
     71             buffer.add_set(parameters)
---> 72             buffer.flush()
     73         self.impl.execute()

Error: ODBC error
state: 23000
native error code: 544
message: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot insert explicit value for identity column in table '##test_some' when IDENTITY_INSERT is set to OFF.

During handling of the above exception, another exception occurred:

DatabaseError                             Traceback (most recent call last)
<ipython-input-39-ca28f00caca6> in <module>()
----> 1 cur.execute("INSERT INTO ##test_some (val) VALUES (?)", (1,))

/Users/Dirkjonker/.pyenv/versions/3.5.2/envs/turbo/lib/python3.5/site-packages/turbodbc/exceptions.py in wrapper(*args, **kwds)
     35             return f(*args, **kwds)
     36         except InternError as e:
---> 37             raise DatabaseError(str(e))
     38     return wrapper

DatabaseError: ODBC error
state: 23000
native error code: 544
message: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot insert explicit value for identity column in table '##test_some' when IDENTITY_INSERT is set to OFF.
@anewmanvs

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants