select
'18' as IDENTIFICADOR,
cliparcod as codigo,
parcnpjcpf as cpfCNPJ,
cli_plano_pagamento as codCondicaoPagamento,
(select clitctccod from clitc where clitccliparcod=cliparcod order by clitcseq limit 1) as codFormaPagamento,
null as codRegiao,
clircaparcod as codVendedor,
clilplpcod as codListaPreco,
parmuncod as codCidade,
clilpempparcod as codFilial,
pardes as nomeRazao,
parfan as nomeFantasia,
case when length(parcnpjcpf)>11 then 'J' else 'F' end as tipoPessoa,
null as intervaloVisitas,
parierg as rgIE,
parfone as telefone1,
parfax as telefone2,
paremail as emailComprador,
paremail as emailNfe,
paremail as emailFinanceiro,
null as contato,
parrua as endereco,
parbai as bairro,
parcep as cep,
munufsigla as uf,
null as observacao,
1 as ativo,
case when clibloq = 'S' then 1 else 0 end as bloqueado,
null as numero,
null as complemento,
null as latitude,
null as longitude,
null as bloqueiaFilialUnidade,
null as bloqueiaListaPreco,
null as bloqueiaCondPagamento,
null as bloqueiaFormaPagamento,
null as inscricaoEstadualPessoaFisica,
cliparcod as idCliente,
null as codTipoClienteTributario,
null as vendedoresRelacionados,
null as prospecto,
null as idAlteracaoERP,
null as percAcordoComercial,
null as codTransportadora,
null as codRedespacho,
null as dataNascimento,
null as segmentosRelacionados,
null as cro,
null as percentualDescontoMaximo,
null as codFaixaFrete,
null as codGrupoEconomico,
null as listaPrecosAdicionais,
null as checkinRaio,
null as checkinDias,
(select
array_to_string(array (select plpgcod
from (          
select plpgcod 
from plpg 
     join cli on cliparcod = cliparcod and plpgpzmedio <= clipz  
where plpgdisp='S' and (not plpgtipo in ('E'))
union 
select plpgcod 
from plpg 
     join cliplpg on cliplpgplpgcod = plpgcod and cliplpgcliparcod = cliparcod
) as t), ',')) as condicoesAdicionais,
null as observacaoIntegracao,
clilim as limiteCredito,--f_calc_clilimdisp(1, cliparcod) as limiteCredito,
null as percMargemContribuicao,
null as VendedoresPrepostos,
null as codHierarquiaLiberacaoPedido,
null as codTipoCliente,
null as codDivisaoVenda,
null as codTipoPedido,
null as bloqueiaTipoPedido,
null as tipoPedidoAdicionais 
from par
join cli on cliparcod = parcod
join clilp on cliparcod = clilpcliparcod  
join mun on parmuncod = muncod
where parsit = 'A'
and clilpempparcod in (select parcod from par join emp on empparcod = parcod where parsit = 'A')
and clircaparcod in  (select parcod from par join rca on rcaparcod = parcod where parsit = 'A') 
order by cliparcod
ExecutarScriptSempre
