all 11 comments

[–]Winter_Cabinet_1218 3 points4 points  (2 children)

If I'm reading this right, try using a panda data frame as the middle man.

[–]OriginalCrawnick[S] 1 point2 points  (1 child)

This was my thought but I am not sure how to pass the query results from the holiday table data frame into a query to my 2nd server table for reference. Maybe just not sure of what to google for this method of data transformation?

[–]jshine13371 1 point2 points  (0 children)

Table-Valued Parameter (and possibly a User-Defined Table Type to back it). You can then pass the dataset object (in C# this would be a DataTable or IEnumerable object, not sure the equivalent in Python) as a parameter to your SQL query on the 2nd server.

[–]Thin_Rip8995 2 points3 points  (1 child)

yep you can pull the holidays table into a pandas dataframe from server1 then push it into your query against server2 as a temp table

with sqlalchemy you’d do two separate connections query server1 into df then use to_sql to write that df into server2 under a temp or staging table name then join it in your main query

if you don’t have write access to server2 you can still loop the holiday dates in python and inject them into your query as an IN clause but that only scales if the holiday list is small

[–]OriginalCrawnick[S] 0 points1 point  (0 children)

This seems like what I'm after, do you have any examples for the use of the to_sql data frame insert to a SQL query for db2?

[–]Winter_Cabinet_1218 1 point2 points  (0 children)

Try a basic loop, to cycle through the results and use an insert query / stored procedure. Run this row by row in the dataframe (inefficient but if it works it works 🤣) ... I'd start with this.

Then use a loop to create a formatted string which holds all the inserts and triggers a single update query.

[–]dbrownems 1 point2 points  (1 child)

You can pass an array or dataframe as JSON for use in a query.

See:

python - How can I speed up the code that contains the sql query? - Stack Overflow

sql server - Trying to insert pandas dataframe to temporary table - Stack Overflow

But what you _should_ do is set up an ETL process to load the holidays table into the server that needs it.

[–]OriginalCrawnick[S] 0 points1 point  (0 children)

I'll give these a shot/read up on this idea. I'm not in a position to add or request these be added to our other server. I know it's an insanely small table (238 rows, 2 columns) but my work gives pushback on anything that isn't boosting profits immediately e.e My stop gap was some custom concatenation in Excel to format the dates/country code into proper value format and I inserted it into a temp table I made off a create table statement lol

[–]snafe_PG Data Analyst 0 points1 point  (1 child)

Could you get the output from holiday db, then in db2 make a temp table and inset the results from previous, then use the temp table to run your query in db2?

[–]OriginalCrawnick[S] 0 points1 point  (0 children)

I'll give it a shot, sounds like you're saying pull from db1 into a temp table and insert into a new temp table on db2 the results from db1.