How to take all values of a select and run in a Procedure?

Asked

Viewed 257 times

0

How to take all the values of a select and play in a precedent to give an Insert without running one value at a time?

I have the following sql code:

   --drop table #tabelaEntSaida;

   --drop table #tabelaEntSaida;

 CREATE TABLE #tabelaEntSaida (
  EmpCod varchar (30),
  FiscalNFDataEmissao datetime,
  FiscalNFDataSaiEnt datetime,
  FiscalNFEspec varchar(30), 
  NatOperacao varchar(30),
  FiscalNFSerie varchar(5), 
  FiscalNFnum varchar(50), 
  PaisSigla varchar(3), 
  UfSigla varchar(2), 
  EntCod varchar(30), 
  UsuCod varchar(30)
)

DECLARE 
@EmpCod varchar(30), 
@FiscalNFDataEmissao datetime,
@FiscalNFDataSaiEnt datetime,
@FiscalNFEspec varchar(30), 
@NatOperacao varchar(50),
@FiscalNFSerie varchar(5), 
@FiscalNFnum varchar(50), 
@PaisSigla varchar(3), 
@UfSigla varchar(2), 
@EntCod varchar(30), 
@USUARIO varchar(30),
@MSG varchar(50),
@Contador varchar(30)

set @MSG = ''


 DECLARE cursor_gera_entrada_saida CURSOR FOR

 SELECT DISTINCT
    NF.EmpCod, 
    NF.FiscalNFDataEmis,
    NF.FiscalNFDataSaiEnt, 
    NF.FiscalNFEspec, 
    ITNF.NatOpCodEstr,
    NF.FiscalNFSerie, 
    NF.FiscalNFnum, 
    NF.PaisSigla, 
    NF.UfSigla, 
    NF.EntCod,
    NF.UsuCod
FROM 
FISCAL_NF NF
LEFT JOIN FISCAL_ITEM_NF ITNF ON ITNF.FiscalNFChv = NF.FiscalNFChv
WHERE NF.FiscalNFEspec = 'NFC-E' and NF.EmpCod = '01.01'


 OPEN cursor_gera_entrada_saida


 FETCH NEXT FROM cursor_gera_entrada_saida INTO 

@EmpCod, 
@FiscalNFDataEmissao,
@FiscalNFDataSaiEnt,
@FiscalNFEspec, 
@NatOperacao,
@FiscalNFSerie, 
@FiscalNFNum, 
@PaisSigla, 
@UfSigla, 
@EntCod, 
@USUARIO


WHILE @@FETCH_STATUS = 0
BEGIN

/* inserindo na tabela temporaria */
INSERT INTO #tabelaEntSaida (
  EmpCod,
  FiscalNFDataEmissao,
  FiscalNFDataSaiEnt,
  FiscalNFEspec, 
  NatOperacao,
  FiscalNFSerie, 
  FiscalNFnum, 
  PaisSigla, 
  UfSigla, 
  EntCod, 
  UsuCod
) values (
    @EmpCod, 
    @FiscalNFDataEmissao, 
    @FiscalNFDataSaiEnt,
    @FiscalNFEspec, 
    @NatOperacao,
    @FiscalNFSerie, 
    @FiscalNFNum, 
    @PaisSigla, 
    @UfSigla, 
    @EntCod, 
    @USUARIO
)

/* insere na proceure */
EXECUTE GERA_LIVRO_ENT_bkp 
    @EmpCod,
    @FiscalNFDataEmissao,
    @FiscalNFDataSaiEnt,
    @NatOperacao,
    @FiscalNFEspec,
    @FiscalNFSerie,
    @FiscalNFNum,
    @FiscalNFNum,
    @UfSigla,
    @PaisSigla,
    @EntCod,
    '',
    @USUARIO,
    @MSG

