Create dynamic table of the result of an exec return with Sql server variable

Asked

Viewed 456 times

0

Good morning, I’m having difficulty to do in sql server 2014.

I need the result back exec (@query) create a dynamic table with this result filling in with columns and rows. Query I’m using to mount:

DECLARE @COLS VARCHAR(MAX)
        ,@QUERY VARCHAR(MAX)

SET @COLS = STUFF((
    SELECT  ',' + QUOTENAME(A.MOTIVO)
    FROM #TMP_GERAR A
        group by A.MOTIVO
    --  ORDER BY A.MOTIVO ASC
        FOR XML PATH('')),1,1, '');

SELECT @COLS

SET @QUERY = 'SELECT * FROM
            (
                SELECT REPRESENTANTE, 
                HORARIO,
                motivo
                FROM #TMP_GERAR 

            ) AS EX
                PIVOT
                (
                    MAX(HORARIO)
                    FOR motivo IN ('+@COLS+')
                )AS X'

-- aqui que gostaria de ter um uma tabela criada 
--dinamicamente para receber o resultado desta variavel 
EXECUTE ( @QUERY) 

inserir a descrição da imagem aqui

Goal is to create an HTML email with the table created, this is the only way I know so far.

Thanks in advance

  • Do you say create the email and send by SQL Server itself? I don’t understand what’s dynamic in this story

  • Hello, this is a comment for my goal after being able to create the dynamic table, if it is the case do not need to read this goal which is just an observation.

  • Where do you want to read this table?

  • read in a table in the email would that be it? not understood just wanted to do type this example nor works select into ##table from @query, I just can’t get the names of columns pq do not know which or how many have seen.

1 answer

0

Place the following command inside the @QUERY string:

SELECT * INTO [TABELANOVA] FROM (SEU SELECT)A

Browser other questions tagged

You are not signed in. Login or sign up in order to post.