【问题标题】:pyodbc.ProgrammingError: ("A TVP's rows must be Sequence objects.", 'HY000')pyodbc.ProgrammingError: ("TVP 的行必须是序列对象。", 'HY000')
【发布时间】:2020-01-17 09:26:29
【问题描述】:

我正在尝试使用 python 脚本将一些数据插入到 SQL Server 数据库中。

cursor.execute(''' insert into TM_VISITAS(
                    idVisita,
                    sesion,
                    idCliente,
                    idPhygi
                    )VALUES(
                    NEXT VALUE FOR seq_visitas,
                    ?,
                    ?,
                    ?
                ) ''',
                (
                total_dict.get("Session_id") ,
                total_dict.get("Client_id"),
                total_dict.get("Device_id") 
                )
            ) 

但这是我收到的:

pyodbc.ProgrammingError: ("A TVP's rows must be Sequence objects.", 'HY000')

谁能告诉我如何使用 python 插入一个序列值?

【问题讨论】:

    标签: python sql-server python-3.x sequence sql-insert


    【解决方案1】:

    我也遇到了同样的问题。 我能够更好地理解这里的原因:python list in sql query as parameter

    此外:我认为您不需要执行语句中逗号后的括号

    看这里:

    self.cursor.execute('INSERT INTO test_table VALUES (?,?,?,?,?,?,?,?)',
                        country_id,
                        product_id,
                        project_name,
                        description,
                        license_type,
                        date,
                        valid,
                        confirmed)
    

    我遇到了和你一样的错误,是因为date属性是一个列表

    【讨论】:

      【解决方案2】:

      当我将插入创建为存储过程时,它对我有用。

          sql_query = "DECLARE @RC int; EXEC @RC = [dbo].[sp] 
          @id=%s,@nm='%s',@module=%s,@type='%s',@file='%s'; SELECT @RC AS rc;"%(str(id), 
          'sales.csv', 1, 'SALES','sales_shift')
      

      PS:这里的序列不是输入,而是sp的一部分

          procedure [dbo].[sp] (@id int,
              @nm varchar(100),
              @module int,
              @fact varchar(30),
              @file varchar(50))
          AS
      
          BEGIN
          SET NOCOUNT ON;
          DECLARE @cur_val bigint;        
          SELECT @cur_val = NEXT VALUE FOR dbo.seq
          INSERT INTO table(seq_id,id,nm,time,status,m_id, type,type_name)VALUES (@cur_val,@id,@nm,getdate(),'READY',@module, @type, @file);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        • 2021-07-23
        • 2020-12-31
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多