提问人:mkylfnur 提问时间:6/4/2023 最后编辑:Gord Thompsonmkylfnur 更新时间:6/5/2023 访问量:113
如何使用fast_executemany将mm / dd / yyyy字符串插入SQL Server日期时间列?
How to insert mm/dd/yyyy strings into a SQL Server datetime column using fast_executemany?
问:
我正在从 BQ 查询数据。我必须将其插入到 SQL Server 2008 中。
伪代码:
# get results from bigquery
client = bigquery.Client()
result = client.query(sql).result()
# create parametrized insert sql statement
sql = 'INSERT INTO [SANDBOX].[dbo].[table_stg] VALUES ((?),(?),(?),(?),(?),(?),(?),(?),(?),(?))'
data = [(2016, 'STRING', 'STRING', 'STRING', 'STRING', 'STRING', INT, 'STRING', '09/28/2015', '09/25/2016'),
(2016, 'STRING', 'STRING', 'STRING', 'STRING', 'STRING', INT, 'STRING', '09/28/2015', '09/25/2016')]
# target table schema
[varchar](50) NULL,
[varchar](50) NULL,
[varchar](50) NULL,
[varchar](50) NULL,
[varchar](50) NULL,
[varchar](50) NOT NULL,
[int] NULL,
[varchar](50) NULL,
[datetime] NULL,
[datetime] NULL
# insert into sql server with fast_executemany
cursor.fast_executemany = True
cursor.executemany(sql, data)
conn.commit()
cursor.close()
conn.close()
因此,当我运行cursor.fast_executemany = True的cursor.executemany(sql, data)时,我收到错误:
强制转换规范的字符值无效。
字符串数据,右截断:长度 44 缓冲区 20,HY000
该代码在没有 cursor.fast_executemany = True 的情况下完美运行,但它以单次提交执行每次插入,这意味着 8 小时才能传输 2 百万行。
提前感谢您的帮助。 此外,如果有一些过滤机制,我可以起诉拆分为fast_execute和正常执行,我将不胜感激。
编辑:根据评论编辑问题
答:
此处的相关问题说明了如何影响对列的空字符串参数的解释方式。使用 ,空字符串被解释为 。使用 时,空字符串参数会导致“强制转换规范的字符值无效”错误。fast_executemany = True
datetime
fast_executemany = False
1900-01-01 00:00:00
fast_executemany = True
字符串参数似乎也是如此。该格式可能不明确(是 OR ?),并且 SQL Server 在 UNDER 下做出假设,而不是在 下做出假设。aa/bb/yyyy
mm/dd/yyyy
dd/mm/yyyy
fast_executemany = False
fast_executemany = True
对于定义为d1 [datetime]
cursor.fast_executemany = False
cursor.executemany(
"INSERT INTO [SANDBOX].[dbo].[table_stg] (d1) VALUES (?)",
[("09/28/2015",)]
)
但这失败了
cursor.fast_executemany = True
cursor.executemany(
"INSERT INTO [SANDBOX].[dbo].[table_stg] (d1) VALUES (?)",
[("09/28/2015",)]
)
但是,这有效:
cursor.fast_executemany = True
cursor.executemany(
"INSERT INTO [SANDBOX].[dbo].[table_stg] (d1) VALUES (?)",
[("2015/09/28",)]
)
因此,要么重新格式化字符串,要么将它们解析为对象yyyy/mm/dd
datetime
cursor.fast_executemany = True
cursor.executemany(
"INSERT INTO [SANDBOX].[dbo].[table_stg] (d1) VALUES (?)",
[(datetime.datetime.strptime("09/28/2015", "%m/%d/%Y"),)]
)
评论
crsr.executemany("INSERT INTO my_table (my_column) VALUES (?)", [("my'problem",), ("what'ever",)])