Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Use of NULL and zero while writing stored procedures

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 39
    Comment on it

    While writing the stored procedure you always pass the parameters as the input type and the output type according to your need.

     

     

    So while using these parameters you may notice that if you miss any parameter value during the stored procedure invocation  the program or application throws an error.

     

     

    So if you are a smart programmer you need to know when nothing is passed in the input parameters you need to assign default values to it. This will run your code properly.

     

     

    So for the integer values you need to pass zero as the default value and for the string values you need to pass the NULL value as the default type.

     

     

    This will ensure that your code will not get affected or broken even when no argument is passed to the stored procedure.

     

     

    LTER PROC [dbo].[uspGetNudgesDetails]
    @AccessToken VARCHAR(50),
    @BeaconID VARCHAR(50)=NULL,
    @BeaconMajorID as VARCHAR(20)=NULL,
    @BeaconMinorID as VARCHAR(20)=NULL
    AS
    
    BEGIN
    
    	DECLARE @CompanyID BIGINT =0
    	DECLARE @TargetUserID BIGINT=0
    	DECLARE @UserName varchar(50)
    
    
    -- Get user and company
    
    	SELECT @UserName=[FirstName] + ' ' + LastName, 
    			@TargetUserID=[ID],
    			@CompanyID=[CompanyID]
    		FROM [User] WHERE AccessToken=@AccessToken
    
    
    		----DECLARE @CreatedDate AS DATETIME = GETDATE()
    		----DECLARE @ExpiryDate AS DATETIME = (SELECT DATEADD(DAY, @DaysTillExpire, @CreatedDate))
    		----SET @ExpiryDate = (SELECT DATEADD(HOUR, @HoursTillExpire, @ExpiryDate))
    
    		UPDATE NUDGE
    		SET DaysTillExpire = CASE  WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    		,
    		HoursTillExpire = CASE  WHEN DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    
    		,
    		IsExpired = CASE WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 AND DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 1
    							ELSE 0
    							END
    -- For general message with no location
    
    	SELECT N.[ID],
    		N.[Message],
    		@BeaconID as [BeaconID],
            N.[LocationID],
    		NULL AS Location,
    		N.[TargetUserID],
    		@UserName as [TargetUser],	
    		N.[IsCompleted],
    		N.[IsDelivered],
    		N.[IsExpired],
    		N.[IsDeleted],
    		N.[Url],		
    		N.[DaysTillExpire],
    		N.[HoursTillExpire],
    		--DATEDIFF( day, GETDATE(), N.ExpiryDate ) AS DaysTillExpire,
    		--DATEDIFF( hour, GETDATE(), N.ExpiryDate ) AS HoursTillExpire,
    		(U.[FirstName] + ' ' + U.[LastName]) AS [CreatedBy],
    		CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],
    		@CompanyID as [CompanyID]
    		
    		FROM [NUDGE] AS N 
    				INNER JOIN  [User] AS U ON N.[CreatedBy]=U.[ID] 
    		WHERE N.[LocationID]=0 
    				AND N.[TargetUserID]=@TargetUserID
    				AND N.[IsDeleted]=0 
    				AND N.[IsExpired]=0 
    				AND N.[IsCompleted]=0 
    
    	UNION
    
    -- For location dependent messages
    
    	SELECT  N.[ID],
    		N.[Message],
    		@BeaconID as [BeaconID],
    		N.[LocationID],
    		L.[Location],
    		N.[TargetUserID],
    		@UserName as [TargetUser],	
    		N.[IsCompleted],
    		N.[IsDelivered],
    		N.[IsExpired],
    		N.[IsDeleted],		
    		N.[Url],		
    		N.[DaysTillExpire],
    		N.[HoursTillExpire],
    		--DATEDIFF( day, GETDATE(), N.ExpiryDate ) AS DaysTillExpire,
    		--DATEDIFF( hour, GETDATE(), N.ExpiryDate ) AS HoursTillExpire,	
    		(U.[FirstName] + ' ' + U.[LastName]) AS [CreatedBy],
    		CONVERT(VARCHAR(20), N.[CreatedDate], 103) as [CreatedDate],
    		@CompanyID as [CompanyID]
    		
    		
    		FROM [NUDGE] AS N 
    			INNER JOIN  [User] AS U ON N.[CreatedBy]=U.[ID] 
    			INNER JOIN [LOCATION] AS L ON L.[ID]=N.[LocationID] 
    		WHERE N.[LocationID] IN (SELECT [LocationID] FROM [Location] WHERE [BeaconUUID]=@BeaconID and [CompanyID]=@CompanyID AND [BeaconMajor]=@BeaconMajorID AND [BeaconMinor]=@BeaconMinorID)
    			AND N.[TargetUserID]=@TargetUserID
    			AND N.[IsDeleted]=0 
    			AND N.[IsExpired]=0 
    			AND N.[IsCompleted]=0 
    END

     

     

    In this stored procedure  we have assigned NULL values as the default type to the input parameters.

    SQL Server

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: