Code:
IF @Date is NULL
BEGIN
SELECT Task
From Tasks
Where LocationID = @LocationID
END
ELSE
BEGIN
SELECT Task
From Tasks
Where LocationID = @LocationID and Date = @Date
END
As you can see the SELECT and the FROM parts never change, the WHERE clause is the only thing that changes.
How would I concatentate the SELECT and FROM into a string variable and then i'll have an IF statement decide which WHERE i need and concatenate that onto the string variable.
Then how will i execute the SQL contained in the string to return the result set?