Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 350
Ramy Avatar asked Jan 05 '11 19:01

Ramy


People also ask

How do you assign a value to a variable in select query?

When a variable is first declared, its value is set to NULL. To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

What is data retrieval operations SQL Server?

Data retrieval means obtaining data from a Database Management System (DBMS) such as ODBMS. In this case, it is considered that data is represented in a structured way, and there is no ambiguity in data. In order to retrieve the desired data the user present a set of criteria by a query.

Can you declare a variable in a select statement?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How will you store select query result in variable in SQL Server?

This provides a way to save a result returned from one query, then refer to it later in other queries. The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving.


2 Answers

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.

like image 110
Marek Grzenkowicz Avatar answered Nov 11 '22 12:11

Marek Grzenkowicz


You cannot use a select statement that assigns values to variables to also return data to the user The below code will work fine, because i have declared 1 local variable and that variable is used in select statement.

 Begin     DECLARE @name nvarchar(max)     select @name=PolicyHolderName from Table     select @name     END 

The below code will throw error "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations" Because we are retriving data(PolicyHolderAddress) from table, but error says data-retrieval operation is not allowed when you use some local variable as part of select statement.

 Begin     DECLARE @name nvarchar(max)     select         @name = PolicyHolderName,        PolicyHolderAddress      from Table  END 

The the above code can be corrected like below,

Begin     DECLARE @name nvarchar(max)     DECLARE @address varchar(100)     select         @name = PolicyHolderName,        @address = PolicyHolderAddress      from Table END 

So either remove the data-retrieval operation or add extra local variable. This will resolve the error.

like image 22
Govind Avatar answered Nov 11 '22 12:11

Govind