0
I have the following function in SQL Server that returns the name of each column of my table:
CREATE FUNCTION dbo.FiltraNomeColunas(@FiltroColuna VARCHAR(200))
RETURNS VARCHAR
AS
BEGIN
DECLARE @Retorno VARCHAR(200);
SET @Retorno = (
SELECT c.name as NomeColuna
FROM sys.tables as t
INNER JOIN sys.columns c on t.object_id = c.object_id
WHERE c.name LIKE @FiltroColuna
);
RETURN @Retorno
END
I need to select the columns returned by this function.
I’m trying to do it this way:
SELECT
table_c1_di.E3TimeStamp AS Data_Hora,
dbo.FiltraNomeColunas('%DISJ%')
FROM table_c1_di
JOIN c1_do.dbo.table_c1_do ON CAST(table_c1_do.E3TimeStamp AS DATETIME2(0)) = CAST(table_c1_di.[E3TimeStamp] AS DATETIME2(0))
WHERE (table_c1_do.E3TimeStamp >= @DataInicial AND table_c1_do.E3TimeStamp <= @DataFinal)
ORDER BY table_c1_do.E3TimeStamp DESC
However, the way I’m pulling this presenting error because the function returns more than one result.
I would like to know some way I can be doing/searching this function so that it returns more values, and as these return values are the column name of this table, the main select completes the query based on the return values.
In your case it would be better to use a
stored procedurein place of afunction. Onefunctionin its essence, it must return a single value (see examples such asGETDATE()andLOWER()). As you need to return more than one value, use astored procedure, that can return the result of aselectwith many values.– Ricardo Pontual
Another solution would be to declare
functionreturning atable, or Table-Valued return.– Ricardo Pontual