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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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;