?
Solved

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

Posted on 2013-01-23
3
Medium Priority
?
3,105 Views
Last Modified: 2013-01-24
I've been programming SLQ Server queries for years and I believe this is the first time I've seen this error. I have the query show below, two select statements. The first select runs just fine and returns values. When the 2nd select executes I get the error:

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

I don't get this message at all. The first select also does a data retrieval operation so just the fact that a data retrieval operation is being performed must not be the whole problem. If I remove the ',@SURpensionTotal = ' bit, it runs OK (but that doesn't do me much good).

What am I doing wrong? Please help!

declare @memberId varchar(11),
            @retireeId varchar(11),
            @retireePension money,
           @SURpensionTotal money

set @memberId = '048011'

select @retireeId = m.retireeId, @retireePension = r.retireePension
from members m
join (select e.memberId, sum(e.amount) retireePension
  from memberEarnings e
  join earningCodes c on c.earningCode = e.earningCode and e.earningCode in ('DRP','PEN')
  where e.enabled = 1 and e.oneShot = 0
  group by e.memberId) r on r.memberId = m.retireeId
where m.memberId = @memberId

select m.retireeId,@SURpensionTotal = sum(e.amount)
from members m
join memberEarnings e on e.memberId = m.memberId and e.enabled = 1 and e.oneShot = 0
  and e.earningCode in ('DRP','PEN')
where (m.terminationDate is null 
  or (dbo.firstOfThisMonth(default) <= m.terminationDate and dbo.firstOfNextMonth(default) > m.terminationDate))
  and m.retireeId = @retireeId
group by m.retireeId

Open in new window

0
Comment
Question by:jmarkfoley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 1600 total points
ID: 38813146
you cannot combine both assigning value to a variable and data selection in same select statement. you can change your second sql to

select @SURpensionTotal = sum(e.amount)
from members m
join memberEarnings e on e.memberId = m.memberId and e.enabled = 1 and e.oneShot = 0
  and e.earningCode in ('DRP','PEN')
where (m.terminationDate is null
  or (dbo.firstOfThisMonth(default) <= m.terminationDate and dbo.firstOfNextMonth(default) > m.terminationDate))
  and m.retireeId = @retireeId
group by m.retireeId
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 38814692
Or to:

SELECT @retireeId = m.retireeId, @SURpensionTotal = sum(e.amount)


"Data retrieval" in this case means data retrieved to return to the caller.  Variable assignments never return data to the caller.

For example, a stored proc returns a result set to the caller.
So:

SELECT m.retireeId, sum(e.amount)
FROM ...

in a stored proc would return that result to the caller, whereas:

SELECT @var1 = m.retireeId, @var2 = sum(e.amount)
FROM ...

would not.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 38817310
duh! Of course! That makes total sense. I've probably not seen this error before because I haven't made this mistake before. ScottPletcher's solution is what I did in my 1st query. What I need for this query is appari's solution. I don't need to return the retireeId again. I guess I just stuck it in the SELECT out of habit of putting GROUP BY columns in the select list (but that's not needed). Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question