Esta es la idea:
Código SQL:
Ver originalDECLARE @reg INT
SELECT h.hora AS hora1,h.marca,h.target, h.fecha AS hfecha,p.hora,p.peso,p.fecha,o.linea
FROM optimas AS o
INNER JOIN pesos AS p ON o.codigo = p.codoptima
INNER JOIN htargets AS h ON o.codigo = h.codoptima
WHERE o.linea = @linea
AND p.fecha >= @fecha AND h.fecha >= @fecha
AND p.hora = @hora AND h.hora = @hora
ORDER BY fecha
SET @reg = @@rowcount
SELECT top 1 @reg AS 'RowsReturned'