title: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
body: <p>What is wrong with this statement?</p>
  
  <pre><code>ALTER Function [InvestmentReturn].[getSecurityMinLowForPeriod](@securityid int,
      @start datetime,
      @end datetime)
  returns xml
  begin
  
  declare @results varchar(500)
  declare @low int
  declare @adjustedLow int
  declare @day varchar(10)
  
  if @end is null
  begin
      set @end = getdate()
  end
  set @adjustedLow = (select min(adjLow) 
                          from (
                              select Low * [InvestmentReturn].[fn_getCorporateActionSplitFactor](isq.securityid, @start, convert(varchar,day, 111)) as adjLow 
                              from 
                                  securityquote isq
                              where isq.securityid = @securityid and isq.day &gt;= convert(varchar(10), @start, 111) and convert(varchar(10), @end, 111) &gt;= isq.day
                              and low != -1 
                              ) as x)
  select 
      top 1 @low = low
      , @day = day
      , @adjustedLow
  --select high
  from 
      securityquote sq
  where 
      day &gt;= convert(varchar(10), @start, 111) and convert(varchar(10), @end, 111) &gt;= day
      and securityid = @securityid and low != -1
  order by low asc
  
      set @results=  '&lt;results type="debug_min"&gt;'
      set @results = @results + '&lt;periodStart&gt;' + coalesce(cast(@start as varchar(20)), 'NULL') + '&lt;/periodStart&gt;' 
      set @results = @results + '&lt;periodEnd&gt;' + coalesce(cast(@end  as varchar(20)), 'NULL') + '&lt;/periodEnd&gt;' 
      set @results = @results + '&lt;securityID&gt;' + coalesce(cast(@securityID as varchar(10)), 'NULL') + '&lt;/securityID&gt;'
      set @results = @results + '&lt;periodMin&gt;' + coalesce(cast(@low as varchar(10)), '-11111') + '&lt;/periodMin&gt;'
      set @results = @results + '&lt;coraxAdjustedPeriodMin&gt;' + coalesce(cast(@adjustedLow as varchar(10)), '-11111') + '&lt;/coraxAdjustedPeriodMin&gt;'
      set @results = @results + '&lt;dayMinOcurred&gt;' + coalesce(@day, 'NULL') + '&lt;/dayMinOcurred&gt;'
      set @results = @results + '&lt;/results&gt;'
  
  return @results
  </code></pre>
  
  <p>Just to explain the answer (after getting where the error was caused), I simply removed @adjustedLow from the second select statement.</p>