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
2,634 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
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 400 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:ScottPletcher
ScottPletcher earned 100 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now