Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 I need to combine data-retrieve with variable

Author  Topic 

eurob
Posting Yak Master

100 Posts

Posted - 2005-08-31 : 12:38:57
declare @a int
select clientid,@a from mytable

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

How can be worked around this issue ?


Thanks




robert

SamC
White Water Yakist

3467 Posts

Posted - 2005-08-31 : 14:30:20
SELECT @a = ClientID FROM MyTable
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2005-08-31 : 14:59:45
sorry,

i forgot something

declare @a int
select clientid,@a = sum(items) from mytable

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

robert
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-08-31 : 16:01:55
Gotta use two steps. It makes sense.

SELECT @a = sum(items) from MyTable

SELECT ClientID FROM MyTable

Take a look and think about why doing both in a single step is a conflict.
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2005-09-01 : 07:53:55
But I need the sum grouped by client, so I need to have it in one select.



robert
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-01 : 08:43:56
Try this

SELECT @a = sum(items) from MyTable where Clienid=someId


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

anandc
Starting Member

20 Posts

Posted - 2005-09-01 : 08:44:35
You may use the query like this...
SELECT clientid, SUM(items) as A FROM mytable

- Anand
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-01 : 08:50:53
quote:
Originally posted by anandc

You may use the query like this...
SELECT clientid, SUM(items) as A FROM mytable

- Anand


That should be
SELECT clientid, SUM(items) as A FROM mytable group by Clientid

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2005-09-01 : 10:04:18

quote:

SELECT @a = sum(items) from MyTable
where Clientid=someId


This does not work, I get the same error msg as I got with my first post. I guess I would have to work with a temp table ? I need to use variables because within the select I want to add and subtract later on.



robert
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-01 : 10:11:25
Can you post some sample data and the result you want?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-01 : 10:57:30
What's the problem you are wanting to solve?

When I look at your example:

declare @a int
select clientid,@a = sum(items) from mytable

it looks like you are trying to output "clientid", but "keep" the @a total for some further processing - and at that point I am sure I have misunderstood your problem!!

Kristen
Go to Top of Page

ke.neelima
Starting Member

6 Posts

Posted - 2008-12-11 : 06:19:03
Check out the below link..
http://www.sql-server-helper.com/error-messages/msg-141.aspx
Hope its useful..!


Neelima
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-11 : 07:51:38
quote:
Originally posted by ke.neelima

Check out the below link..
http://www.sql-server-helper.com/error-messages/msg-141.aspx
Hope its useful..!


Neelima


Yes. Already suggested.

How did you find out this old thread?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

shijobaby
Starting Member

44 Posts

Posted - 2009-08-21 : 09:31:05
Hi

Actually this in the group of small errors consuming time

Just have aook on my blog

http://sqlerrormessages.blogspot.com/2009/08/msg-141-select-statement-that-assigns.html


Happy Programming
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-21 : 09:36:50
quote:
Originally posted by shijobaby

Hi

Actually this in the group of small errors consuming time

Just have aook on my blog

http://sqlerrormessages.blogspot.com/2009/08/msg-141-select-statement-that-assigns.html


Happy Programming


So, you are creating blog entries based on the error messages posted here?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -