-
I am trying to create a view where the view definition is parameterized, such as:
This works fine with PostgreSQL and psycopg2 and also with Microsoft SQL Server and pymssql, but using pyodbc, it throws a ProgrammingError:
Unfortunately (don't take it personally ;)), I need pyodbc because pymssql doesn't support integrated authentication (Trusted_Connection=yes), as far as I can see. This answer says that "Databases do not allow you to prepare DDL", which I wonder how true that is in general, but I assume it is at least true for MSSQL and is the cause of my issue. (If so, I'm guessing that pymssql must be interpolating the parameters before sending the command to SQL Server.) One obvious solution is to do the interpolation myself, escaping single-quotes. I can't help but wonder if I'm missing a better solution though - surely I'm not the only one with this need. Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
That's not valid syntax for CREATE VIEW. See here for more information: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql Also, this has nothing to do with pyODBC. |
Beta Was this translation helpful? Give feedback.
-
Ah, sorry, my bad, of course the command was incomplete. I'll edit the report. |
Beta Was this translation helpful? Give feedback.
-
That's just DBMS/driver-specific variation ,which is normal for something as generic as ODBC. https://learn.microsoft.com/en-us/sql/odbc/reference/develop-app/statement-parameters?view=sql-server-ver16 |
Beta Was this translation helpful? Give feedback.
-
Yes, that is true. For crsr.execute("CREATE TABLE table_name (lastname varchar(%s))", (50, )) pymssql sends
whereas with the corresponding
and SQL Server rejects it with
|
Beta Was this translation helpful? Give feedback.
That's just DBMS/driver-specific variation ,which is normal for something as generic as ODBC.
https://learn.microsoft.com/en-us/sql/odbc/reference/develop-app/statement-parameters?view=sql-server-ver16
"Generally, parameters are valid only in Data Manipulation Language (DML) statements, and not in Data Definition Language (DDL) statements."