1

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

3
  • 1
    sp_executesql Commented Nov 30, 2018 at 15:09
  • Just making sure; are yo using SQL Server? Commented Nov 30, 2018 at 15:28
  • Yes I'm using Microsoft SQL Server Management Studio Commented Nov 30, 2018 at 15:32

2 Answers 2

1

You need yo execute your dynamic script with simply using EXEC(). Also you need to put FROM to your select, use ORDER BY instead of GROUP BY to order your query. Her is your corrected script :

With using EXEC() :

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
exec(@SQLScript)

With using sp_executesql. Be aware, you cannot use VARCHAR to execute your query with sp_executesql . You must use NVARCHAR(or ntext,nchar).

Declare 
@SQLScript NVARCHAR(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
EXECUTE sp_executesql @SQLScript
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for correcting the script. Although, I would recommend that they use sp_executesql instead of just exec for the reasons listed here: blogs.msdn.microsoft.com/turgays/2013/09/17/…
and this is all assuming they're using SQL Server :)
@StuartAinsworth I will update the script for both solutions. Thanks for double checking.
1

Just use

execute sp_executesql @SQLScript

to execute the query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.