[英]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 EXISTS
或CASE
逻辑以检查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.