0

I'm trying to replace some old MSSQL stored procedures with python, in an attempt to take some of the heavy calculations off of the sql server. The part of the procedure I'm having issues replacing is as follows

UPDATE mytable
SET calc_value = tmp.calc_value
  FROM dbo.mytable mytable INNER JOIN 
       @my_temp_table tmp ON mytable.a = tmp.a AND mytable.b = tmp.b AND mytable.c = tmp.c
  WHERE (mytable.a = some_value)
    and (mytable.x = tmp.x)
    and (mytable.b = some_other_value)

Up to this point, I've made some queries with SQLAlchemy, stored those data in Dataframes, and done the requisite calculations on them. I don't know now how to put the data back into the server using SQLAlchemy, either with raw SQL or function calls. The dataframe I have on my end would essentially have to work in the place of the temporary table created in MSSQL Server, but I'm not sure how I can do that.

The difficulty is of course that I don't know of a way to join between a dataframe and a mssql table, and I'm guessing this wouldn't work so I'm looking for a workaround

1
  • Did you find any way to do this? I am try to do similar task.. Commented Jul 20, 2018 at 20:10

1 Answer 1

1

As the pandas doc suggests here :

from sqlalchemy import create_engine 
engine = create_engine("mssql+pyodbc://user:password@DSN", echo = False)
dataframe.to_sql('tablename', engine , if_exists = 'replace')

engine parameter for msSql is basically the connection string check it here

if_exist parameter is a but tricky since 'replace' actually drops the table first and then recreates it and then inserts all data at once.

by setting the echo attribute to True it shows all background logs and sql's.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for a reply. Looking at the to_sql documentation (pandas.pydata.org/pandas-docs/stable/generated/…) it seems there isn't an update option. Would there be any way you know of to update only records which match on certain columns, while keeping the rest of the records?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.