简体   繁体   English

SQL如何在插入前检查列是否存在

[英]SQL How to check if column exists before inserting

I need to migrate company emails into user emails我需要将公司电子邮件迁移到用户电子邮件

INSERT INTO public.user_emails ( SELECT DISTINCT
    ar. "userId",
    c. "companyEmail"
FROM
    public.companies AS c
    INNER JOIN public.account_roles AS ar ON c.id = ar. "companyId"
        AND ar. "employeeId" IS NULL
WHERE
    ar. "userId" NOT IN (
        SELECT
            "userId"
        FROM
            public.user_emails));

At the moment I get this error:目前我收到此错误:

[ERROR] AssertionError [ERR_ASSERTION]: ifError got unwanted exception: column c.companyEmail does not exist [ERROR] AssertionError [ERR_ASSERTION]:ifError 得到了不需要的异常:列 c.companyEmail 不存在

How can I add IF EXISTS or CASE logic here to check if c.companyEmail exists?如何在此处添加IF EXISTSCASE逻辑以检查c.companyEmail存在?

It seems postgresql does have this statement.看来 postgresql 确实有这个声明。

ALTER TABLE companies 
  ADD companyEmail IF NOT EXISTS companyEmail varchar(32);

Then after this you can run the insert sql.然后在此之后您可以运行插入 sql。

But again, this is not recommended.但同样,不建议这样做。 DDL and DML should be run with different roles, the role to insert should not have permission to change table structure. DDL 和 DML 应该以不同的角色运行,插入的角色应该没有更改表结构的权限。

which kind of database?什么样的数据库? if mysql, you can try INSERT INTO .. ON DUPLICATE KEY如果是 mysql,你可以尝试 INSERT INTO .. ON DUPLICATE KEY

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

相关问题 如何编写PL / SQL以在插入之前先检查记录是否存在 - How to write PL/SQL to check if record exists first, before inserting 如何在插入 sql 表之前检查条目是否存在 - how to check if entry exists before inserting in sql table 如何在将列添加到 PL/SQL 中的现有表之前检查它是否存在? - How to check if a column exists before adding it to an existing table in PL/SQL? 如何在插入时检查mysql数据库列中是否存在值 - how to check whether value exists or not in mysql database column while inserting 在PL / SQL中运行查询之前,如何检查列是否存在以及表行计数是否一致? - How to check If column exists and table row count togather before running the query in PL/SQL? 在插入之前检查是否存在某些东西是不好的做法 - Is it bad practice to check if something exists before inserting 如何编写一个 PL/SQL 代码,在插入之前检查 table_name.column 名称中是否存在值? - How do I write a PL/SQL code that checks if a value exists in table_name.column name before inserting into it? 从xml插入时如何检查记录是否存在的sql - sql for how to check if a record exists when inserting from xml 在插入php sql之前检查表是否存在 - Checking Table exists before inserting php sql 在插入之前检查数据库中是否不存在数据。 SQL、节点、快递服务器 - Check if data not exists in database before inserting it. SQL, NODE, EXPRESS SERVER
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM