DatabaseError: ORA-01036: illegal variable name / number

I need to substitute the following values ​​in select query. but i got an error as i mentioned below

self.jobNo = J-12060 qcActivity = C173 self.wrkArea = 1666339 cursor.execute("""SELECT A.MARKERID, D.COMMENTS,A.STATUS,A.X1,A.Y1,A.X2,A.Y2,C.ERRGROUP,C.ERRDESC,c.category FROM MDP_ERR_MASTER A,(SELECT MARKERID, MAX(RECNO) maxRECNO FROM MDP_ERR_MASTER where project_code = ':jobno' and errorcode like ':jobno_:qcActivity%' AND WORKAREA LIKE ':workarea%' GROUP BY MARKERID) B,MDP_ERR_CONFIG C,(SELECT MARKERID, COMMENTS FROM MDP_ERR_MASTER WHERE PROJECT_CODE = ':jobno' AND RECNO = 1 AND errorcode like ':jobno_:qcActivity%' AND WORKAREA LIKE ':workarea%') D WHERE(A.MARKERID = B.MARKERID And A.RECNO = B.maxRECNO And A.Markerid = D.MARKERID)AND A.PROJECT_CODE = ':jobno' AND A.ERRORCODE LIKE ':jobno_:qcActivity%' AND A.WORKAREA LIKE ':workarea%' AND A.ERRORCODE = C.ERRCODE""", {"jobno" : str(self.jobNo), "qcActivity" : str(qcActivity), "workarea" : str(self.wrkArea) }) Traceback (most recent call last): File "D:\work\venkat\QGIS\Tools\GlobalErrorMarker\globalerrormarker.py", line 272, in btnDownloadError_Clicked "workarea" : str(self.wrkArea) DatabaseError: ORA-01036: illegal variable name/number 
+4
source share
1 answer

I think you misunderstood how binding variables work with Oracle and cx_Oracle.

Oracle recognizes an expression such as :myvar in the SQL query as a placeholder for the binding variable. When he encounters this, he will notice that he needs a value for this variable before he can run the query, but he can still continue parsing the query without this value.

Binding variable variables does not work inside string literals. A condition such as project_code = ':jobno' will only match lines whose project_code is the actual six-character string :jobno , regardless of whether you have the bind parameter named jobno . Instead, you should write project_code = :jobno . Don't worry about telling Oracle what type of jobno value should contain; he will verify that you have the correct type when you really give it a value.

There are several places where you are trying to build LIKE clauses by combining variable binding labels. This concatenation can still be done, but it must be done in SQL using the || . So, instead of writing ':workarea%' write :workarea || '%' :workarea || '%' and running the entry ':jobno_:qcActivity%' , write :jobno || '_' || :qcActivity || '%' :jobno || '_' || :qcActivity || '%' :jobno || '_' || :qcActivity || '%' .

I made these changes to your SQL query, created a couple of tables with enough columns to make the query valid, and ran it. I did not have data to run it, so I did not get the results, but the database at least analyzed and successfully executed the request. I also formatted the request to make it easier to read:

  cursor.execute(""" SELECT A.MARKERID, D.COMMENTS, A.STATUS, A.X1, A.Y1, A.X2, A.Y2, C.ERRGROUP, C.ERRDESC, c.category FROM MDP_ERR_MASTER A, (SELECT MARKERID, MAX(RECNO) maxRECNO FROM MDP_ERR_MASTER WHERE project_code = :jobno AND errorcode like :jobno || '_' || :qcActivity || '%' AND WORKAREA LIKE :workarea || '%' GROUP BY MARKERID) B, MDP_ERR_CONFIG C, (SELECT MARKERID, COMMENTS FROM MDP_ERR_MASTER WHERE PROJECT_CODE = :jobno AND RECNO = 1 AND errorcode like :jobno || '_' || :qcActivity || '%' AND WORKAREA LIKE :workarea || '%') D WHERE A.MARKERID = B.MARKERID AND A.RECNO = B.maxRECNO AND A.Markerid = D.MARKERID AND A.PROJECT_CODE = :jobno AND A.ERRORCODE LIKE :jobno || '_' || :qcActivity || '%' AND A.WORKAREA LIKE :workarea || '%' AND A.ERRORCODE = C.ERRCODE""", {"jobno" : str(self.jobNo), "qcActivity" : str(qcActivity), "workarea" : str(self.wrkArea) }) 
+11
source

Source: https://habr.com/ru/post/1479297/


All Articles