我在主程序中有以下代码:
cleanDuplicatesAttendeesSQL = attendees.clean_duplicates()
sqlUtilities.databaseWorkerExecute(cleanDuplicatesAttendeesSQL)
attendees.clean_duplicates()
设置变量cleanDuplicatesAttendeesSQL
:
def clean_duplicates():
return """
WITH cte AS(
SELECT [Sessions]
,[Confirmation #]
,[First Name]
,[Last Name]
,ROW_NUMBER() OVER (
PARTITION BY
[Sessions]
,[Confirmation #]
,[First Name]
,[Last Name]
ORDER BY
[Sessions]
,[Confirmation #]
,[First Name]
,[Last Name]
) row_num
FROM attendees
)
DELETE FROM cte
WHERE row_num > 1"""
然后,我将cleanDuplicatesAttendeesSQL
传递给以下函数以对数据库执行:
def databaseWorkerExecute (sql):
engine = sqlalchemy_connect()
try:
engine.execute(sql)
print("Execution successful.")
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print (error)
sys.exit("Execution was not sucessful. Exiting the program...")
这个函数没有错误,我每次都得到一个“执行成功”。但是,当我查看数据库中的表时,仍然会看到重复项。我成功地使用了相同的databaseWorkerExecute
函数来执行更新、插入和删除等其他操作。如何使CTE也工作
SQL输出的屏幕截图:
目前没有回答
相关问题 更多 >
编程相关推荐