SET TERM ^ ;

CREATE OR ALTER procedure SPC_GRADE_GERAL (
    PRODUTO_ID integer)
returns (
    DESCRICAO varchar(100),
    CODIGO integer,
    TOTAL_TAMANHOS integer,
    DATA date,
    STATUS integer,
    STATUSEXCLUSAO integer,
    USUARIO_CODIGO integer,
    T01 varchar(10),
    T02 varchar(10),
    T03 varchar(10),
    T04 varchar(10),
    T05 varchar(10),
    T06 varchar(10),
    T07 varchar(10),
    T08 varchar(10),
    T09 varchar(10),
    T10 varchar(10),
    T11 varchar(10),
    T12 varchar(10),
    T13 varchar(10),
    T14 varchar(10),
    T15 varchar(10),
    T16 varchar(10),
    T17 varchar(10),
    T18 varchar(10),
    T19 varchar(10),
    T20 varchar(10),
    E01 varchar(10),
    E02 varchar(10),
    E03 varchar(10),
    E04 varchar(10),
    E05 varchar(10),
    E06 varchar(10),
    E07 varchar(10),
    E08 varchar(10),
    E09 varchar(10),
    E10 varchar(10),
    E11 varchar(10),
    E12 varchar(10),
    E13 varchar(10),
    E14 varchar(10),
    E15 varchar(10),
    E16 varchar(10),
    E17 varchar(10),
    E18 varchar(10),
    E19 varchar(10),
    E20 varchar(10),
    USA01 varchar(10),
    USA02 varchar(10),
    USA03 varchar(10),
    USA04 varchar(10),
    USA05 varchar(10),
    USA06 varchar(10),
    USA07 varchar(10),
    USA08 varchar(10),
    USA09 varchar(10),
    USA10 varchar(10),
    USA11 varchar(10),
    USA12 varchar(10),
    USA13 varchar(10),
    USA14 varchar(10),
    USA15 varchar(10),
    USA16 varchar(10),
    USA17 varchar(10),
    USA18 varchar(10),
    USA19 varchar(10),
    USA20 varchar(10),
    EUR01 varchar(10),
    EUR02 varchar(10),
    EUR03 varchar(10),
    EUR04 varchar(10),
    EUR05 varchar(10),
    EUR06 varchar(10),
    EUR07 varchar(10),
    EUR08 varchar(10),
    EUR09 varchar(10),
    EUR10 varchar(10),
    EUR11 varchar(10),
    EUR12 varchar(10),
    EUR13 varchar(10),
    EUR14 varchar(10),
    EUR15 varchar(10),
    EUR16 varchar(10),
    EUR17 varchar(10),
    EUR18 varchar(10),
    EUR19 varchar(10),
    EUR20 varchar(10),
    A01 integer,
    A02 integer,
    A03 integer,
    A04 integer,
    A05 integer,
    A06 integer,
    A07 integer,
    A08 integer,
    A09 integer,
    A10 integer,
    A11 integer,
    A12 integer,
    A13 integer,
    A14 integer,
    A15 integer,
    A16 integer,
    A17 integer,
    A18 integer,
    A19 integer,
    A20 integer,
    EAN01 varchar(20),
    EAN02 varchar(20),
    EAN03 varchar(20),
    EAN04 varchar(20),
    EAN05 varchar(20),
    EAN06 varchar(20),
    EAN07 varchar(20),
    EAN08 varchar(20),
    EAN09 varchar(20),
    EAN10 varchar(20),
    EAN11 varchar(20),
    EAN12 varchar(20),
    EAN13 varchar(20),
    EAN14 varchar(20),
    EAN15 varchar(20),
    EAN16 varchar(20),
    EAN17 varchar(20),
    EAN18 varchar(20),
    EAN19 varchar(20),
    EAN20 varchar(20))
