postgresql 使用WITH...AS语句时出现语法错误

ql3eal8s  于 2023-03-17  发布在  PostgreSQL
关注(0)|答案(2)|浏览(850)

我正在向表中插入数据,我想获取最后一个ID,稍后将用于另一个查询。
下面是表结构和查询sql fiddle
我收到此错误
错误:输入结束位置出现语法错误:890
insert语句中可能有什么错误?

6l7fqoea

6l7fqoea1#

在所有的DBMS中,当你使用 with... as() 语法声明一个“视图”时,你应该立即使用它。如果你试图只执行 with... as() 结构,你会得到一个错误,PostgreSQL会等待你完成查询。
您有两种选择:要么删除“with”构造并获得“returning”子句的结果,要么向通过 with... as() 创建的“视图”追加一个查询以读取其内容,如下所示:

with app as (
 INSERT INTO public.tag 
 (class, name, value_type, history_type, persistent_history_limit_type,persistent_history_size, cache_size, unity, threshold_group, threshold, threshold_saving_option, threshold_time, uses_linear_regression, linear_regression_cluster_period, linear_regression_number_of_clusters, enum_mapping, site_id, as_id, use_bit_info, tag_bit, writing_only, active, detail, scanner_id, top_output, top_input, bottom_input, bottom_output, primary_rated, transducer_rated, secondary_rated, raw_upper_range, invert, scaling_type, relative_offset, absolute_offset)
 VALUES
 ('TAG', 'PLCOFFLINEx', 2, 1, 0, 1, 1, '', 'System', 1.0, 2, 0, 0, 0, 0, '', 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) RETURNING oid as lastid
)
 select * from app

来自文档:
...并且WITH子句本身附加到主语句,该主语句也可以是SELECT、INSERT、UPDATE或DELETE
希望这能有所帮助!

smdncfj3

smdncfj32#

SQL小提琴

with lastinsert AS (
INSERT INTO public.tag ("class", "name", value_type, history_type, persistent_history_limit_type, persistent_history_size, cache_size, unity, threshold_group, threshold, threshold_saving_option, threshold_time, uses_linear_regression, linear_regression_cluster_period, linear_regression_number_of_clusters, enum_mapping, site_id, as_id, use_bit_info, tag_bit, writing_only, active, detail, scanner_id, top_output, top_input, bottom_input, bottom_output, primary_rated, transducer_rated, secondary_rated, raw_upper_range, invert, scaling_type, relative_offset, absolute_offset)
VALUES('TAG', 'PLCOFFLINEx', 2, 1, 0, 1, 1, '', 'System', 1.0, 2, 0, 0, 0, 0, '', 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) 
 RETURNING oid)
 select oid as lastid from lastinsert

相关问题