
CREATE TABLE #CE_PRODUTOS_TRIB(
	[COD_EMP] [int] NOT NULL,
	[COD_PROD] [int] NOT NULL,
	[UF] [char](2) NOT NULL,
	[CFOP] [char](5) NOT NULL,
	[ALIQ_ICMS] [numeric](9, 2) NOT NULL,
	[RED_BC_ICMS] [numeric](9, 2) NULL,
	[COD_FISCAL] [int] NULL,
	[ST_PERC_MVA] [numeric](9, 2) NULL,
	[ST_ALIQ_ICMS_INTERNA] [numeric](9, 2) NULL,
	[IVA_ST] [numeric](9, 2) NULL,
	[ST_RED_BC_ICMS_INTERNA] [numeric](9, 2) NULL,
	[ALIQ_PIS] [numeric](9, 2) NULL,
	[ALIQ_COFINS] [numeric](9, 2) NULL,
	[CST_PIS] [char](2) NULL,
	[CST_COFINS] [char](2) NULL,
	[ALIQ_IPI] [numeric](9, 2) NULL,
	[CST_IPI] [char](2) NULL,
	[COD_ENQUAD_IPI] [char](3) NULL,
	[TRIB_LEI_TRANSP_NFE] [numeric](15, 2) NULL,
	[UNID_TRIB] [varchar](5) NULL,
	[PROCODBAR_TRIB] [varchar](25) NULL,
	[FATOR_QTD_TRIB] [numeric](15, 3) NULL,
	[MOD_BC_ICMS] [char](1) NULL,
	[MOD_BC_ICMS_ST] [char](1) NULL,
	[VLR_PAUTA_IPI] [numeric](15, 4) NULL,
	[ORIGEM_MERC] [char](1) NULL,
	[PERC_DIFER_ICMS] [numeric](9, 2) NULL,
	[ALIQ_ICMS_INTERNA] [numeric](9, 4) NULL,
	[PERC_FCP] [numeric](9, 4) NULL,
	[ICMS_SEQ_IMPOSTO] [varchar](50) NULL,
	[ICMS_CONTROLE_UP] [datetime] NULL,
	[PIS_SEQ_IMPOSTO] [varchar](50) NULL,
	[PIS_CONTROLE_UP] [datetime] NULL,
	[COFINS_SEQ_IMPOSTO] [varchar](50) NULL,
	[COFINS_CONTROLE_UP] [datetime] NULL,
	[IPI_SEQ_IMPOSTO] [varchar](50) NULL,
	[IPI_CONTROLE_UP] [datetime] NULL,
	[VLR_PAUTA_ICMS_INTERNA] [numeric](15, 4) NULL,
	[VLR_PAUTA_ICMS_ST] [numeric](15, 4) NULL,
	[COD_CONTA_CONTABIL] [varchar](60) NULL,
	[ALIQ_ICMS_EFETIVO] [numeric](9, 4) NULL,
	[RED_BC_ICMS_EFETIVO] [numeric](9, 4) NULL,
	[CARGA_MEDIA_ST] [numeric](9, 2) NULL,
	[ALIQ_ICMS_DESONERADO] [numeric](9, 2) NULL,
	[MOTIVO_DESONERACAO] [int] NULL,
	[ENVIA_COD_BENEFICIO_FISCAL] [char](1) NOT NULL,
	[COD_BENEFICIO_FISCAL] [varchar](10) NULL,
	[TIPO_CALCULO] [char](1) NOT NULL)
	GO