as
declare variable MODELO_ID integer;
declare variable GRADE_NO_MODELO integer;
begin

  MODELO_ID = coalesce((select first 1 p.modelo_codigo from tbproduto p where p.codigo = :PRODUTO_ID),0);
  GRADE_NO_MODELO = 0;

  if(modelo_id > 0)then
  begin
    for
         select first 1
            m.DESCRICAO , m.CODIGO , m.TOTAL_TAMANHOS , m.DATA ,
            m.STATUS , m.STATUSEXCLUSAO , m.USUARIO_CODIGO ,
    
            m.T01 , m.T02 , m.T03 , m.T04 , m.T05 ,
            m.T06 , m.T07 , m.T08 , m.T09 , m.T10 ,
            m.T11 , m.T12 , m.T13 , m.T14 , m.T15 ,
            m.T16 , m.T17 , m.T18 , m.T19 , m.T20 ,
    
            m.E01 , m.E02 , m.E03 , m.E04 , m.E05 ,
            m.E06 , m.E07 , m.E08 , m.E09 , m.E10 ,
            m.E11 , m.E12 , m.E13 , m.E14 , m.E15 ,
            m.E16 , m.E17 , m.E18 , m.E19 , m.E20 ,
    
            m.USA01 , m.USA02 , m.USA03 , m.USA04 ,
            m.USA05 , m.USA06 , m.USA07 , m.USA08 ,
            m.USA09 , m.USA10 , m.USA11 , m.USA12 ,
            m.USA13 , m.USA14 , m.USA15 , m.USA16 ,
            m.USA17 , m.USA18 , m.USA19 , m.USA20 ,
    
            m.EUR01 , m.EUR02 , m.EUR03 , m.EUR04 ,
            m.EUR05 , m.EUR06 , m.EUR07 , m.EUR08 ,
            m.EUR09 , m.EUR10 , m.EUR11 , m.EUR12 ,
            m.EUR13 , m.EUR14 , m.EUR15 , m.EUR16 ,
            m.EUR17 , m.EUR18 , m.EUR19 , m.EUR20 ,
    
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T01) > 0,1,0),n.T01) as A01,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T02) > 0,1,0),n.T02) as A02,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T03) > 0,1,0),n.T03) as A03,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T04) > 0,1,0),n.T04) as A04,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),n.T05) as A05,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),n.T06) as A06,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),n.T07) as A07,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),n.T08) as A08,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T07) > 0,1,0),n.T09) as A09,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T10) > 0,1,0),n.T10) as A10,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T11) > 0,1,0),n.T11) as A11,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T12) > 0,1,0),n.T12) as A12,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T13) > 0,1,0),n.T13) as A13,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T14) > 0,1,0),n.T14) as A14,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T15) > 0,1,0),n.T15) as A15,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T16) > 0,1,0),n.T16) as A16,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T17) > 0,1,0),n.T17) as A17,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T18) > 0,1,0),n.T18) as A18,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T19) > 0,1,0),n.T19) as A19,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T20) > 0,1,0),n.T20) as A20,
    
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T01) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 1),''))  as EAN01,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T02) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 2),''))  as EAN02,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T03) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 3),''))  as EAN03,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T04) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 4),''))  as EAN04,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 5),''))  as EAN05,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 6),''))  as EAN06,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 7),''))  as EAN07,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 8),''))  as EAN08,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T07) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 9),''))  as EAN09,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T10) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 10),'')) as EAN10,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T11) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 11),'')) as EAN11,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T12) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 12),'')) as EAN12,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T13) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 13),'')) as EAN13,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T14) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 14),'')) as EAN14,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T15) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 15),'')) as EAN15,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T16) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 16),'')) as EAN16,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T17) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 17),'')) as EAN17,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T18) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 18),'')) as EAN18,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T19) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 19),'')) as EAN19,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T20) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 20),'')) as EAN20
    
        from
            tbgrade m,tbproduto p, tbmodelo n
    
        where m.codigo = n.grade_codigo
        and p.codigo = :PRODUTO_ID
        and n.codigo = :modelo_id
        and p.modelo_codigo = p.modelo_codigo
      into
        :DESCRICAO,
        :CODIGO,
        :TOTAL_TAMANHOS,
        :DATA,
        :STATUS,
        :STATUSEXCLUSAO,
        :USUARIO_CODIGO,
        :T01,
        :T02,
        :T03,
        :T04,
        :T05,
        :T06,
        :T07,
        :T08,
        :T09,
        :T10,
        :T11,
        :T12,
        :T13,
        :T14,
        :T15,
        :T16,
        :T17,
        :T18,
        :T19,
        :T20,
        :E01,
        :E02,
        :E03,
        :E04,
        :E05,
        :E06,
        :E07,
        :E08,
        :E09,
        :E10,
        :E11,
        :E12,
        :E13,
        :E14,
        :E15,
        :E16,
        :E17,
        :E18,
        :E19,
        :E20,
        :USA01,
        :USA02,
        :USA03,
        :USA04,
        :USA05,
        :USA06,
        :USA07,
        :USA08,
        :USA09,
        :USA10,
        :USA11,
        :USA12,
        :USA13,
        :USA14,
        :USA15,
        :USA16,
        :USA17,
        :USA18,
        :USA19,
        :USA20,
        :EUR01,
        :EUR02,
        :EUR03,
        :EUR04,
        :EUR05,
        :EUR06,
        :EUR07,
        :EUR08,
        :EUR09,
        :EUR10,
        :EUR11,
        :EUR12,
        :EUR13,
        :EUR14,
        :EUR15,
        :EUR16,
        :EUR17,
        :EUR18,
        :EUR19,
        :EUR20,
        :A01,
        :A02,
        :A03,
        :A04,
        :A05,
        :A06,
        :A07,
        :A08,
        :A09,
        :A10,
        :A11,
        :A12,
        :A13,
        :A14,
        :A15,
        :A16,
        :A17,
        :A18,
        :A19,
        :A20,
        :EAN01,
        :EAN02,
        :EAN03,
        :EAN04,
        :EAN05,
        :EAN06,
        :EAN07,
        :EAN08,
        :EAN09,
        :EAN10,
        :EAN11,
        :EAN12,
        :EAN13,
        :EAN14,
        :EAN15,
        :EAN16,
        :EAN17,
        :EAN18,
        :EAN19,
        :EAN20
        
      do begin
        suspend;
        GRADE_NO_MODELO = 1;
      end
  end

  if(modelo_id = 0 or GRADE_NO_MODELO = 0)then
  begin
      for
         select first 1
            m.DESCRICAO , m.CODIGO , m.TOTAL_TAMANHOS , m.DATA ,
            m.STATUS , m.STATUSEXCLUSAO , m.USUARIO_CODIGO ,
    
            m.T01 , m.T02 , m.T03 , m.T04 , m.T05 ,
            m.T06 , m.T07 , m.T08 , m.T09 , m.T10 ,
            m.T11 , m.T12 , m.T13 , m.T14 , m.T15 ,
            m.T16 , m.T17 , m.T18 , m.T19 , m.T20 ,
    
            m.E01 , m.E02 , m.E03 , m.E04 , m.E05 ,
            m.E06 , m.E07 , m.E08 , m.E09 , m.E10 ,
            m.E11 , m.E12 , m.E13 , m.E14 , m.E15 ,
            m.E16 , m.E17 , m.E18 , m.E19 , m.E20 ,
    
            m.USA01 , m.USA02 , m.USA03 , m.USA04 ,
            m.USA05 , m.USA06 , m.USA07 , m.USA08 ,
            m.USA09 , m.USA10 , m.USA11 , m.USA12 ,
            m.USA13 , m.USA14 , m.USA15 , m.USA16 ,
            m.USA17 , m.USA18 , m.USA19 , m.USA20 ,
    
            m.EUR01 , m.EUR02 , m.EUR03 , m.EUR04 ,
            m.EUR05 , m.EUR06 , m.EUR07 , m.EUR08 ,
            m.EUR09 , m.EUR10 , m.EUR11 , m.EUR12 ,
            m.EUR13 , m.EUR14 , m.EUR15 , m.EUR16 ,
            m.EUR17 , m.EUR18 , m.EUR19 , m.EUR20 ,
    
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T01) > 0,1,0),n.T01) as A01,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T02) > 0,1,0),n.T02) as A02,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T03) > 0,1,0),n.T03) as A03,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T04) > 0,1,0),n.T04) as A04,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),n.T05) as A05,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),n.T06) as A06,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),n.T07) as A07,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),n.T08) as A08,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T07) > 0,1,0),n.T09) as A09,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T10) > 0,1,0),n.T10) as A10,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T11) > 0,1,0),n.T11) as A11,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T12) > 0,1,0),n.T12) as A12,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T13) > 0,1,0),n.T13) as A13,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T14) > 0,1,0),n.T14) as A14,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T15) > 0,1,0),n.T15) as A15,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T16) > 0,1,0),n.T16) as A16,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T17) > 0,1,0),n.T17) as A17,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T18) > 0,1,0),n.T18) as A18,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T19) > 0,1,0),n.T19) as A19,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T20) > 0,1,0),n.T20) as A20,
    
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T01) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 1),''))  as EAN01,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T02) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 2),''))  as EAN02,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T03) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 3),''))  as EAN03,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T04) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 4),''))  as EAN04,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 5),''))  as EAN05,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T05) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 6),''))  as EAN06,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 7),''))  as EAN07,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T06) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 8),''))  as EAN08,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T07) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 9),''))  as EAN09,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T10) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 10),'')) as EAN10,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T11) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 11),'')) as EAN11,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T12) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 12),'')) as EAN12,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T13) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 13),'')) as EAN13,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T14) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 14),'')) as EAN14,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T15) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 15),'')) as EAN15,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T16) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 16),'')) as EAN16,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T17) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 17),'')) as EAN17,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T18) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 18),'')) as EAN18,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T19) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 19),'')) as EAN19,
            IIF(coalesce(p.modelo_codigo,0) = 0,IIF(CHAR_LENGTH(m.T20) > 0,1,0),COALESCE((SELECT first 1 S.PREFIXO||S.CODIGO||FN_DIGITOEAN(S.PREFIXO||S.CODIGO) FROM TBEAN S WHERE S.MODELO_ID = p.modelo_codigo AND S.COR_ID = P.COR_CODIGO AND S.TAMANHO = 20),'')) as EAN20
    
        from
    
            tbgrade m,tbproduto p, tbmodelo n
    
        where m.codigo = p.GRADE_CODIGO
        and p.codigo = :PRODUTO_ID
        and iif(coalesce(p.modelo_codigo,0) >0,n.codigo = p.modelo_codigo, true)
      into
        :DESCRICAO,
        :CODIGO,
        :TOTAL_TAMANHOS,
        :DATA,
        :STATUS,
        :STATUSEXCLUSAO,
        :USUARIO_CODIGO,
        :T01,
        :T02,
        :T03,
        :T04,
        :T05,
        :T06,
        :T07,
        :T08,
        :T09,
        :T10,
        :T11,
        :T12,
        :T13,
        :T14,
        :T15,
        :T16,
        :T17,
        :T18,
        :T19,
        :T20,
        :E01,
        :E02,
        :E03,
        :E04,
        :E05,
        :E06,
        :E07,
        :E08,
        :E09,
        :E10,
        :E11,
        :E12,
        :E13,
        :E14,
        :E15,
        :E16,
        :E17,
        :E18,
        :E19,
        :E20,
        :USA01,
        :USA02,
        :USA03,
        :USA04,
        :USA05,
        :USA06,
        :USA07,
        :USA08,
        :USA09,
        :USA10,
        :USA11,
        :USA12,
        :USA13,
        :USA14,
        :USA15,
        :USA16,
        :USA17,
        :USA18,
        :USA19,
        :USA20,
        :EUR01,
        :EUR02,
        :EUR03,
        :EUR04,
        :EUR05,
        :EUR06,
        :EUR07,
        :EUR08,
        :EUR09,
        :EUR10,
        :EUR11,
        :EUR12,
        :EUR13,
        :EUR14,
        :EUR15,
        :EUR16,
        :EUR17,
        :EUR18,
        :EUR19,
        :EUR20,
        :A01,
        :A02,
        :A03,
        :A04,
        :A05,
        :A06,
        :A07,
        :A08,
        :A09,
        :A10,
        :A11,
        :A12,
        :A13,
        :A14,
        :A15,
        :A16,
        :A17,
        :A18,
        :A19,
        :A20,
        :EAN01,
        :EAN02,
        :EAN03,
        :EAN04,
        :EAN05,
        :EAN06,
        :EAN07,
        :EAN08,
        :EAN09,
        :EAN10,
        :EAN11,
        :EAN12,
        :EAN13,
        :EAN14,
        :EAN15,
        :EAN16,
        :EAN17,
        :EAN18,
        :EAN19,
        :EAN20

      do begin
        suspend;
      end
  end

end
^

SET TERM ; ^

