python - python 使用 query_1 导致 query_2 where 条件,因为这两个查询都将在不同的 sql server 中运行
问题描述
python 使用 query_1 导致 query_2 where 条件,因为这两个查询都将在不同的 sql server 中运行
cnxn = pyodbc.connect(driver = '{SQL Server}', host = 'server1', database = 'db1', user = '', password = '', Trusted_Connection = 'yes')
cursor = cnxn.cursor()
cnxn1 = pyodbc.connect(driver = '{SQL Server}', host = 'server2', database = 'db2', user = '', password = '', Trusted_Connection = 'yes')
cursor1 = cnxn1.cursor()
query = "SELECT top(10) Batch_ID FROM [db1].[dbo].[table1];"
cursor.execute(query)
Result = cursor.fetchall()
print (Result)
query1="delete FROM [db2].[dbo].[table2] where Batch_ID in (**Result**);" ###i have use 1st query result in this where condition
cursor.execute(query1)
Result1 = cursor1.fetchall()
解决方案
你真的不需要IN
为你的陈述构建一个子句DELETE
。您可以只使用常规... WHERE Batch_ID = ?
,然后将值列表传递给.executemany
,例如
query = "SELECT TOP(10) Batch_ID FROM [db1].[dbo].[table1];"
cursor.execute(query)
result = cursor.fetchall()
query1 = "DELETE FROM [db2].[dbo].[table2] WHERE Batch_ID = ?;"
cursor.executemany(query1, result)
推荐阅读
- c# - 从 httpcontext dotnet 6 获取控制器名称
- spring-boot - JSON解析错误:无效的UTF-8中间,方法POST Spring Boot
- python - tf.keras GradientTape:获取关于输入的梯度(返回 nans)
- angular - Angular - 如何在初始负载上具有由父组件确定的动态负载?
- javascript - 在 React native 中,在同一行文本中使用不同的 fonFamily
- python - 根据 pandas 数据框中的列对行求和
- python - 如何从弹出窗口中抓取数据(我需要抓取仅在单击弹出窗口后才可见的数据,这不是链接)
- verilog - Verilog 参数声明是否需要默认值?
- ios - React Native:在 iPhone 11 (IOS 15.0) 上成功构建后,IOS 应用程序在启动屏幕上崩溃
- asp.net-mvc - asp.net MVC 中的 OIDC 会话管理(checksession)是否可行?