
/****** Object:  UserDefinedFunction [dbo].[f_str_tecnicosOT]    Script Date: 07/04/2015 15:36:03 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[f_str_OTsReporte]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.f_str_OTsReporte
GO

CREATE FUNCTION dbo.f_str_OTsReporte(
	@id_reporte int
	
) RETURNS NVARCHAR(300)
AS
BEGIN

DECLARE @OTS NVARCHAR(700)
SET @OTS = ''

SELECT   @OTS = @OTS + OT.prefijo+ '-' + cast(OT.documento_numero as varchar(10))+ ' ' 
from ort_programacion inner join
	[doc_documento] OT on OT.id =  ort_programacion.[id_documento.ot] 
where ort_programacion.active=1
	and	ort_programacion.[id_reporteTecnico]=@id_reporte 
	and ort_programacion.id_tipoProgramacion=6 
group by OT.prefijo, OT.documento_numero 

SET @OTS = RTRIM(@OTS)
RETURN @OTS 

END

GO

update ort_reporteTecnico
set reporteTecnico=dbo.f_str_OTsReporte(ort_reporteTecnico.id)
where active=1


