i have this query
SELECT SI_Num_Inventario = COALESCE(t.SI_Num_Inventario, c.SI_Num_Inventario)
,SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion)
,SI_Ubicacion_Fisica = COALESCE(t.SI_Ubicacion_Fisica, c.SI_Ubicacion_Fisica)
,SI_Num_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo)
,NULL
,SI_Num_Conteo = COALESCE(cs.SI_Num_Conteo, 2)
,GETDATE()
,'Admin'
,c.SI_OV
FROM SI_Inventario_Teorico_QAD t
FULL JOIN SI_Conteo c ON t.SI_Articulo = c.SI_Num_Articulo
AND t.SI_Ubicacion = c.SI_Ubicacion
INNER JOIN SI_Maestro_Ref_QAD m ON t.SI_Articulo = m.SI_Num_Articulo
OR c.SI_Num_Articulo = m.SI_Num_Articulo
FULL JOIN SI_Consecutivo cs ON c.SI_Num_Inventario = cs.SI_Num_Inventario
AND cs.SI_Estado = 0
WHERE c.SI_Num_Articulo = 201423
OR t.SI_Articulo = 201423
And I'm trying to tell you that if c.SI_OV IS NULL
I do this INSERT
:
IF c.SI_OV IS NULL
INSERT INTO SI_Conteo(SI_Num_Inventario, SI_Ubicacion,SI_Num_Articulo,SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo, SI_Usuario,SI_OV)
And if it is NULL
not, insert this other one:
ELSE
INSERT INTO SI_Conteo(SI_Num_Inventario, SI_Ubicacion_Fisica,SI_Num_Articulo,SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo,SI_Usuario,SI_OV)
END IF;
Or do it with a CASE WHEN
:
I hope you can help me.
To achieve what you want, you need to rely on a temporary table with the same definition of the fields that you need to select (as an example I put
INT
some fields of the temporary table as data type).Then, the result of the query is going to be inserted into the temporary table so that later you can validate the result of
SI_OV
and evaluate what type ofINSERT
will be performed.The code would look like this:
Note: the result of
INSERT
is assuming that the main query will always return a result, since if it does not return results or more than one result, the behavior may not be what you expect.