Er_Faizan
Er_Faizan

Reputation: 39

How to use python functions in sql select statement

I have one function(token_sort_ratio()) in python which compares two strings and gives the output as percentage match between them. I want to use that function in sql query which i'm running in python using pyodbc. But i cannot achieve it. Can anyone help me doing that.? Here is my code

cursor = conn.cursor() 
sql_query ="""select """+fuzz.token_sort_ratio("""[MAT DESC],MAT_GROUP_DESC""")+""" as percentage_match ,[MAT DESC] MAT_GROUP_DESC from Sample_data """  
cursor.execute(sql_query)  
rows = cursor.fetchall()

Upvotes: 2

Views: 3914

Answers (1)

nimdil
nimdil

Reputation: 1381

You can't. You can't even through IronPython compiled into DLL Assembly and such tricks. Your best bet is to rewrite it into T-SQL or - maybe a bit easier stuff - into one of the .NET languages and load them through .dll to SQL Server. I think the F# is perhaps most similar to Python but on the other hand the documentation on writing .dll for SQL Server is mostly for C#/VB.net so consider writing more complex stuff in C#

Upvotes: 1

Related Questions