exit stored procedure based on condition

braxx 461 Reputation points
2021-02-18T17:44:45.053+00:00

I have a stored procedure which is working fine and a simplified version is provided below. The result of this procedure is a json string containing data. It is then send to Azure Function for further processing.

There are cases when I do not want this stored procedure to be executed. I mean when there is no data for either @json1 or @json2.
In that cases, I need to add such a condition which will check:

if @json1 or @json2 <> null

if yes - > exec the procedure
if no - > do not exec the procedure

I tried with "if else" or "return" but with no luck. I do not want to do a mess by providing the wrong code here, basically because I do not know how to approch it.

CREATE procedure [schema].[myProcedure]  
@category nvarchar (255),  
@brand nvarchar(255),  
@geo nvarchar (255),  
@emailrecipient nvarchar (100)  
  
as begin  
  
DECLARE @json_construct varchar(MAX) = '{"req1": {X}, "req2": {Y}}';  
DECLARE @json1 VARCHAR(MAX); DECLARE @json2 VARCHAR(MAX)  
  
SET @json1 =  
(  
			select distinct  
			   [Category]  
			  ,[Brand]  
			  ,[Geo]  
			  ,[Owner] as [EmailRecipent]  
			from   
			   [schema].[table1]  
			where  
			   [Owner] = @emailrecipient  
			   and isnull([Category], '') = @category  
			   and isnull([Brand], '') = @brand  
			   and isnull([Geo], '') = @geo  
			for json path, INCLUDE_NULL_VALUES   
)  
SET @json2 =  
(  
			select  
			   [URL]  
			  ,[Start - End Date]  
			  ,[Media Channels]  
			from [schema].[table2]  
			where  
				isnull([CatDescription], '') like @category  
			    and isnull([BrandDescription], '') like @brand  
			    and isnull([Restriction Markets], '') like @geo  
			for json path, INCLUDE_NULL_VALUES   
)  
SELECT   
		REPLACE(  
			REPLACE(@json_construct,'{X}', isnull(@json1, '[]')),  
		'{Y}', isnull(@json2,'[]'))  
	AS json_output;  
end  


  
  
  
  
  
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
{count} votes

3 answers

Sort by: Most helpful
  1. Tom Phillips 17,786 Reputation points
    2021-02-18T18:16:08.443+00:00
    CREATE procedure [schema].[myProcedure]
     @category nvarchar (255),
     @brand nvarchar(255),
     @geo nvarchar (255),
     @emailrecipient nvarchar (100)
    
     as begin
    
     DECLARE @json_construct varchar(MAX) = '{"req1": {X}, "req2": {Y}}';
     DECLARE @json1 VARCHAR(MAX); DECLARE @json2 VARCHAR(MAX)
    
     SET @json1 =
     (
                 select distinct
                    [Category]
                   ,[Brand]
                   ,[Geo]
                   ,[Owner] as [EmailRecipent]
                 from 
                    [schema].[table1]
                 where
                    [Owner] = @emailrecipient
                    and isnull([Category], '') = @category
                    and isnull([Brand], '') = @brand
                    and isnull([Geo], '') = @geo
                 for json path, INCLUDE_NULL_VALUES 
     )
     SET @json2 =
     (
                 select
                    [URL]
                   ,[Start - End Date]
                   ,[Media Channels]
                 from [schema].[table2]
                 where
                     isnull([CatDescription], '') like @category
                     and isnull([BrandDescription], '') like @brand
                     and isnull([Restriction Markets], '') like @geo
                 for json path, INCLUDE_NULL_VALUES 
     )
    
     If @json1 IS NOT NULL OR @json2 IS NOT NULL
     BEGIN
    
         SELECT 
                 REPLACE(
                     REPLACE(@json_construct,'{X}', isnull(@json1, '[]')),
                 '{Y}', isnull(@json2,'[]'))
             AS json_output;
    END
    end
    
    1 person found this answer helpful.

  2. brenda grossnickle 226 Reputation points
    2021-02-18T18:11:37.417+00:00

    IF @json2 is null and @json1 is null
    return

    1 person found this answer helpful.

  3. braxx 461 Reputation points
    2021-02-18T20:19:11.5+00:00

    resolved by combining the answers of @Tom Phillips and @brenda grossnickle

    the last part of a stored procedure is like:

    ......  
      
    )  
      
    	If @json1 IS NULL or @json2 IS NULL return  
    	begin  
    	SELECT   
    			REPLACE(  
    				REPLACE(@json_construct,'{X}', isnull(@json1, '[]')),  
    			'{Y}', isnull(@json2,'[]'))  
    		AS json_output;  
    	end  
    end  
    GO  
    

    Thank you guys!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.