This is my first time doing this and the reason is because I would like to avoid typing the same sql script code when using the conditional "if". My first option was creating a variable and assign the "select" statement to it , now depending on another variable value , I will just change the "where" condition but instead of getting all the columns and rows that I get on a regular sql script , I'm getting just one cell with the text assigned to the variable. Below is my code:
DECLARE @SQLScript varchar(MAX)='' ,
@Subordinate int = 1;
SELECT @SQLScript = N'SELECT * FROM Account
WHERE AccountID = 345015'
IF @Subordinate = 1
BEGIN
SELECT @SQLScript =@SQLScript +' AND Status=2 ORDER BY 1 asc'
END
ELSE
BEGIN
SELECT @SQLScript =@SQLScript +' AND Status=1 ORDER BY 2 asc'
END
SELECT @SQLScript
What I'm getting after executing the above script is 1 cell showing the following text "SELECT * Account where AccountID = 345015 AND Status=2 group by 1 asc" . What I need is to get the table data from that script