Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

sql server - A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations

What is wrong with this statement?

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 >= convert(varchar(10), @start, 111) and convert(varchar(10), @end, 111) >= isq.day
                            and low != -1 
                            ) as x)
select 
    top 1 @low = low
    , @day = day
    , @adjustedLow
--select high
from 
    securityquote sq
where 
    day >= convert(varchar(10), @start, 111) and convert(varchar(10), @end, 111) >= day
    and securityid = @securityid and low != -1
order by low asc

    set @results=  '<results type="debug_min">'
    set @results = @results + '<periodStart>' + coalesce(cast(@start as varchar(20)), 'NULL') + '</periodStart>' 
    set @results = @results + '<periodEnd>' + coalesce(cast(@end  as varchar(20)), 'NULL') + '</periodEnd>' 
    set @results = @results + '<securityID>' + coalesce(cast(@securityID as varchar(10)), 'NULL') + '</securityID>'
    set @results = @results + '<periodMin>' + coalesce(cast(@low as varchar(10)), '-11111') + '</periodMin>'
    set @results = @results + '<coraxAdjustedPeriodMin>' + coalesce(cast(@adjustedLow as varchar(10)), '-11111') + '</coraxAdjustedPeriodMin>'
    set @results = @results + '<dayMinOcurred>' + coalesce(@day, 'NULL') + '</dayMinOcurred>'
    set @results = @results + '</results>'

return @results

Just to explain the answer (after getting where the error was caused), I simply removed @adjustedLow from the second select statement.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Column values from the SELECT statement are assigned into @low and @day local variables; the @adjustedLow value is not assigned into any variable and it causes the problem:

The problem is here:

select 
    top 1 @low = low
    , @day = day
    , @adjustedLow  -- causes error!
--select high
from 
    securityquote sq
...

Detailed explanation and workaround: SQL Server Error Messages - Msg 141 - A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...