Hello SQL Gurus, I am from procedure programming background, end up writing TSQL recently with my new job. My mindset still thinking about writing queries with if conditions. How to avoid following query without if condition.
DECLARE @NumAddress INT
SELECT
@NumAddress = COUNT(*)
FROM Address
WHERE UserID = 1001
IF @NumAddress > 0
BEGIN
SELECT
u.FullName, a.Address AS Address
FROM
Users u
JOIN Address a ON a.UserID = u.UserID
WHERE
u.UserId = 1000
END
ELSE
BEGIN
SELECT
u.FullName, NULL AS Address
FROM
Users u
WHERE
u.UserId = 1000
END
NOTE: My sample query is a simplified example of my actual query. So please ignore this and provide me an example, so that how could I avoid IF conditions like this one. Thanks in advance.