【问题标题】:Inserting data into a Postgres table if data is not null如果数据不为空,则将数据插入 Postgres 表
【发布时间】:2017-04-03 06:33:15
【问题描述】:

我正在使用 Postgres 的公用表表达式,如果数据中的值不为空,我会 - 如果可能的话 - 将数据插入表中:

WITH prod1 AS (
   INSERT INTO p1 ... RETURNING id), 
     prod2 AS (
   INSERT INTO p2 (fk,foo,bar) VALUES (prod1.id,@Foo,@Bar) 
     RETURNING id)

如果@Foo 和@Bar 的值等于null,postgres 是否可以跳过将数据插入到p2 中,否则会创建一个空白引用?或者,我必须通过代码手动检查并根据需要插入吗?

【问题讨论】:

    标签: postgresql dapper


    【解决方案1】:
    insert into p2 (fk, foo, bar)
    select prod1.id, @foo, @bar
    from prod1
    where @foo is not null or @bar is not null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多