问题
I am trying to execute and sql query through python. This is based on my first question on how to split columns based on two dataframes(Splittin of company extensions and company name). Since I was finding it difficult to code in python, I used sql query to split and executed through python. I am able to execute just queries like update, etc but when I try using loops am not getting an output nor any error.
My input:
Original_Input Cleansed_Input
DORO INC ( O/S DORO SAFETY & SECURITY) DORO INC OS DORO SAFETY SECURITY
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris
International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
My expected output:
Original_Input Cleansed_Input
DORO INC ( O/S DORO SAFETY & SECURITY) DORO INC OS DORO SAFETY SECURITY
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris
International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
Core_Input Type_Input
Iris Diagnostics a Division of Iris International Incorporated
GINGIPAK a division of The Belport Company Incorporated
When I tried just the update statement it was working fine but then there is a priority given for the splitting so I tried using while loop and execute through python which didn't work. It was working fine in sql but not through python.
In the code Tempcompanyname is my table with input and output. Company_Extension is another table with extension, abbreviation and priority.
Eg of my second table is as below.
Name_Extension Company_Type Priority
co llc Company LLC 2
Pvt ltd Private Limited 8
Corp orporation 4
Co inc Company Incorporated 9
This is my piece of code.
engine.execute('''Declare @priority int = (select max(priority) from [company_Extension])
Declare @p int=1
while @p <= @priority
begin
update A
set A.Type_input = B.Company_Type
from [TempCompanyName]A (nolock), [company_Extension]B where A.Cleansed_Input like
'%'+B.Name_Extension and Priority=@p
update A
set A.Core_Input =replace(A.[Cleansed_Input],B.Name_Extension,'')
from [TempCompanyName]A (nolock), [company_Extension]B where A.Cleansed_Input like
'%'+B.Name_Extension and Priority=@p
set @p=@p+1
end''')
engine.execution_options(autocommit=True)
Thanks in advance for the help.
来源:https://stackoverflow.com/questions/59820976/how-to-execute-sql-while-loops-through-pandas-library-sqlalchemy