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

时间:2019-04-02 22:57:56

标签: python tsql pyodbc

我正在尝试通过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语句值(?,?,?)”,变量)合并到cursor.execute中,并与其他sql语句一起执行?我试过了,但是出错了...     cursor_insert.execute('合并为dbo.jjy_table作为目标'     'USING Incidents_All AS Source'     'ON Target.Incident_Id = Source.Incident_ID'     “当目标未匹配时”     '“ INSERT([Incident_Id],[Incident_Type],[Priority])     值(?,?,?)“,IncIncident_ID,IncIncident_Type,IncPriority'     “匹配之后”     '更新设置Target。[Incident_Type] = IncIncident_Type,Target。[Priority] =      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()

1 个答案:

答案 0 :(得分: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个?,并且在初始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)