CREATE  FUNCTION [dbo].[CheckFarmacope]
    (
      @K_Code VARCHAR(15) ,
      @Sazman_Code VARCHAR(3) ,
      @grade_Code VARCHAR(3)
    )
RETURNS @Farma TABLE
    (
      Status CHAR(1) ,
      Message NVARCHAR(50) ,
      ShowMessage CHAR(1)
    )
AS
    BEGIN
        IF ( EXISTS ( SELECT    K_Code
                      FROM      dbo.Farmacope
                      WHERE     K_Code = @K_Code ) )
            BEGIN
                IF ( EXISTS ( SELECT    K_Code
                              FROM      dbo.Farmacope
                              WHERE     K_Code = @k_Code
                                        AND Sazman_Code = '00'
                                        AND Grade_Code = '00' ) )
                    BEGIN
                        INSERT  @Farma
                                ( Status ,
                                  Message ,
                                  ShowMessage                           
                                )
                                SELECT  [Status] ,
                                        [Message] ,
                                        [ShowMessage]
                                FROM    dbo.Farmacope
                                WHERE   K_Code = @K_Code
                                        AND Sazman_Code = '00'
                                        AND Grade_Code = '00' 
     
                    END
                ELSE
                    IF ( EXISTS ( SELECT    k_code
                                  FROM      dbo.Farmacope
                                  WHERE     K_Code = @K_Code
                                            AND Sazman_Code = '00'
                                            AND Grade_Code = @grade_Code ) )
                        BEGIN                            

                            INSERT  @Farma
                                    ( Status ,
                                      Message ,
                                      ShowMessage                           
                                    )
                                    SELECT  [Status] ,
                                            [Message] ,
                                            [ShowMessage]
                                    FROM    dbo.Farmacope
                                    WHERE   K_Code = @K_Code
                                            AND Sazman_Code = '00'
                                            AND Grade_Code = @grade_Code
                        END                
                    ELSE
                        IF ( EXISTS ( SELECT    k_code
                                      FROM      dbo.Farmacope
                                      WHERE     K_Code = @K_Code
                                                AND Sazman_Code = @Sazman_Code
                                                AND Grade_Code = '00' ) )
                            BEGIN                           
                                INSERT  @Farma
                                        ( Status ,
                                          Message ,
                                          ShowMessage                           
                                        )
                                        SELECT  [Status] ,
                                                [Message] ,
                                                [ShowMessage]
                                        FROM    dbo.Farmacope
                                        WHERE   K_Code = @K_Code
                                                AND Sazman_Code = @Sazman_Code
                                                AND Grade_Code = '00'              

                            END
                        ELSE
                            IF ( EXISTS ( SELECT    k_code
                                          FROM      dbo.Farmacope
                                          WHERE     K_Code = @K_Code
                                                    AND Sazman_Code = @Sazman_Code
                                                    AND Grade_Code = @grade_Code ) )
                                BEGIN                           
                                    INSERT  @Farma
                                            ( Status ,
                                              Message ,
                                              ShowMessage                           
                                            )
                                            SELECT  [Status] ,
                                                    [Message] ,
                                                    [ShowMessage]
                                            FROM    dbo.Farmacope
                                            WHERE   K_Code = @K_Code
                                                    AND Sazman_Code = @Sazman_Code
                                                    AND Grade_Code = @grade_Code

                                END
            END
        RETURN
    END
