How to convert a function to view

Asked

Viewed 63 times

0

Good afternoon,

I have the following function:

if object_id('dbo.batidas', 'TF') is null
begin
    exec('create function dbo.batidas() returns @retorno table(T int) as begin return end');
end;
go

alter function dbo.batidas(@data_inicio date,
                           @data_final date)
returns @retorno table(chapa varchar(100),
    nome varchar(100),
    filial varchar(100),
    secao varchar(100),
    situacao varchar(100),
    [data 1] date,
    [data 2] date,
    [data 3] date,
    ocorrencia varchar(100))
as
begin
    declare @batidas table(filial varchar(100),
        coligada varchar(100),
        chapa varchar(100),
        secao varchar(100),
        situacao varchar(100),
        nome varchar(100),
        data datetime,
        dia varchar(100),
        batida int);

    insert into @batidas
        (filial,
        coligada,
        chapa,
        secao,
        situacao,
        nome,
        data,
        dia,
        batida)
    select f.filial,
        f.coligada,
        f.chapa,
        f.secao,
        f.situacao,
        f.nome,
        v.data,
        datename(dw,v.data),
        max(v.sequencialbatida)
    from arelbatidatransitoview as v
        left join vwfunc  as f on v.chapa = f.chapa
    where datepart(dw,v.data) = 1 -- domingo
        and batida is not null
        and v.data between @data_inicio and @data_final
        and v.codcoligada = 1
    group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

    insert into @retorno
        (chapa,
        nome,
        filial,
        secao,
        situacao,
        [data 1],
        [data 2],
        [data 3],
        ocorrencia)
    select t1.chapa,
        t1.nome,
        t1.filial,
        t1.secao,
        t1.situacao,
        cast(t1.data as date),
        cast(t2.data as date),
        cast(t3.data as date),
        '04 - TESTE DOMINGO'
    from @batidas as t1
        inner join @batidas as t2 on t2.chapa = t1.chapa
        inner join @batidas as t3 on t3.chapa = t1.chapa
    where t2.data = dateadd(day, + 7, t1.data)
        and t3.data = dateadd(day, + 14, t1.data);

    return;
end;
go

I search it with the following query:

SELECT 
    bat.CHAPA,
    LEFT(bat.NOME,25)  AS  NOME,
    bat.FILIAL,
    bat.SECAO,
    bat.NSECAO,
    bat.SITUACAO,
    bat.[DATA 1],
    bat.[DATA 2],
    bat.[DATA 3],
    bat.OCORRENCIA
        FROM dbo.batidas(@DTINICIO, @DTFINAL) bat
        WHERE bat.FILIAL = @LOJA
        ORDER BY bat.FILIAL,bat.NSECAO,bat.NOME;

What I would like, turn the query into a view so I could make a Join with other views I have to display in a panel.

Only today I can not because the function needs vestments, and so does not accept, someone could give me a tip how to do ?

See the message when I try to create a query view:

With the parameters in the query:

Msg 137, Nível 15, Estado 2, Procedimento VIEW_MAIS_DOIS_DOMINGOS_SEM_FOLGA, Linha 13 [Linha de Início do Lote 11]
Must declare the scalar variable "@DTINICIO".

No parameters in query:

Msg 216, Nível 16, Estado 1, Procedimento VIEW_MAIS_DOIS_DOMINGOS_SEM_FOLGA, Linha 13 [Linha de Início do Lote 11]
Parameters were not supplied for the function 'dbo.batidas'.

1 answer

0

Views do not accept parameters, what you can do in this case is a new function with all his JOIN or call your function in their consultations.

Browser other questions tagged

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