I have thousands of databases in Microsoft SQL Server and I want the user to select the server and DB they want so I cannot use arcpy.ArcSDESQLExecute
I am trying to use pymssql to connect to the database and run a query. I understand how to save it as a CSV but I am struggling to understand how to save the results of the query to an in_memory table.
Here is my code to save it as a CSV.
from os import getenv import pymssql import pandas as pd import arcpy import csv from decimal import Decimal conServer = arcpy.GetParameterAsText(0) #r"server\server" #conUser = getenv("put your username") #conPassword = getenv("put your password") condb = arcpy.GetParameterAsText(1) #"databaseName" conn = pymssql.connect(server=conServer, database=condb) cursor = conn.cursor() query1 = "some sql query" cursor.execute(query1) with open(r"D:\GIS Data\output.csv", "w") as outfile: writer = csv.writer(outfile, quoting=csv.QUOTE_NONNUMERIC) writer.writerow([i for i in cursor.description]) # write headers for row in cursor: writer.writerow(row) cursor.close() conn.close() arcpy.SetParameter(3, outfile)