Using SQL Server stored procedures from Python (pyodbc)

后端 未结 7 913
广开言路
广开言路 2020-12-30 00:23

I\'m have a stored procedure, code:

DECLARE @RC int 
DECLARE @id varchar(13) 
DECLARE @pw varchar(13) 
DECLARE @depart varchar(32) 
DECLARE @class varchar(12         


        
相关标签:
7条回答
  • 2020-12-30 01:13

    After searching everywhere for this solution, i couldnt find a simplified version. All results seem to overcomplicate this that should be so easy to do. Heres my solution.

     import pyodbc
     import pandas as pd
     import datetime as d
    
    
      conn = pyodbc.connect('Driver=;'
                      'Server=;'
                      'Database=;'
                      'UID=;'
                      'PWD=;')
    
    
         # define parameters to be passed in and out
    
         quarter_date = d.date(year=2020, month=10, day=1)
    
         SQL = r'exec TERRITORIES_SP @quarterStart = ' + "'" + str(quarter_date) + "'"
    
         print(SQL)
    
         try:
             cursor = conn.cursor()
              cursor.execute(SQL)
              cursor.close()
              conn.commit()
        finally:
              conn.close()
    
    0 讨论(0)
提交回复
热议问题