【问题标题】:Using VTL Velocity, how can I create VALUES array to use in SQL Statement使用 VTL Velocity,如何创建 VALUES 数组以在 SQL 语句中使用
【发布时间】:2019-06-04 03:28:15
【问题描述】:

我正在尝试在我的 AWS 解析器中创建一个使用值的 SQL 语句。

例如:

#set( $inserts = [] )
#foreach ( $item in $ctx.args.input.listItems)
        $util.qr($inserts.add( ['$item.item_id', '$item.item_name', 'item.item_image_id'] ))
#end

而我的 SQL 语句是:

#set( $sql = "INSERT INTO items (item_id, item_name, item_image_id) VALUES $inserts" )

但是这不起作用。我有另一种方法来创建这种类型的 SQL 语句吗? 谢谢

【问题讨论】:

    标签: sql velocity aws-appsync vtl


    【解决方案1】:

    我会这样做:

    #set( $sql = "INSERT INTO items (item_id, item_name, item_image_id) VALUES " )
    #foreach ( $item in $ctx.args.input.listItems)
        #set( $sql = $sql + "($item.item_id, $item.item_name, $item.item_image_id)")
        #if( $foreach.hasNext )
            #set( $sql = $sql + ", ")
        #else
            #set( $sql = $sql + ";")
        #end
    #end
    $sql
    

    此解决方案对您有用吗?

    $ctx 视为

    {
        ctx: {
            args: {
                input: {
                    listItems: [
                        {
                            item_id: "itemId1",
                            item_name: "itemName1",
                            item_image_id: "itemImageId1"
    
                        },
                        {
                            item_id: "itemId2",
                            item_name: "itemName2",
                            item_image_id: "itemImageId2"
    
                        }
                    ]
                }
            }
        }
    }
    

    输出等于INSERT INTO items (item_id, item_name, item_image_id) VALUES (itemId1, itemName1, itemImageId1), (itemId2, itemName2, itemImageId2);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 2012-06-26
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多