IF EXISTS (SELECT * 
             FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'CONSULTA_QUADRO_ALIQUOTAS'
              AND TYPE = 'FN'
              AND U.UID = O.UID
              AND U.NAME = USER)
   DROP FUNCTION CONSULTA_QUADRO_ALIQUOTAS
GO

CREATE FUNCTION CONSULTA_QUADRO_ALIQUOTAS(
  @P_SGESTADOORIG VARCHAR(02),
  @P_SGESTADODEST VARCHAR(02)) RETURNS VARCHAR(05)
AS
BEGIN
   DECLARE @V_VRALIQUOTA VARCHAR(02);

   SELECT @V_VRALIQUOTA = CASE UPPER(@P_SGESTADODEST) WHEN 'AC' THEN SGESTADODESTAC
                                                      WHEN 'AL' THEN SGESTADODESTAL
                                                      WHEN 'AM' THEN SGESTADODESTAM
                                                      WHEN 'AP' THEN SGESTADODESTAP
                                                      WHEN 'BA' THEN SGESTADODESTBA
                                                      WHEN 'CE' THEN SGESTADODESTCE
                                                      WHEN 'DF' THEN SGESTADODESTDF
                                                      WHEN 'ES' THEN SGESTADODESTES
                                                      WHEN 'GO' THEN SGESTADODESTGO
                                                      WHEN 'MA' THEN SGESTADODESTMA
                                                      WHEN 'MT' THEN SGESTADODESTMT
                                                      WHEN 'MS' THEN SGESTADODESTMS
                                                      WHEN 'MG' THEN SGESTADODESTMG
                                                      WHEN 'PA' THEN SGESTADODESTPA
                                                      WHEN 'PB' THEN SGESTADODESTPB
                                                      WHEN 'PR' THEN SGESTADODESTPR
                                                      WHEN 'PE' THEN SGESTADODESTPE
                                                      WHEN 'PI' THEN SGESTADODESTPI
                                                      WHEN 'RN' THEN SGESTADODESTRN
                                                      WHEN 'RS' THEN SGESTADODESTRS
                                                      WHEN 'RJ' THEN SGESTADODESTRJ
                                                      WHEN 'RO' THEN SGESTADODESTRO
                                                      WHEN 'RR' THEN SGESTADODESTRR
                                                      WHEN 'SC' THEN SGESTADODESTSC
                                                      WHEN 'SP' THEN SGESTADODESTSP
                                                      WHEN 'SE' THEN SGESTADODESTSE
                                                      WHEN 'TO' THEN SGESTADODESTTO END
     FROM QUADROALIQUOTA
    WHERE SGESTADOORIG = @P_SGESTADOORIG;
   RETURN (ISNULL(@V_VRALIQUOTA, '0'));
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'CONSULTA_QUADRO_ALIQUOTA','6.05.xxx';
GO

