I need this query inside a select statement in another query, i have tried to put it inside a function and call it from the select, but i get errors maybe because non-scalar statements are not allowed in a function. How can i proceed?
SELECT Tip.NombreTipoMov
INTO #tabla_temp
FROM ut_sgt_Movimientos_t Reg
INNER JOIN ut_sgt_TiposMovimientos_m Tip
ON Reg.id_TipoMov = Tip.id_TipoMov
WHERE Reg.id_Registro = @IdRegistro
DECLARE @data VARCHAR(100)
UPDATE #tabla_temp
SET @data = @data + ' ' + NombreTipoMov
SELECT LTRIM(RTRIM(@data))
DROP TABLE #tabla_temp
This is the function:
CREATE FUNCTION dbo.uf_sgt_ObtieneTiposMovimientosXRegistroVehiculo
(
@IdRegistro INTEGER
)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Retorno VARCHAR(100)
SET @Retorno = ( SELECT Tip.NombreTipoMov
INTO #tabla_temp
FROM ut_sgt_Movimientos_t Reg
INNER JOIN ut_sgt_TiposMovimientos_m Tip
ON Reg.id_TipoMov = Tip.id_TipoMov
WHERE Reg.id_Registro = @IdRegistro
DECLARE @data VARCHAR(100)
UPDATE #tabla_temp
SET @data = @data + ' ' + NombreTipoMov
SELECT LTRIM(RTRIM(@data))
DROP TABLE #tabla_temp
)
RETURN @Retorno END GO
This is the error:
Incorrect syntax near the keyword 'INTO'.
Illegal UPDATE statement within a scalar SQL function.
Illegal SELECT statement within a scalar SQL function.
Illegal DROP TABLE statement within a scalar SQL function.
Incorrect syntax near ')'.