Python Forum
MSSQL query not working in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MSSQL query not working in Python
#1
the MSSQL Query works just fine by its self in SQL Studio and if I remove
this line
and ompRequestedShipDate >=getdate() and ompRequestedShipDate <=getdate()+7
it will work in python. But with this line it gets the column names and nothing else. no error when I remove the line I get data in python.

import datetime import pyodbc import xlsxwriter import pandas as pd from os import environ, lseek from datetime import timedelta #from plyer import notification #from datetime import datetime begin = pd.to_datetime("today") CONNECTION_STRING="""Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1; Server=server; Database=M1_KF; UID=who; PWD=password;""" connection = pyodbc.connect(CONNECTION_STRING) with pd.ExcelWriter("Output.xlsx", engine="xlsxwriter", options = {'strings_to_numbers': False, 'strings_to_formulas': False}) as writer: try: df = pd.read_sql_query("""select top 10000 omlPartID as Part ,CONVERT(varchar(7),ompRequestedShipDate,100) as day ,sum(omlOrderQuantity) as ordered ,CASE WHEN imbWarehouseID='' THEN 'WH1' WHEN imbWarehouseID='WH2' THEN 'WH2' else 'None' end as Warehouse ,cast(imbQuantityOnHand as INT) as onhand from M1_KF.dbo.SalesOrders left outer join M1_KF.dbo.SalesOrderLines on omlSalesOrderID=ompSalesOrderID left outer join M1_KF.dbo.Parts on impPartID=omlPartID left outer join M1_KF.dbo.PartRevisions on imrPartID=omlPartID left outer join M1_KF.dbo.PartBins on imbPartID=omlPartID where ompClosed!=-1 and imbWarehouseID!='vw' and UOMPTRUCKNUMBER!='' and ompRequestedShipDate >=getdate() and ompRequestedShipDate <=getdate()+7 group by omlPartID,ompRequestedShipDate,imbWarehouseID ,imbQuantityOnHand""",connection) df.to_excel(writer,sheet_name="Sheet1", header=True , index =False) connection.close() print("File saved successfully") except: print("There is an error")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling data from mssql to PG DB hartman60 1 1,008 Jan-31-2025, 12:26 PM
Last Post: hartman60
  python connect to mssql wailoonho 7 8,943 Dec-07-2023, 02:06 AM
Last Post: wailoonho
  list the files using query in python arjunaram 0 1,446 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python sql query single quote in a string mg24 1 2,992 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  "SUMIF" type query in Python (help required) BlainEillimatta 0 1,961 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  from MSSQL to excel kat35601 1 3,024 Apr-11-2022, 06:19 PM
Last Post: buran
  Error using mariadb select query with form in python? shams 2 3,431 Jul-29-2021, 12:30 PM
Last Post: shams
  Python mysql query help please tduckman 4 6,444 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  SQLite Query in Python rowyourboat 2 4,305 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  Python Turtle and order of implementation query Parsleigh 2 4,036 Mar-04-2019, 02:43 PM
Last Post: Parsleigh

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.