Incorrect syntax near Go with Pypyodbc - sql-server

I am using the pypyodbc library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
This is the python script that I am using:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
The workaround for this problem is to delete the Use SL_Site1_App Go line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.

GO is a batch separator used by sqlcmd and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App", and then remove the USE and GO statements in your SQL Statement.

Related

INSERT failure with SQLAlchemy ORM / SQL Server 2017 - Invalid Object Name

Firstly, I have seen many answers which is specific to the Invalid Object Name error working with SQL Server, but None of them seem to solve my problem. I don't have much idea on SQL Server dialect, but here is my current setup required on the project.
SQL Server 2017
SQLAlchemy (pyodbc+mssql)
Python 3.9
I'm trying to insert a database row, using the SQLAlchemy ORM, but it fails to resolve the schema and table, giving me the error of type
(pyodbc.ProgrammingError) ('42S02', "[42S02] [Microsoft][ODBC Driver
17 for SQL Server][SQL Server]Invalid object name 'agent.dbo.tasks'.
(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Statement(s) could not be prepared. (8180)")
I'm creating a session with the following code.
engine = create_engine(connection_string, pool_size=10, max_overflow=5,
pool_use_lifo=False, pool_pre_ping=True, echo=True, echo_pool=True)
db_session_manager = sessionmaker()
db_session_manager.configure(bind=engine)
session = db_session_manager()
I have a task object defined like
class Task(BaseModel):
__tablename__ = "tasks"
__table_args__ = {"schema": "agent.dbo"}
# .. field defs
I'm trying to fill the object fields and then do the insert like the usual
task = Task()
task.tid = 1
...
session.add(task)
session.commit()
But this fails, with the error mentioned before. I tried to execute a direct query like
session.execute("SELECT * FROM agent.dbo.tasks")
And it returned a result set.
The connection string is a URL object, which prints like
mssql+pyodbc://task-user:******#CRD4E0050L/agent?driver=ODBC+Driver+17+for+SQL+Server
I tried to use the SQL Server Management Studio to insert manually and check, there it shown me a sql dialect with [] as field separators
like
INSERT INTO [dbo].[tasks]
([tid]..
, but SQLAlchemy on echo did not show that, instead it used the one I see in MySQL like
INSERT INTO agent.dbo.tasks (onbase_handle_id,..
What is that I'm doing wrong ? I thought SQLAlchemy if configured with a supported dialect, should work fine (I use it on MySQL quite well). Am I missing any configuration ? Any help appreciated.

How to access information schema using a pypika query?

I'm trying to get the names of the columns from a table in an Azure SQL database using a PyPika SQL query, but keep running into trouble. Here's the code I'm using to generate the query:
def dbView(table):
infoSchema = ppk.Table("INFORMATION_SCHEMA.COLUMNS")
return ppk.MSSQLQuery.from_(infoSchema).select(infoSchema.COLUMN_NAME).where(infoSchema.TABLE_NAME == table)
I created another function that uses the PyODBC library to get the SQL from the query, execute it against the database, and return all the rows:
def getData(query: ppk.Query):
'''
Execute a query against the Azure db and return
every row in the results list.
'''
print("QUERY: ", query.get_sql())
conn = getConnection()
with conn.cursor() as cursor:
cursor.execute(query.get_sql())
return cursor.fetchall()
I know the getData() function works because when I pass it a simple select query, everything works correctly. However, when I try to use the query generated by pypika above, I get the following error:
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'INFORMATION_SCHEMA.COLUMNS'. (208) (SQLExecDirectW)")
To make sure this wasn't just some kind of permissions error, I wrote the following query by hand and executed it using the getData() function and it worked just fine:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Validation'
I also printed out the query that pypika generated to the console. The only difference appears to be the addition of some double quotes:
SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA.COLUMNS" WHERE "TABLE_NAME"='Validation'
What am I doing wrong? For some reason, this error appears to be limited to specifically the information schema table, because I have used similar queries several other times in my code without issue. I know I can just use the query I wrote by hand, but the point of using PyPika was to make all my SQL queries more readable and reusable - it'd be nice to understand why it doesn't work in this very specific situation.
Thanks!
It apparently has an API to schema-qualify tables.
from pypika import Table, Query, Schema
views = Schema('views')
q = Query.from_(views.customers).select(customers.id, customers.phone)
https://pypika.readthedocs.io/en/latest/2_tutorial.html#tables-columns-schemas-and-databases

SQL to SAS ODBC connection - truncation of NVARCHAR(max) but getting ERROR: The option dbtype is not a valid output data set option

I have a table stored in SQL Server Management Studio version 15.0.18369.0 and I have a working and established ODBC connection to SAS language program World Programming Software version 3.4.
Previous import/reading of this data has been successful but the operator of the SQL server may have recently converted their data type to NVARCHAR(max). It may also be due to a driver change (I got a new laptop and reinstalled what I thought was the exact same OBDC for SQl driver as I had before but who knows) I have 64-bit ODBC Driver 17 for SQL server.
The VARCHAR(max) type in SQL causes the data to only be 1 character long in every column.
I have tried to fix it by:
Adding the DB max text option to libname
libname odbclib odbc dsn="xxx" user="xxx" password="xxx" DBMAX_TEXT=8000;
This did nothing so I also tried to add DB type option:
data mydata (dbtype=(mycol='char(25)')) ;
set odbclib.'sql data'n
run;
And I get ERROR:
The option dbtype is not a valid output data set option.
I have also tried DBSASTYPE, and putting both options in the set statement and this yields the same error.
I also tried with proc SQL:
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract(compress=no dbsastype=(mycol='CHAR(20)')) as select * from
connection to odbc ( select * from dbo.'sql data'n
); disconnect from odbc; quit;
And I get
NOTE: Connected to DB: BB64 (Microsoft SQL Server version 12.00.2148)
NOTE: Successfully connected to database ODBC as alias ODBC. 3915
create table extract(compress=no
dbsastype=(mycol='CHAR(20)')) as 3916 select *
from connection to odbc 3917 ( 3918 select *
from dbo.'sql data'n ERROR: The option dbsastype is not a valid
output data set option 3919 3920 ); 3921
disconnect from odbc; NOTE: ERRORSTOP was specified. The statement
will not be executed due to an earlier error NOTE: Statements not
executed because of errors detected 3922 quit;
One thing to try would be putting the DBTYPE or DBSASTYPE in the right place.
data mydata;
set odbclib.'sql data'n(dbtype=(mycol='char(25)'));
run;
It is an option that would go on the "set" not the "data" line.
I would also encourage you to contact the developers, as it seems like this is a bug in their implementation; or perhaps try a different ODBC SQL driver.
Maybe a workaround in the meanwhile is to use CONVERT in the passthrough?
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract as select * from
connection to odbc ( select a,b,c,convert(NCHAR(20),mycol) from dbo.'sql data'n
); disconnect from odbc; quit;

Error writing R Dataframe in SQL Server table

I am writing a dataframe in R to Microsoft SQL server table and seeing below error. I have required permissions to insert records to this table, dbo.temp2 but it is failing with this error. The number of columns in Dataframe and Table are matching.
dbWriteTable2(connDBI,"dbo.temp2",test.df1,append=TRUE,overwrite=FALSE,row.names=FALSE,add.id=TRUE)
Error: <SQL> 'SELECT id FROM dbo.temp2 ORDER BY id DESC LIMIT 1'
nanodbc/nanodbc.cpp:1587: 42000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'
Also tried with function dbWriteTable from DBI package and it is failing with Create table error even though am using append=TRUE option.
I could able to resolve this issue with some online research and use of RMySQL package.
Below is the line of R code which fixed this and now am able to write the Dataframe into SQL Database table.
RMySQL::dbWriteTable(connDBI,name = "temp2"
,value=test.df1,append=TRUE,row.names=FALSE,overwrite=FALSE)

Why calling stored procedure in SQL server from x++ break AX?

I have a script for renaming column in database written in X++.
public static server void renameColumn()
{
str sql;
Connection oConnection;
Statement oStatement;
ResultSet resSet;
SqlStatementExecutePermission perm;
sql = strFmt( 'use DBNAME; exec sp_rename \'DBNAME.dbo.Table1.Name2\', \'Name1\', \'COLUMN\'');
oConnection = new Connection();
oStatement = oConnection.createStatement();
perm = new SqlStatementExecutePermission(sql);
perm.assert();
oStatement.executeUpdate(sql);
}
Which change existing column named Name2 to Name1.
The strange errors start to occures few minutes later after I call it (using job).
Cannot select a record in Current AOS instances (SysServerSessions). ServerId: 0.
The SQL database has issued an error.
SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'SYSSERVERSESSIONS'.
SQL statement: SELECT T1.SERVERID,T1.AOSID,T1.INSTANCE_NAME,T1.VERSION,T1.LOGINDATETIME,T1.LOGINDATETIMETZID,T1.STATUS,T1.LOADBALANCE,T1.WORKLOAD,T1.LASTUPDATEDATETIME,T1.LASTUPDATEDATETIMETZID,T1.AOSACCOUNT,T1.RECVERSION,T1.RECID,T2.SESSIONID,T2.SERVERID,T2.VERSION,T2.LOGINDATETIME,T2.LOGINDATETIMETZID,T2.STATUS,T2.USERID,T2.SID,T2.USERLANGUAGE,T2.HELPLANGUAGE,T2.CLIENTTYPE,T2.SESSIONTYPE,T2.CLIENTCOMPUTER,T2.DATAPARTITION,T2.RECVERSION,T2.RECID FROM SYSSERVERSESSIONS T1 CROSS JOIN SYSCLIENTSESSIONS T2 WHERE ((T2.SERVERID=T1.SERVERID) AND (T2.SESSIONID=?))
Cannot select a record in Alerts - event inbox (EventInbox).
The SQL database has issued an error.
SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'EVENTINBOX'.
SQL statement: SELECT COUNT(T1.RECID) FROM EVENTINBOX T1 WHERE ((PARTITION=?) AND ((((ISREAD=?) AND (USERID=?)) AND (DELETED=?)) AND (VISIBLE=?)))
Anyone have an idea how to repair it and what exactly cause it?
Microsoft Dynamics AX 2012 R3
Microsoft SQL Server 2012
P.S.: It can not be used without use DBNAME; because
SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
I find a way that do not take AOS with it when crash. I am calling stored procedure without using
use DBNAME;
Instead of that I am using full name of stored procedure by
[DBNAME].[dbo].[sp_rename]
And when this crashed I get a normal SQL error:
Cannot execute the required database operation.
The SQL database has issued an error.
SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]No item by the name of 'DBNAME.dbo.Table1.Name1' could be found in the current database 'DBNAME', given that #itemtype was input as '(null)'.
SQL statement: exec [VABO].[dbo].[sp_rename] 'DBNAME.dbo.Table1.Name1', 'Name2'
Axapta is using Microsoft SQL Server also so the
use DBNAME;
must break some inner settings.

Resources