PYODBC / SQL:协助包含多个SQL语句的字符串

问题描述 投票:0回答:1

我正在尝试通过cursor.execute发送SQL语句,但在字符串中有多个语句时无法获得正确的语法。

我用简单的insert语句测试了cursor.execute(),它填充了tsql数据库表。

当尝试应用对包含多个语句的cursor.execute()字符串起​​作用的语法时,会出现此问题。

4/3更新:所以我认为这是我需要解决的问题,看来我需要使用格式(“SQL语句值(?,?,?)”,变量)。因为我没有引用列,所以如果我尝试使用,我会收到错误:'INSERT([Incident_Id],[Incident_Type],[Priority])VALUES(IncIncident_ID,IncIncident_Type,IncPriority)'

那么如何将这种语法(“SQL语句值(?,?,?)”,变量)与其他sql语句一起合并到cursor.execute中?我尝试了这个但是错误了... cursor_insert.execute('MERGE INTO dbo.jjy_table as Target''使用Incidents_All AS Source''On Target.Incident_Id = Source.Incident_ID''当没有匹配目标那么''“INSERT ([Incident_Id],[Incident_Type],[Priority])值(?,?,?)“,IncIncident_ID,IncIncident_Type,IncPriority''匹配然后'更新设置目标。[Incident_Type] = IncIncident_Type,Target。[优先级] = IncPriority;')


import pyodbc 
import os.path
import logging

import datetime
import time

import os
import logging.handlers

conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=TESTSERVER;"
"Database=ITSM;"
"Trusted_Connection=yes;"
"MARS_Connection=Yes;")

cursor=conn.cursor()
cursor_select = conn.cursor()
cursor_insert = conn.cursor()

if conn:
    print('***** Connected to TESTSERVER *****')

select_str="SELECT TOP 5 Incident_ID,Incident_Type,Priority FROM 
incidents_all WHERE incidents_all.Status NOT IN ('Closed','Resolved')"

cursor_select.execute(select_str)


while True:
    row = cursor_select.fetchone()
    if not row:
        break
    print(' Row:     ', row)

    IncIncident_ID= row[0]
    IncIncident_Type=row[1]
    IncPriority=row[2]

    print('IncIncident_ID: ',IncIncident_ID)
    print('IncIncident_Type: ',IncIncident_Type)
    print('IncPriority: ',IncPriority)

    # This works, I get the sytax, I have a statement and passing in the 
    #variables .
    cursor_insert.execute("INSERT INTO dbo.jjy_table ([Incident_Id], 
    [Incident_Type],[Priority]) values 
    (?,?,?)",IncIncident_ID,IncIncident_Type,IncPriority)

   #This Doesn't work in Python but the SQL code works in SQL if I remove 
   #the value (?,?,?)
   # I believe issue is with Python syntax but not sure how to fix. How do 
  #I incorporate the syntax above into a string of multiple statements.

  cursor_insert.execute("MERGE INTO dbo.jjy_table as Target USING 
  Incidents_All AS Source ON Target.Incident_Id = Source.Incident_ID WHEN 
  NOT MATCHED BY Target THEN "INSERT ([Incident_Id],[Incident_Type], 
  [Priority]) values (?,?,?)",IncIncident_ID, IncIncident_Type, 
  IncPriority" WHEN MATCHED THEN UPDATE SET Target.[Incident_Type] = 
  IncIncident_Type, Target.[Priority] = IncPriority;")

cursor.commit()
conn.close()
python tsql pyodbc
1个回答
0
投票

我想你想把你的参数分组为一个序列,它是execute()的第二个参数:

cursor_insert.execute("INSERT INTO dbo.jjy_table ([Incident_Id], 
    [Incident_Type],[Priority]) values 
    (?,?,?)", [IncIncident_ID,IncIncident_Type,IncPriority] )

the PYODBC wiki有更多信息。


基于使用merge语句更新。您需要指定列表中的每个参数,即使您复制了变量也是如此。请注意,下面的语句有5个?s,初始SQL字符串后面有5个execute()参数。

cursor_insert.execute("""
    MERGE INTO dbo.jjy_table as Target 
    USING Incidents_All AS Source 
    ON Target.Incident_Id = Source.Incident_ID 
    WHEN NOT MATCHED BY Target THEN 
        INSERT ([Incident_Id],[Incident_Type],[Priority]) values (?,?,?)
    WHEN MATCHED THEN
        UPDATE SET Target.[Incident_Type] = ?, Target.[Priority] = ?;
    """, IncIncident_ID, IncIncident_Type, IncPriority, IncIncident_Type, IncPriority)

© www.soinside.com 2019 - 2024. All rights reserved.