IF @json2 is null and @json1 is null
return
exit stored procedure based on condition
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
3 answers
Sort by: Most helpful
-
brenda grossnickle 206 Reputation points
2021-02-18T18:11:37.417+00:00 -
Tom Phillips 17,741 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
-
braxx 441 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!