Saturday, March 22, 2014

SQL SERVER Stored Procedure Check parameter Value exists on query result and return value according to existence

i have stored procedure in sql server take parameter user_id
i need to check permission for user_id for specific query concept
i need to pass user_id parameter and check if this user exists on query result or not
if it is exists the stored will return 1 else the stored will return 0
so if result = 1 meaning that the user has permission i want else i will prevent him to complete action in my code side.

the stored Procedure Sample Code
USE [HES_Intranet]
GO
/****** Object:  StoredProcedure [dbo].[VACATION_REPORTS_REQUEST_GET_SENT_COUNT]    Script Date: 03/23/2014 01:49:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[VACATION_REPORTS_REQUEST_GET_USER_STAGE_PERMISSION] 
 @USER_ID INT ,
 @POSITION_ID INT ,
 @STAGE_ID INT , 
 @WORK_FLOW_ID INT
AS 
BEGIN
SET NOCOUNT ON 
SET XACT_ABORT ON  
/*THE IF STATEMENT CHECK IF 1 IS SELECTED THIS MEANING THAT THE 
USER_ID PASSED EXISTS ON SELECT PERMISSION QUERY
ELSE MEANING THAT THAT USER POSITION NOT ALLOWED TO LOGIN AND ALSO 
NO PERMISSION FOR WORKFLOW GAVEN TO HIM BY SYSTEM ADMIN */

  IF(  
(SELECT 1
     WHERE @USER_ID IN ( select USER_ID from USERS U  where U.POSITION_ID=@POSITION_ID
union
select USER_ID from USER_ROLES UR 
where ur.WORK_FLOW_ID=@WORK_FLOW_ID
AND UR.REQUEST_STAGE_ID=@STAGE_ID)) =1)
RETURN 1
ELSE
RETURN 0;
END

the Stored Procedure Call Test  in sql server Side
declare @valback int;
exec @valback = VACATION_REPORTS_REQUEST_GET_USER_STAGE_PERMISSION 10,1,1;
SELECT  @valback; 

No comments:

Post a Comment