select concat('update tbl_notas_faturamento_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 41 , ', @cd_lancamento = ', a.cd_lancamento , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_notas_faturamento_itens a join tbl_notas_faturamento b on a.cd_lancamento = b.cd_lancamento where cd_almoxarifado is null union all select concat('update tbl_notas_emitidas_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 2 , ', @cd_lancamento = ', a.cd_nota , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_notas_emitidas_itens a join tbl_notas_emitidas b on a.cd_nota = b.cd_nota join tbl_tipo_documentos t on b.cd_tipodocumento = t.cd_tipodocumento where t.cd_operacao = 1 and cd_almoxarifado is null union all select concat('update tbl_notas_emitidas_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' where cd_id = ', cd_id, ';' , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 43 , ', @cd_lancamento = ', a.cd_nota , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_notas_emitidas_itens a join tbl_notas_emitidas b on a.cd_nota = b.cd_nota join tbl_tipo_documentos t on b.cd_tipodocumento = t.cd_tipodocumento where t.cd_operacao = 2 and cd_almoxarifado is null union all select concat('update tbl_compras_notafiscal_entrada_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 1 , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_compras_notafiscal_entrada_itens a join tbl_compras_notafiscal_entrada b on a.cd_entrada = b.cd_entrada where cd_almoxarifado is null union all select concat('update tbl_requisicao_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 42 , ', @cd_lancamento = ', a.cd_requisicao , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_requisicao_itens a join tbl_requisicao b on a.cd_requisicao = b.cd_requisicao where b.cd_operacao = 1 and cd_almoxarifado is null union all select concat('update tbl_requisicao_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 50 , ', @cd_lancamento = ', a.cd_requisicao , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_requisicao_itens a join tbl_requisicao b on a.cd_requisicao = b.cd_requisicao where b.cd_operacao = 2 and cd_almoxarifado is null union all select concat('update tbl_materiais_contagem_estoque_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 100 , ', @cd_lancamento = ', a.cd_lancamento , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_lancamento, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_materiais_contagem_estoque_itens a join tbl_materiais_contagem_estoque b on a.cd_lancamento = b.cd_lancamento where cd_almoxarifado is null union all select concat('update tbl_orcamentos_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' where cd_id = ', cd_id, '; -- ', cd_filial) script from tbl_orcamentos_itens a join tbl_orcamentos b on a.cd_orcamento = b.cd_orcamento where cd_almoxarifado is null union all select concat('update tbl_ordem_servico_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' where cd_id = ', cd_id, '; -- ', cd_filial) script from tbl_ordem_servico_itens a join tbl_ordem_servico b on a.cd_ordem = b.cd_ordem where cd_almoxarifado is null union all select concat('update tbl_pedidos_itens set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' where cd_id = ', cd_id, '; -- ', cd_filial) script from tbl_pedidos_itens a join tbl_pedidos b on a.cd_pedido = b.cd_pedido where cd_almoxarifado is null union all select concat('update tbl_ordem_producao_consumo set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 46 , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_entrada, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_ordem_producao_consumo a join tbl_ordem_producao b on a.cd_entrada = b.cd_entrada where cd_almoxarifado is null union all select concat('update tbl_ordem_producao_produto_produzido set cd_almoxarifado = ', case when cd_filial = 1 then 1 when cd_filial = 4 then 3 else 1 end , ' exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', cd_filial , ', @cd_origem = ', 4 , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', cd_id , ', @cd_material = ', cd_material , ', @dt_kardex = ', '''', format(dt_entrada, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', cd_filial ) script from tbl_ordem_producao_produto_produzido a join tbl_ordem_producao b on a.cd_entrada = b.cd_entrada where cd_almoxarifado is null