USE [GrpInsurance]
GO
/****** Object:  StoredProcedure [dbo].[gproc_GetPermission]    Script Date: 02/28/2014 15:25:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[gproc_GetPermission]
@IdMenu varchar(5),
@IdUser varchar(4),
@FieldName varchar(20) OUTPUT,
@FieldValue varchar(50) OUTPUT
AS
DECLARE @Id_Permission varchar(5)
   SET @FieldName=''
   SET @FieldValue=''

   --IF EXISTS(SELECT T1.Id_Permission FROM gPermission_Menu T1 INNER JOIN gPermission_User T2
   --ON T1.Id_Permission=T2.Id_Permission WHERE T1.Id_Menu=@IdMenu AND T2.Id_User=@IdUser) 
   --BEGIN
   --  SELECT @FieldName=T1.Field_Name, @FieldValue=T1.Field_Value  FROM gPermission_Menu T1 INNER JOIN gPermission_User T2
   --  ON T1.Id_Permission=T2.Id_Permission WHERE T1.Id_Menu=@IdMenu AND T2.Id_User=@IdUser
   --  RETURN 0
   --END
   --ELSE RETURN 1
   
   IF @IdUser NOT IN (SELECT T2.Id_User
                      FROM 
                          gPermission_Menu T1 INNER JOIN 
                          gPermission_User T2 ON T1.Id_Permission=T2.Id_Permission 
                      WHERE T1.Id_Menu=@IdMenu)
      BEGIN
          SELECT DISTINCT @FieldName=T1.Field_Name, @FieldValue=T1.Field_Value  
          FROM gPermission_Menu T1 INNER JOIN 
               gPermission_User T2 ON T1.Id_Permission=T2.Id_Permission 
          WHERE T1.Id_Menu=@IdMenu
          RETURN 0
      END
   ELSE RETURN 1



SELECT T1.*,T2.*  
FROM 
    gPermission_Menu T1 INNER JOIN 
    gPermission_User T2 ON T1.Id_Permission=T2.Id_Permission 
WHERE T1.Id_Menu='10411' AND T2.Id_User<>'DUR'

IF 'DUR' IN (
SELECT T2.Id_User
FROM 
    gPermission_Menu T1 INNER JOIN 
    gPermission_User T2 ON T1.Id_Permission=T2.Id_Permission 
WHERE T1.Id_Menu='10411')
PRINT 'ada'
ELSE
PRINT 'takada'
----------------------
SELECT DISTINCT T1.Field_Name, T1.Field_Value  
FROM gPermission_Menu T1 INNER JOIN 
     gPermission_User T2 ON T1.Id_Permission=T2.Id_Permission 
WHERE T1.Id_Menu='10411'