-- Lendo a próxima linha
FETCH NEXT FROM cursor_gera_entrada_saida INTO 
    @EmpCod, 
    @FiscalNFDataEmissao,
    @FiscalNFDataSaiEnt,
    @FiscalNFEspec, 
    @NatOperacao,
    @FiscalNFSerie, 
    @FiscalNFnum, 
    @PaisSigla, 
    @UfSigla, 
    @EntCod, 
    @USUARIO
  END

 select * from #tabelaEntSaida;

 drop table #tabelaEntSaida;

 -- Fechando Cursor para leitura
CLOSE cursor_gera_entrada_saida

-- Desalocando o cursor
DEALLOCATE cursor_gera_entrada_saida

Image of the returned select. I want you to play all these values in the past: inserir a descrição da imagem aqui

1 answer

2

Until a while ago I sent several records to a trial as follows: converting the result of select for XML, the precedent expected as a parameter a VARCHAR(7999) and was used the OPENXML to read the XML and insert the records into a temporary table. Only I found a more elegant way to do this: using Table-Valued Parameters

Taking your example code, I created the table FISCAL_NF_BKP to enter all incoming records in the process.

  1. Set a Type as Table and create the precedent by receiving the type created as parameter.
CREATE TYPE RegistrosNotaFiscal 
      AS TABLE
         (EmpCod VARCHAR (30),
         FiscalNFDataEmissao DATETIME,
         FiscalNFDataSaiEnt DATETIME,
         FiscalNFEspec VARCHAR(30), 
         NatOperacao VARCHAR(30),
         FiscalNFSerie VARCHAR(5), 
         FiscalNFnum VARCHAR(50), 
         PaisSigla VARCHAR(3), 
         UfSigla VARCHAR(2), 
         EntCod VARCHAR(30), 
         UsuCod VARCHAR(30))
   GO

Criando um tipo tabela

     CREATE PROCEDURE dbo.GERA_LIVRO_ENT_bkp (@EntradaSaida RegistrosNotaFiscal READONLY)

    AS

          SET NOCOUNT ON

         -- CONTEÚDO DA SUA PROCEDURE ...

         INSERT INTO FISCAL_NF_BKP
         SELECT * FROM @EntradaSaida
    GO

procedure

  1. After that, you can declare a table type variable created previously, insert records and call the past.


    DECLARE @TabelaEntradaSaida AS RegistrosNotaFiscal

    INSERT INTO @TabelaEntradaSaida
    SELECT DISTINCT
        NF.EmpCod, 
        NF.FiscalNFDataEmissao,
        NF.FiscalNFDataSaiEnt, 
        NF.FiscalNFEspec, 
        ITNF.NatOpCodEstr,
        NF.FiscalNFSerie, 
        NF.FiscalNFnum, 
        NF.PaisSigla, 
        NF.UfSigla, 
        NF.EntCod,
        NF.UsuCod 
    FROM FISCAL_NF NF
    LEFT JOIN FISCAL_ITEM_NF ITNF ON ITNF.FiscalNFChv = NF.FiscalNFChv
    WHERE NF.FiscalNFEspec = 'NFC-E' and NF.EmpCod = '01.01'


    SELECT * FROM @TabelaEntradaSaida


    EXEC GERA_LIVRO_ENT_bkp @TabelaEntradaSaida;

Tabela FISCAL_NF_BKP antes de executar a procedure

Tabela FISCAL_NF_BKP DEPOIS de executar a procedure

If you’re going to do it in C# you can see in this reference.

  • It didn’t work. He didn’t write the values. How do I list them in the corresponding variables of the past? ALTER PROCEDURE dbo.Gera_livro_ent_bkp (@Entradasaida Registrosnotafiscal1 READONLY) AS SET NOCOUNT ON DECLARE @EMPCOD VARCHAR(20), etc

  • But has Procedure received all the records in the @Input variable!? What you didn’t record were the records of the @Input variable left in another table that you are trying to do the insert in the past, is that it?? To get all the records and go playing on variables inside the past, you would have to make one while or a cursos, which is what you were already doing. But then it doesn’t make sense for you to pass all the records by parameter and then do a loop.

  • 1

    That’s right, Raquel. TVP is the best way to pass table as parameter. Already won +1logo in the initial paragraph.

Browser other questions tagged

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