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 2005 Forums
 Transact-SQL (2005)
 var must not be combined with data-retrieval opera

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2009-04-07 : 02:17:16
Dear All,

I'm getting error
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.


DECLARE @customer varchar(200)
DECLARE @r VARCHAR(8000)

DECLARE ActiveCustomers Cursor FOR
SELECT distinct Customer
FROM tabCustomers

-- Open the cursor
OPEN ActiveCustomers
-- Fetch the first row of the cursor and assign its values into variables
FETCH NEXT FROM ActiveCustomers INTO @Customer
-- perform action whilst a row was found
WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @Customer=customer,@r = ISNULL(@r+',', '')
+ branch
FROM tabCustomers
WHERE customer = @Customer
SELECT @Customer=customer,Branch=@r

-- get next row of cursor
FETCH NEXT FROM ActiveCustomers INTO @Customer
END
-- Close the cursor to release locks
CLOSE ActiveCustomers
-- Free memory used by cursor
DEALLOCATE ActiveCustomers



Thank You.

Regards,
Michelle

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 02:48:31
See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254
to make this work without a CURSOR.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2009-04-07 : 03:09:12
Dear Peso,

Thank you for your kindly reply. I'm using MS SQL 2000 & xml doesn't work.

I'm actually wanna to learn how to use cursor method; I know i can solved this situation by using function.

Can anyone help me on Cursor Method? And what is the dis-advantages of using cursor??

Please advise.

Thank You.

Regards,
Michelle
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 03:55:13
I thought you were using SQL Server 2005 because you posted in a SQL Server 2005 forum.
Please repost your question in a SQL Server 2000 forum and you may getter answer there.
http://www.sqlteam.com/forums/forum.asp?FORUM_ID=12



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2009-04-07 : 20:48:33
Dear Peso,

Thank you. I finally figure out the solution.

Here are the solution by using cursor.


/*--Create & Dump data
create table tabCustomers
(
Customer varchar(100),
Branch varchar(100)
)

insert into tabCustomers (Customer,Branch) values ('AAA','Delhi')
insert into tabCustomers (Customer,Branch) values ('AAA','Delhi1')
insert into tabCustomers (Customer,Branch) values ('AAA','Delhi2')
insert into tabCustomers (Customer,Branch) values ('ABB','Delhi')
insert into tabCustomers (Customer,Branch) values ('ABB','Delhi4')
insert into tabCustomers (Customer,Branch) values ('ABB','Delhi5')
insert into tabCustomers (Customer,Branch) values ('AAC','Delhi9')
insert into tabCustomers (Customer,Branch) values ('AAD','Delhi10')
insert into tabCustomers (Customer,Branch) values ('AAD','Delhi11')
insert into tabCustomers (Customer,Branch) values ('ACB','Delhi6')
insert into tabCustomers (Customer,Branch) values ('ACB','Delhi7')
insert into tabCustomers (Customer,Branch) values ('ACB','Delhi8')
--*/

DECLARE @customer varchar(20)
DECLARE merge_cursor CURSOR FAST_FORWARD FOR
SELECT distinct customer FROM tabCustomers
DECLARE @r VARCHAR(8000)


OPEN merge_cursor
declare @temp table
(
Customer varchar(100),
Branch varchar(200)
)

FETCH NEXT FROM merge_cursor INTO @customer

WHILE @@FETCH_STATUS = 0
BEGIN
set @r=''
SELECT @customer=customer,
@r = ISNULL(@r+',', '')
+ branch

FROM tabCustomers
WHERE customer = @customer

insert into @temp
SELECT @customer,@r


FETCH NEXT FROM merge_cursor INTO @customer
END

select customer,branch=right(branch,(len(branch)-1)) from @temp

CLOSE merge_cursor
DEALLOCATE merge_cursor
Go to Top of Page

shijobaby
Starting Member

44 Posts

Posted - 2009-08-21 : 09:29:38
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
   

- Advertisement -