0

I am executing below code using ctrlM job then I am experiencing

pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near ';'.
(102) (SQLExecDirectW)")

error whereas the same code when run in vscode is working fine.

logger = Logger.get_logger(processCode.LOG_PURGE_PROCESS.value, __name__)

d = "dateadd(mm, -1, getdate())" 
params = UtilityLib.open_param_file(configFiles.PROJECT.value, logger)
path = params['LogFile']
open_connection_error_constant = 'Error occurred when opening connection to database : '

with open(path) as csv_file:
    r = csv.DictReader(csv_file)
    for row in r:
        connection_params = row['database']

        if connection_params is not None:
            deletesql = "delete from " + str(row["tablename"]) + " where log_chg_dtm < = " + d + " ;"   
            print(deletesql)
            cnxn = DBManagement.set_db_connection(connection_params, logger)
            try: 
                result = PostgresqlQueryAssist.sql_delete(cnxn, deletesql, logger)
                print('deleted records from table:', row["tablename"])
            except pyodbc.Error as ex:
                    logger.exception(open_connection_error_constant + str(ex))
                    raise
            finally:
                cnxn.close()

My config file name is passed in csv file, after reading the file, connections are like :

 "DBMSType": "SYBASE",
    "serverName": "test",
    "databaseName": "treat",
    "port": "1000",
    "userID": "user",
    "password": "pswd"
Thom A
  • 88,727
  • 11
  • 45
  • 75
DIVYA
  • 15
  • 6
  • 1
    I would switch to parameterisation first, as you are *injecting* at the moment. Also you've tagged SQL Server here, however, your Connection details states Sybase. What are you actually using? Sybase or SQL Server? You also have the helper function `PostgresqlQueryAssist`; PostgreSQL is *another* RDBMS. – Thom A Dec 19 '22 at 17:10
  • Hi @Larnu, I am connecting to sybase but the error has ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near ';'.\n (102) (SQLExecDirectW)") so I tagged sql-server. Though I am using PostgresqlQueryAssist, I just defined with that name but calling cursor. – DIVYA Dec 19 '22 at 17:28
  • So by "ctrlM" do you mean [this](https://www.bmc.com/it-solutions/control-m.html)? If so, and if the *exact same* code runs in Visual Studio Code but fails in Control-M then you may need to ask BMC for assistance. (Although it might be worth a try to remove the `;` from the end of your SQL statement to see if that makes any difference.) – Gord Thompson Dec 19 '22 at 22:48
  • remove the closing ";" in the SQL statement in the line: deletesql = "delete .... – access_granted Dec 22 '22 at 19:24

1 Answers1

0

After removing ; at the end of the query resolved the issue.

DIVYA
  • 15
  • 6