简体   繁体   English

如果不存在则插入 ROWS SQL

[英]Insert ROWS if not exists SQL

I want to insert rows that not exist in my table我想插入表中不存在的行

INSERT INTO [Cop].[fact_capacidadOperativa] ([id_empleado],[id_proyecto],[id_rol],[id_categoria],[id_subCategoria],[id_portfolio]
,[id_programa],[horas],[horasPlan],[id_semanaAño],[id_torre])
/*SELECT * FROM [Cop].[timeSheet]*/
SELECT id_empleado,id_proyecto,id_rol,id_categoria,id_subCategoria,
id_portfolio,id_programa,[Cop].[timeSheet].horas,[Cop].[ListaEmpleados].horaPlan,
[Cop].[timeSheet].nroSemana+[Cop].[timeSheet].año AS id_semanaAño,id_torre FROM [Cop].[timeSheet]
JOIN [Cop].[ListaEmpleados] 
ON [Cop].[timeSheet].nombre = [Cop].[ListaEmpleados].recurso
LEFT JOIN [Cop].[ListaProyectos]
ON [Cop].[timeSheet].[proyecto] = [Cop].[ListaProyectos].proyecto
JOIN [Cop].[dim_empleados]
ON [Cop].[timeSheet].nombre = [Cop].[dim_empleados].empleado
LEFT JOIN [Cop].[dim_proyectos]
ON [Cop].[timeSheet].proyecto = [Cop].[dim_proyectos].proyecto
JOIN [Cop].[dim_roles]
ON [Cop].[ListaEmpleados].rol = [Cop].[dim_roles].rol
LEFT JOIN [Cop].[dim_categorias]
ON [Cop].[ListaProyectos].categoria = [Cop].[dim_categorias].categoria
LEFT JOIN [Cop].[dim_subCategorias]
ON [Cop].[ListaProyectos].subcategoria = [Cop].[dim_subCategorias].subCategoria
left JOIN [Cop].[dim_portfolios]
ON [Cop].[ListaProyectos].[portfolio] = [Cop].[dim_portfolios].portfolio
LEFT JOIN [Cop].[dim_programas]
ON [Cop].[ListaProyectos].program = [Cop].[dim_programas].programa
JOIN [Cop].[dim_torres]
ON [Cop].[timeSheet].torre = [Cop].[dim_torres].torre

imagen意象

Insert the values that not exist in [Cop].[fact_capacidadOperativa] , i don t know if i need to use the where.插入[Cop].[fact_capacidadOperativa]中不存在的值,我不知道是否需要使用 where。 this insert into is from one stored procedure that im making此插入来自我正在制作的一个存储过程

I think something like this我想是这样的

INSERT INTO [Cop].[fact_capacidadOperativa] ([id_empleado],[id_proyecto],[id_rol],[id_categoria],[id_subCategoria],[id_portfolio]
,[id_programa],[horas],[horasPlan],[id_semanaAño],[id_torre])
/*SELECT * FROM [Cop].[timeSheet]*/
select * from (SELECT id_empleado,id_proyecto,id_rol,id_categoria,id_subCategoria,
id_portfolio,id_programa,[Cop].[timeSheet].horas,[Cop].[ListaEmpleados].horaPlan,
[Cop].[timeSheet].nroSemana+[Cop].[timeSheet].año AS id_semanaAño,id_torre FROM [Cop].[timeSheet]
JOIN [Cop].[ListaEmpleados] 
ON [Cop].[timeSheet].nombre = [Cop].[ListaEmpleados].recurso
JOIN [Cop].[ListaProyectos]
ON [Cop].[timeSheet].[proyecto] = [Cop].[ListaProyectos].proyecto
JOIN [Cop].[dim_empleados]
ON [Cop].[timeSheet].nombre = [Cop].[dim_empleados].empleado
JOIN [Cop].[dim_proyectos]
ON [Cop].[timeSheet].proyecto = [Cop].[dim_proyectos].proyecto
JOIN [Cop].[dim_roles]
ON [Cop].[ListaEmpleados].rol = [Cop].[dim_roles].rol
JOIN [Cop].[dim_categorias]
ON [Cop].[ListaProyectos].categoria = [Cop].[dim_categorias].categoria
JOIN [Cop].[dim_subCategorias]
ON [Cop].[ListaProyectos].subcategoria = [Cop].[dim_subCategorias].subCategoria
JOIN [Cop].[dim_portfolios]
ON [Cop].[ListaProyectos].[portfolio] = [Cop].[dim_portfolios].portfolio
JOIN [Cop].[dim_programas]
ON [Cop].[ListaProyectos].program = [Cop].[dim_programas].programa
JOIN [Cop].[dim_torres]
ON [Cop].[timeSheet].torre = [Cop].[dim_torres].torre) a
where not exists (select 1 from [Cop].[fact_capacidadOperativa] b where a.id_empleado = b.id_empleado 
                    and a.id_proyecto = b.id_proyecto  and a.id_rol = b.id_rol and a.id_categoria = b.id_categoria
                    and a.id_subCategoria = b.id_subCategoria and a.id_portfolio = b.id_portfolio and a.id_programa = b.id_programa and
                    a.horas = b.horas and a.horaPlan = b.horasPlan and a.id_torre = b.id_torre and a.id_semanaAño = b.id_semanaAño);

But i don t know it s the best way但我不知道这是最好的方法

As mentioned in the comment, we can create a UNIQUE constraint on all affected columns.如评论中所述,我们可以在所有受影响的列上创建UNIQUE约束。

Then we use INSERT IGNORE to only insert valid rows in our target table from the other table(s).然后我们使用INSERT IGNORE仅将其他表中的有效行插入到我们的目标表中。

Assume we have two tables, both with column1, column2 and column3 as integers.假设我们有两个表,column1、column2 和 column3 都是整数。 Now we want to insert all data from one of these tables to the other, but apply a unique constraint on column1 and column2 of the target table.现在我们要将其中一个表中的所有数据插入到另一个表中,但对目标表的 column1 和 column2 应用唯一约束。

This will create the unique constraint:这将创建唯一约束:

ALTER TABLE yourtable 
ADD UNIQUE (column1, column2);

Then this insert commands will only insert valid data in the table which does not violate the unqiue constraint:然后这个插入命令只会在不违反unqiue约束的表中插入有效数据:

INSERT IGNORE INTO yourtable 
  (SELECT column1, column2, column3 FROM anothertable);

I created a sample fiddle which shows the complete behaviour with and without this constraint and with or without the usage of INSERT IGNORE .我创建了一个示例小提琴,它显示了使用和不使用此约束以及使用或不使用INSERT IGNORE的完整行为。

So replicate this idea here: db<>fiddle所以在这里复制这个想法: db<>fiddle

Here is the documentation of INSERT IGNORE : documentation这是INSERT IGNORE的文档:文档

All you need to do is to create the correct unique constraint for your current situation and to make sure your insert command is basically correct.您需要做的就是为您当前的情况创建正确的唯一约束,并确保您的插入命令基本正确。 Then only valid rows will be inserted, as shown in the fiddle.然后只插入有效的行,如小提琴所示。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM