SQL-使用WITH在INSERT INTO上声明变量
技术问答
206 人阅读
|
0 人回复
|
2023-09-12
|
我正在尝试使用它WITH时间是查询声明的变量INSERTINTO。我正在关注https://stackoverflow.com/a/165241/2923526提供以下示例SELECT查询示例:
) T; }% I9 v5 D- lWITH myconstants (var1,var2) as ( values (5,'foo'))SELECT *FROM somewhere,myconstantsWHERE something = var1 OR something_else = var2;我尝试了以下方法,没有运气:) X3 N3 O+ N1 o" g8 d- B
playground> CREATE TABLE foo (id numeric)CREATE TABLEplayground> WITH consts (x) AS (VALUES (2)) INSERT INTO foo VALUES (x)column "x" does not existLINE 1: WITH consts (x) AS (VALUES (2)) INSERT INTO foo VALUES (x) ^playground> WITH consts (x) AS (VALUES (2)) INSERT INTO foo VALUES (consts.x)missing FROM-clause entry for table "consts"LINE 1: ...consts (x) AS (VALUES (2)) INSERT INTO foo VALUES (consts.x) ^playground> WITH consts (x) AS (VALUES (2)) INSERT INTO foo VALUES (consts.x) FROM constssyntax error at or near "FROM"LINE 1: ...AS (VALUES (2)) INSERT INTO foo VALUES (consts.x) FROM const... ^playground> WITH consts (x) AS (VALUES (2)) INSERT INTO foo FROM consts VALUES (consts.x)syntax error at or near "FROM"LINE 1: WITH consts (x) AS (VALUES (2)) INSERT INTO foo FROM consts ... ^注意:我是SQL初学者,所以我在寻找避免暗示的初学者PLPGSQL解决方案的方法。
' ^0 L* d: b9 b8 C
% ]) }# _0 `; t- s/ P s* ` 解决方案: |
|
|
|
|
|