How can I delete rows which meet several conditions from a table without primary key? (actually the primary key is formed by every column)
After so many tries I have a code that first creates a temporary table where it inserts the rows I want to delete and finally I try to delete the rows from original table which meet the conditions comparing to temporary table.
But it gives me the error about cannot linking the several parts identifier #temp.idCarga
Maybe it’s easy but I have been trying so much time and I cannot focus properly.
CREATE TABLE #TEMP
(
CC int,
idCarga int,
Tipo nvarchar(50),
Importe float,
Bloque nvarchar(50),
idsistema int
)
INSERT INTO #TEMP(CC,idCarga,Importe,Bloque,Tipo,IdSistema)
select distinct CI.CC,CI.idCarga,CI.Importe,CI.Bloque,CI.Tipo,CI.idSistema
from CONT_INGRESOS_InformeMayor CI
INNER JOIN
(
SELECT idCarga,CC,Bloque,Importe,TIpo
FROM CONT_INGRESOS_InformeMayor
) CI2
ON CI.CC = CI2.CC
AND CI.Bloque = CI2.Bloque
AND CI.TIpo <> CI2.Tipo
WHERE CI.idCarga = @idCarga
AND CI2.idCarga = @idCarga
AND CI.Importe = 0
DELETE
FROM CONT_INGRESOS_InformeMayor
WHERE idCarga = #temp.idCarga
AND tipo = #temp.tipo
AND importe = #temp.importe
and bloque = #temp.bloque
You missed a join on your
DELETE. Try this: