Store Procedure with SELECT and with data make an INSERT

Asked

Viewed 41 times

0

Good morning

I need to do a Store Procedure where I need to do an Insert but I need to pull data from another table to include this Insert.

Example:

'''CREATE PROCEDURE SP_Corrige_Parcelas(@idPagamentoVenda int,@qtParcelas int,@prTaxaAdministracao numeric(9,2) 

)

AS

BEGIN DECLARE @cont int DECLARE @idEmpresa int DECLARE @dtEmissao Date DECLARE @dtvenci Date DECLARE @vlParcela Numeric(9,2)

set @cont = 1

SELECT card.tbPagamentoVenda.idEmpresa, card.tbPagamentoVenda.dtEmissao , card.tbPagamentoVenda.vlPagamento FROM card.tbPagamentoVenda WHERE card.tbPagamentoVenda.idFormaPackage = @idPagamentoVenda

WHILE @cont < @qtParcelas BEGIN

 INSERT INTO card.tbParcela(idPagamentoVenda, 
                            nrParcela,
                            idEmpresa,
                            dtEmissao,
                            dtVencimento,
                            vlParcela,
                            vlTaxaAdministracao,
                            dtPagamento,
                            vlPago,
                            idStatusParcela,
                            idMovimentoBanco) 
                            VALUES(@idPagamentoVenda,
                            @cont,
                            card.tbPagamentoVenda.idEmpresa,
                            card.tbPagamentoVenda.dtEmissao,
                            DATEADD(MONTH,@cont,card.tbPagamentoVenda.dtEmissao),
                            card.tbPagamentoVenda.vlPagamento / @vlParcela,
                            (card.tbPagamentoVenda.vlPagamento * @prTaxaAdministracao)/100,
                            NULL,
                            NULL,
                            NULL,
                            NULL)                    


SET @cont = @cont + 1   
END;

END;'''

Thanks this already.

1 answer

0

You need to use the SELECT INTO, for example:

SELECT dot.*
INTO tabela_final
FROM dados_outra_tabela dot

Organize the data of SELECT equal to the data in the final table (in which case it is the table that will be inserted the result of the SELECT.

  • I forgot to comment, but I have to merge with external data too.

  • Use the UNION ALL

Browser other questions tagged

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