INSERT INTO #CE_PRODUTOS_TRIB
(COD_EMP,
COD_PROD,
UF,
CFOP,
ALIQ_ICMS,
RED_BC_ICMS,
COD_FISCAL,
ST_PERC_MVA,
ST_ALIQ_ICMS_INTERNA,
IVA_ST,
ST_RED_BC_ICMS_INTERNA,
ALIQ_PIS,
ALIQ_COFINS,
CST_PIS,
CST_COFINS,
ALIQ_IPI,
CST_IPI,
COD_ENQUAD_IPI,
TRIB_LEI_TRANSP_NFE,
UNID_TRIB,
PROCODBAR_TRIB,
FATOR_QTD_TRIB,
MOD_BC_ICMS,
MOD_BC_ICMS_ST,
VLR_PAUTA_IPI,
ORIGEM_MERC,
PERC_DIFER_ICMS,
ALIQ_ICMS_INTERNA,
PERC_FCP,
ICMS_SEQ_IMPOSTO,
ICMS_CONTROLE_UP,
PIS_SEQ_IMPOSTO,
PIS_CONTROLE_UP,
COFINS_SEQ_IMPOSTO,
COFINS_CONTROLE_UP,
IPI_SEQ_IMPOSTO,
IPI_CONTROLE_UP,
VLR_PAUTA_ICMS_INTERNA,
VLR_PAUTA_ICMS_ST,
COD_CONTA_CONTABIL,
ALIQ_ICMS_EFETIVO,
RED_BC_ICMS_EFETIVO,
CARGA_MEDIA_ST,
ALIQ_ICMS_DESONERADO,
MOTIVO_DESONERACAO,
ENVIA_COD_BENEFICIO_FISCAL,
COD_BENEFICIO_FISCAL,
TIPO_CALCULO)
SELECT 
1, --COD_EMP,
PRO_COD, --COD_PROD,
'MG', --UF,
6102, --CFOP,
CASE
     WHEN CODTABALIQUOTAS = 1 AND COD_CST_ICMS = '00' THEN 4
     WHEN CODTABALIQUOTAS = 2 AND COD_CST_ICMS = '00' THEN 12
     WHEN CODTABALIQUOTAS = 3 AND COD_CST_ICMS = '00' THEN 18
     WHEN CODTABALIQUOTAS = 5 AND COD_CST_ICMS = '00' THEN 4
END, --ALIQ_ICMS,
0, --RED_BC_ICMS,
CASE
    WHEN 
END, --COD_FISCAL,
NULL, --ST_PERC_MVA,
NULL, --ST_ALIQ_ICMS_INTERNA,
NULL, --IVA_ST,
NULL, --ST_RED_BC_ICMS_INTERNA,
0, --ALIQ_PIS,
0, --ALIQ_COFINS,
'06', --CST_PIS,
'06', --CST_COFINS,
0, --ALIQ_IPI,
'53', --CST_IPI,
NULL, --COD_ENQUAD_IPI,
NULL, --TRIB_LEI_TRANSP_NFE,
NULL, --VUNID_TRIB,
NULL, --PROCODBAR_TRIB,
NULL, --VFATOR_QTD_TRIB,
NULL, --MOD_BC_ICMS,
NULL, --MOD_BC_ICMS_ST,
NULL, --VLR_PAUTA_IPI,
NULL, --ORIGEM_MERC,
NULL, --PERC_DIFER_ICMS,
NULL, --ALIQ_ICMS_INTERNA,
NULL, --PERC_FCP,
NULL, --ICMS_SEQ_IMPOSTO,
NULL, --ICMS_CONTROLE_UP,
NULL, --PIS_SEQ_IMPOSTO,
NULL, --PIS_CONTROLE_UP,
NULL, --COFINS_SEQ_IMPOSTO,
NULL, --COFINS_CONTROLE_UP,
NULL, --IPI_SEQ_IMPOSTO,
NULL, --IPI_CONTROLE_UP,
NULL, --VLR_PAUTA_ICMS_INTERNA,
NULL, --VLR_PAUTA_ICMS_ST,
NULL, --COD_CONTA_CONTABIL,
NULL, --ALIQ_ICMS_EFETIVO,
NULL, --RED_BC_ICMS_EFETIVO,
NULL, --CARGA_MEDIA_ST
NULL, --ALIQ_ICMS_DESONERADO
0, --MOTIVO_DESONERACAO
'S', --ENVIA_COD_BENEFICIO_FISCAL
NULL, --COD_BENEFICIO_FISCAL
'F' --TIPO_CALCULO
FROM PRODUTOS_NATOPERA
WHERE UF_SIGLA = 'MG' AND CONTRIB_ICMS = 'S' AND COD_CRT = 3 AND CLI_CONS_FINAL = 'N'

SELECT * FROM PRODUTOS_NATOPERA

SELECT * FROM PRODUTOS_NATOPERA
WHERE UF_SIGLA = 'MG' AND CONTRIB_ICMS = 'S' AND COD_CRT = 3 AND CLI_CONS_FINAL = 'N'



SELECT * FROM NATOPERA
WHERE NOP_COD IN(6000,6001,6403,6400)

SELECT * FROM ALIQ_ICMS
WHERE CODTABALIQUOTA IN(1,2,3,5) AND EST_SIGLA = 'MG' AND ENTR_SAIDA = 'S' AND COD_CST_ICMS IN('00','10')