2

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()
4
  • 1
    This doesn't make sense at all. How could it possibly work? Commented Apr 26, 2017 at 8:21
  • Is there any work around to use python user defined function on columns of a table? Commented Apr 26, 2017 at 8:26
  • 1
    Welcome to Stack Overflow. You can either read back all the records from the database and work on them in Python or you can write your percentage match in SQL from the start. I would recommend rewriting it in SQL as it will be more efficient. You cannot ask SQL to run your Python commands. Commented Apr 26, 2017 at 9:48
  • 1
    Rob, What can be the best way to read all records in python and apply that function? I'm new to python I don't know much about it. Commented Apr 26, 2017 at 10:22

1 Answer 1

1

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#

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

Comments

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.