I have the following issue when using the Create Database View tool in ArcMap.
My query runs fine in Microsoft SSMS and Visual Code (with mssql extension) against the enterprise geodatabase.
But for some reason, when I'm trying to create the view using Create Database View in ArcMap, I get the following error.
Traceback (most recent call last): File "D:\Beni\OT\Databases\Actualizare\Tools\Scripts\10_Creare_view.py", line 39, in <module> arcpy.CreateDatabaseView_management(in_db,numeView,defView) File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 4318, in CreateDatabaseView raise e ExecuteError: ERROR 999999: Error executing function. [[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '('.] DBMS table not found Failed to execute (CreateDatabaseView). ERROR 999999: Error executing function. [[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '('.] DBMS table not found Failed to execute (CreateDatabaseView).
I can successfully connect to the Enterprise Geodatabase in ArcMap and create other views using the same tool, on the same geodatabase.
I am able to create the view via Catalog window in ArcMap, using right-click on geodatabase connection - New - View. It works flawlessly this way and it doesn't complain about syntax.
I could create the views manually, via the above-mentioned method, but I am trying to automate the operation via the Create Database View tool. This is not the only query that fails with this tool (I have about 400 of them which work and about 70 which fail).
I checked the syntax a thousand times and I still can't figure it out. Maybe someone here could help me understand the issue. My only conclusion is that this could be a bug in the Create Database View tool, with certain types of queries.
The SQL query looks like this:
SELECT CAST (ROW_NUMBER() OVER (ORDER BY ADM.DENUMIRE) AS INT) AS OBJECTID, ADM.DENUMIRE, CAST(ADM.LOC_COMPONENTE_ALE_MUN_ORAS AS DECIMAL) / CAST(iif(AGR.ANUL_2014 = 0, -1, AGR.ANUL_2014) AS DECIMAL) AS DENSITATEA_ASEZARILOR_URBANE, ADM.LOC_COMPONENTE_ALE_MUN_ORAS, AGR.ANUL_2014 AS SUPRAFATA, ADM.SHAPE FROM (SELECT JUD.OBJECTID, JUD.DEN_JUD AS DENUMIRE, ADM.LOC_COMPONENTE_ALE_MUN_ORAS, JUD.SHAPE FROM (SELECT JUDET, SUM(LOC_COMPONENTE_ALE_MUN_ORAS) AS LOC_COMPONENTE_ALE_MUN_ORAS FROM gisdata_owner.ADM010 AS ADM GROUP BY JUDET) AS ADM RIGHT OUTER JOIN gisdata_owner.JUDETESIMP AS JUD ON JUD.DEN_JUD = ADM.JUDET) AS ADM INNER JOIN (SELECT AGR.MACROREGIUNI_REGIUNI_DE_DEZVO AS DENUMIRE, AGR.ANUL_2014 FROM gisdata_owner.AGR101A AS AGR WHERE AGR.FORME_DE_PROPRIETATE = 'Total' AND AGR.MODUL_DE_FOLOSINTA_A_FONDULUI = 'Total') AS AGR ON AGR.DENUMIRE = ADM.DENUMIRE
Windows 10 Pro v. 2004 (build 19041.572)
ArcGIS Desktop Advanced / ArcMap 10.5.0.6491
SQL Server 2014 Developer Edition (12.0.6118.4)