Stack Exchange
Simplify The If and else IF Query
ALTER PROCEDURE spDisplayTableNames1
    @T_ID INT OUT,
    @BatchNumber VARCHAR(30) OUT
AS
BEGIN
    IF((SELECT COUNT(*) FROM tblPacks 
        WHERE T_ID = @T_ID 
          AND BatchNumber = @BatchNumber) = 0) 
    AND ((SELECT COUNT(*) FROM tblBlisters 
          WHERE T_ID = @T_ID 
            AND BatchNumber = @BatchNumber) = 0) 
    AND ((SELECT COUNT(*) FROM tblShippers 
          WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber) = 0)
BEGIN 
    PRINT '0 0 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '1 0 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '0 1 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '0 0 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '1 1 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '1 0 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '0 1 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '1 1 1'
END

END

Here I am checking conditions on three tables and if a column exists in three tables it should print '1 1 1', if it exists only in first and third table then print '1 0 1'. Like that I wrote 8 conditional statements. But I want a simplified query for the above stored procedure
I want the simplest query for the above example - how can I do that?

Try this:
declare @prt varchar(10);
if ((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
    set @prt = '0';
else
    set @prt = '1';

if ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
    set @prt = @prt + ' 0';
else
    set @prt = @prt + ' 1';

if ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) 
    set @prt = @prt + ' 0';
else
    set @prt = @prt + ' 1'; 

print @prt;


Try this
DECLARE @PRINT VARCHAR(10)=''

SELECT @PRINT = @PRINT+CAST(PR AS VARCHAR(10))+' ' FROM (
SELECT COUNT(DISTINCT 1) AS PR FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber
UNION ALL
SELECT COUNT(DISTINCT 1) FROM tblBlisters WHERE  T_ID=@T_ID AND BatchNumber=@BatchNumber
UNION ALL
SELECT COUNT(DISTINCT 1) FROM tblShippers WHERE  T_ID=@T_ID AND BatchNumber=@BatchNumber
)A

SELECT @PRINT

If the record exist in a table then the COUNT(DISTINCT 1) will give One 1  ,otherwise Zero(0). 
Next store the result set in a variable by appending every record into it.

I think this would give an extra space at the beginning, for example ' 1 0 0' instead of '1 0 0'. But that's probably easily fixed ...

No. It won't give Space as prefix. But space will come in the last. If we need we can remove. However the Trailing spaces not a matter

You're right, it's a trailing space, I misread the code. Either is easily fixed by LTRIM() or RTRIM() ...

I am answering because if (select count(*) . . .) is a very bad idiom.  This requires running an aggregation query to see if a single row exists.  A better approach is if exists().
In addition, all the conditional logic can be done in a select:
declare @prt varchar(10);

select @prt = (case when exists (select 1 from tblpacks where T_ID = @T_ID  and BatchNumber = @BatchNumber)
                    then '0' else '1'
               end) +
              (case when exists (select 1 from tblBlisters where T_ID = @T_ID and BatchNumber = @BatchNumber)
                    then ' 0'
                    else ' 1'
               end) +
              (case when exists (select 1 from tblShippers where T_ID = @T_ID and BatchNumber = @BatchNumber)
                    then ' 0';
                    else ' 1'
               end)
              )
print @prt;

