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:
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
– Felipe Michael da Fonseca
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 onewhile
or acursos
, 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 aloop
.– Raquel Pinheiro
That’s right, Raquel. TVP is the best way to pass table as parameter. Already won +1logo in the initial paragraph.
– José Diz