简体   繁体   English

如何将时间戳(使用 GETDATE())添加到我的插入语句中?

[英]How do I add timestamp (using GETDATE()) to my insert statement?

I'm trying to figure out how to add a timestamp to my database table.我想弄清楚如何将时间戳添加到我的数据库表中。 df2 doesn't include any column for time so i'm trying to create the value either in values_ or when I execute to sql. I want to use the GETDATE() redshift function df2 不包含任何时间列,所以我试图在 values_ 或执行到 sql 时创建值。我想使用 GETDATE() redshift function

  values_ = ', '.join([f"('{str(i.columnA)}','{str(i.columnB)}','{str(i.columnC)}','{str(i.columnD)}', 'GETDATE()')" for i in df2.itertuples()])
        
        sqlexecute(f'''insert into table.table2 (columnA, columnB, columnC, columnD, time_) 
                values
        ({values_})
        ;
        ''')

This is one of several errors I get depending on where I put GETDATE()这是我遇到的几个错误之一,具体取决于我放置 GETDATE() 的位置


FeatureNotSupported: ROW expression, implicit or explicit, is not supported in target list

The "INSERT... VALUES (...)" construct is for inserting literals into a table and getdate() is not a literal. “INSERT...VALUES (...)”构造用于将文字插入表中,而 getdate() 不是文字。 However, there are a number of ways to get this to work.但是,有多种方法可以使其发挥作用。 A couple of easy ways are:几个简单的方法是:

  1. You can make the default value of the column 'time_' be getdate() and then just use the key work default in the insert values statement.您可以将“time_”列的默认值设置为 getdate(),然后在 insert values 语句中使用键 work default。 This will tell Redshift to use the default for the column (getdate())这将告诉 Redshift 使用列的默认值 (getdate())

    insert into values ('A', 'B', 3, default)插入值('A','B',3,默认值)

  2. You could switch to a "INSERT... SELECT..." construct which will allow you to have a mix of literals and function calls.您可以切换到“INSERT ... SELECT ...”构造,这将允许您混合使用文字和 function 调用。

    insert into table (select 'A', 'B', 3, getdate())插入表(选择“A”、“B”、3、getdate())

NOTE: inserting row by row into a table in Redshift can slow and make a mess of the table if the number of rows being inserted is large.注意:如果插入的行数很大,在 Redshift 中逐行插入表可能会减慢速度并弄乱表。 This can be compounded if auto-commit is on as each insert will be committed which will need to work its way through the commit queue.如果自动提交开启,这可能会更加复杂,因为每个插入都将被提交,这将需要通过提交队列。 If you are inserting a large amount of data you should do this through writing an S3 object and COPYing it to Redshift.如果您要插入大量数据,您应该通过写入 S3 object 并将其复制到 Redshift 来执行此操作。 Or at least bundling up 100+ rows of data into a single insert statement (with auto-commit off and explicitly commit the changes at the end).或者至少将 100 多行数据捆绑到一个插入语句中(关闭自动提交并在末尾显式提交更改)。

when i created the table i added a time_log column using timestamp当我创建表时,我使用时间戳添加了一个 time_log 列

drop table if exists table1;如果表 1 存在则删除表; create table table1( column1 varchar (255), column2 varchar(255), time_log timestamp );创建表 table1( column1 varchar (255), column2 varchar(255), time_log 时间戳);

The issue was i had parentheses around the values in my insert statement.问题是我在插入语句中的值周围加了括号。 remove those and it will work.{values_}删除那些,它将起作用。{values_}

sqlexecute(f'''insert into table.table2 (columnA, columnB, time_log) values ({values_}) sqlexecute(f'''insert into table.table2 (columnA, columnB, time_log) values ({values_})
; ; ''') ''')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果有声明,我如何向我的 Lambda 添加更多政策? - How can i add more policies to my Lambda if there is a Statement? 如何将 channelId 添加到我的通知中? - How do I add a channelId to my notification? 如何在 Flutter dart 的 ListView 中添加 floatingactionbutton - How do I add floatingactionbutton in my ListView in Flutter dart 如何正确地将工作节点添加到我的集群? - How do I correctly add worker nodes to my cluster? 如何将 Firestore 日期/时间戳转换为 JS Date()? - How do I convert a Firestore date/Timestamp to a JS Date()? 如何在python中添加时间戳? - How to add timestamp in python? 如何将 firebase 添加到我的反应项目中? - How do I add firebase to my react project? 如何使用 append 将列表添加到我的 DynamoDB 表中 Python? - How do I append a list to my DynamoDB table using Python? 如何为 google data studio 添加安全组以访问我的 amayon RDS? - How do I add security group for google data studio to access my amayon RDS? 如何将 Firestore 日期/时间戳转换为 Kotlin 中的日期? - How do I convert a Firestore date/Timestamp to Date in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM