Dynamic SQL // :
DECLARE @Query1 varchar(max),@Query2 varchar(max), @Variable varchar(20),@Flag varchar(20)
SET @Variable = '''TEST'''
SET @Flag = 'asd'
SET @Query1 = 'SELECT * FROM DATA WHERE VOUCHERNO ='+@Variable
SET @Query2 = 'SELECT * FROM DATA D INNER JOIN MR000 M ON D.BookNo = M.MasterID WHERE VOUCHERNO ='+@Variable
BEGIN
IF(@Flag<>@Variable)
BEGIN
EXEC(@Query2)
END
ELSE
BEGIN
EXEC(@Query1)
END
END
This Might Help!