I have a stored procedure with a number of parameters. I would like to write my query so that it joins with certain tables but only if a particular parameter has a value. Take
DECLARE @SQL varchar(max)
SET @SQL = 'SELECT * FROM PERSON P'
IF NULLIF(@ADDRESSID,"") IS NULL SET @SQL = @SQL + " INNER JOIN ADDRESSES A ON P.AddressID = A.AddressID"
EXEC sp_executesql @SQL, N'@ADDRESSID int', @ADDRESSID