D
To achieve what you want, you need to support yourself from a temporary table with the same definition of the fields you need to select (as an example I put as a data type INT to some fields of the temporary table).Then the result of the query will be inserted into the temporary table so that you can then validate the result SI_OV and assess what type of INSERT It will be done.The code would read as follows:DECLARE @resultado TABLE (
SI_Num_Inventario INT
,SI_Ubicacion INT
,SI_Ubicacion_Fisica INT
,SI_Num_Articulo INT
,DatoNulo INT
,SI_Num_Conteo INT
,Fecha DATETIME
,Usuario VARCHAR(32)
,SI_OV INT
)
INSERT INTO @resultado
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
IF (SELECT TOP 1 SI_OV FROM @resultado) IS NULL
BEGIN
INSERT INTO SI_Conteo(SI_Num_Inventario, SI_Ubicacion,SI_Num_Articulo,SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo, SI_Usuario,SI_OV)
SELECT SI_Num_Inventario, SI_Ubicacion,SI_Num_Articulo,SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo, SI_Usuario,SI_OV
FROM @resultado
END
ELSE
BEGIN
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)
SELECT SI_Num_Inventario, SI_Ubicacion_Fisica,SI_Num_Articulo,SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo,SI_Usuario,SI_OV
FROM @resultado
END
Note: the result of INSERT is assuming that the main consultation will always return a result, as if it did not come to bring results or more of a result, the behavior maybe is not the one you expect.