title: Scalar variable must be declared in SQL variable
body: <p>I'm creating a report using sql scripts through management studio and I'm getting the error " Must Declare the scalar variable "@Account". I've been reading other similar questions on this portal but they are related to c#</p>
  
  <p>I'm currently trying to reduce the code on the script so I decided to put a sql script into a variable because depending on a condition the where condition will change. Below is an example of the code</p>
  
  <pre><code>Declare @Account int = 1 , @SQL varchar(max)=''
  Select @SQL = N'Select ColumnA,ColumnB, ColumnC from Table1 where ColumnA =1'
  
  if @Account IS NULL
  Begin
  exec(@SQL)
  end
  
  --Here is where the error is hapening
  else
  begin
  --This is the line causing the error
  Select @SQL = @SQL + 'AND ColumnB=@Account"
  exec(@SQL)
  end
  </code></pre>
  
  <p>If I type manually the value of the variable next to "ColumnB=" it works but the account number will be selected by the user executing the script. I'm thinking on maybe building a temp table to capture the variable value and then do a sub query on the where condition but maybe the solution to this error may be more easier</p>