所以我有一个要连接到并在python中执行的存储过程,因此我可以将数据导出为精美的表。
这是我正在使用的代码,注释是我尝试过的代码。
import plotly as py
import plotly.figure_factory as ff
import pandas as pd
import pypyodbc
import pyodbc
import datetime
import seaborn as sns
import matplotlib.pyplot as plt
#force the ODBC driver to use case-sensitive column names
pypyodbc.lowercase = True
startdate = '2018-10-23 08:00'
enddate = '2018-10-24 12:00'
params = startdate,enddate
##Establishing a connection to SQL database
connection = pypyodbc.connect('Driver={SQL Server};'
'Server=LOLOL;'
'Database=LOL_Collection;'
'trusted_connection=yes;')
crsr = connection.cursor()
SQL = """
SET NOCOUNT ON
declare @start = '2018-10-23 08:00'
declare @end = '2018-10-24 12:00'
exec [dbo].[AM_SIGNAL_Histogram] @start, @end
"""
#SQL = """
#
#exec [dbo].[AM_SIGNAL_Histogram] @start, @end
#
#"""
#result = crsr.execute(SQL)
#
#print(result.fetchall())
df = pd.read_sql(SQL,connection)
connection.close()
这是我不断得到的错误:
ProgrammingError :(“ 42000”,“ [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]“ =”附近的语法不正确。”)
感谢您的帮助, 肯尼思
答案 0 :(得分:0)
问题不在于您的Python代码,而在于您的T-SQL。直接在SQL Server Management Studio(SSMS)中运行它,
SET NOCOUNT ON
declare @start = '2018-10-23 08:00'
declare @end = '2018-10-24 12:00'
exec [dbo].[AM_SIGNAL_Histogram] @start, @end
产生
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@start".
您的declare
语句缺少数据类型。以下修改对我有效:
SET NOCOUNT ON;
declare @start varchar(50) = '2018-10-23 08:00';
declare @end varchar(50) = '2018-10-24 12:00';
exec [dbo].[AM_SIGNAL_Histogram] @start, @end;