dbTalk Databases Forums  

Storing variable values whilst using cursors

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Storing variable values whilst using cursors in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
archanapatelwhite@googlemail.com
 
Posts: n/a

Default Storing variable values whilst using cursors - 02-20-2007 , 07:41 PM






Hi below is the code I am using.

------------------------------------
SET NOCOUNT ON

DECLARE @emailid varchar(50), @rastype varchar(50),
@message varchar(80)
declare @allrastypes varchar(200)

DECLARE email_cursor CURSOR FOR
SELECT distinct Email
FROM dbo.tblMaintCustomer
ORDER BY Email

OPEN email_cursor

FETCH NEXT FROM email_cursor
INTO @emailid

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT ' '
SELECT @message = 'Email Address ' +
@emailid

PRINT @message

-- Declare an inner cursor based
-- on vendor_id from the outer cursor.

DECLARE rastype_cursor CURSOR FOR
SELECT distinct [RasType]
FROM dbo.tblMaintCase x, dbo.tblMaintCustomer y
WHERE x.caseid = y.caseid AND
y.Email = @emailid
and RasType is not null

OPEN rastype_cursor
FETCH NEXT FROM rastype_cursor INTO @rastype
select @allrastypes = @allrastypes + ',' + @rastype

IF @@FETCH_STATUS <> 0
PRINT ' <<None>>'

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @message = @rastype
PRINT @message
select @allrastypes = @allrastypes + ',' + @rastype
FETCH NEXT FROM rastype_cursor INTO @rastype

END

CLOSE rastype_cursor
DEALLOCATE rastype_cursor

insert into dbo.tblTest values(@emailid,@allrastypes)
select @allrastypes = ''
FETCH NEXT FROM email_cursor
INTO @emailid
END
CLOSE email_cursor
DEALLOCATE email_cursor
--------------------------------------

I basically want the value of @allrastypes to accumulate each time it
loops through, which is is not doing.

The result I get is :

Email Address xx (AT) xx (DOT) NET
G5R
(for here i want @allrastypes to be 'G5R,')

Email Address yy (AT) yY (DOT) ORG
G1
G3
G5O

(for here i want @allrastypes to be 'G1,G3,G5O')

Can someone help

Thanks
Archana


Reply With Quote
  #2  
Old   
Russ Rose
 
Posts: n/a

Default Re: Storing variable values whilst using cursors - 02-20-2007 , 09:36 PM






Set your strings equal to empty strings immediately after declaring them.
Otherwise you are adding a string to a NULL which yields a NULL.

Not sure all you are trying to accomplish, but the following is better for
building comma delimited strings than a cursor.

DECLARE @allrastypes varchar(200)
SELECT @allrastypes = ''

SELECT @allrastypes = @allrastypes + ',' + T.RasType
FROM (SELECT DISTINCT RasType
FROM dbo.tblMaintCase x,
dbo.tblMaintCustomer y
WHERE x.caseid = y.caseid AND
y.Email = @emailid
AND RasType is not null) T

--Drop first comma
SELECT @allrastypes = SUBSTRING(@allrastypes, 2, 200)


<archanapatelwhite (AT) googlemail (DOT) com> wrote

Quote:
Hi below is the code I am using.

------------------------------------
SET NOCOUNT ON

DECLARE @emailid varchar(50), @rastype varchar(50),
@message varchar(80)
declare @allrastypes varchar(200)

DECLARE email_cursor CURSOR FOR
SELECT distinct Email
FROM dbo.tblMaintCustomer
ORDER BY Email

OPEN email_cursor

FETCH NEXT FROM email_cursor
INTO @emailid

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT ' '
SELECT @message = 'Email Address ' +
@emailid

PRINT @message

-- Declare an inner cursor based
-- on vendor_id from the outer cursor.

DECLARE rastype_cursor CURSOR FOR
SELECT distinct [RasType]
FROM dbo.tblMaintCase x, dbo.tblMaintCustomer y
WHERE x.caseid = y.caseid AND
y.Email = @emailid
and RasType is not null

OPEN rastype_cursor
FETCH NEXT FROM rastype_cursor INTO @rastype
select @allrastypes = @allrastypes + ',' + @rastype

IF @@FETCH_STATUS <> 0
PRINT ' <<None>>'

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @message = @rastype
PRINT @message
select @allrastypes = @allrastypes + ',' + @rastype
FETCH NEXT FROM rastype_cursor INTO @rastype

END

CLOSE rastype_cursor
DEALLOCATE rastype_cursor

insert into dbo.tblTest values(@emailid,@allrastypes)
select @allrastypes = ''
FETCH NEXT FROM email_cursor
INTO @emailid
END
CLOSE email_cursor
DEALLOCATE email_cursor
--------------------------------------

I basically want the value of @allrastypes to accumulate each time it
loops through, which is is not doing.

The result I get is :

Email Address xx (AT) xx (DOT) NET
G5R
(for here i want @allrastypes to be 'G5R,')

Email Address yy (AT) yY (DOT) ORG
G1
G3
G5O

(for here i want @allrastypes to be 'G1,G3,G5O')

Can someone help

Thanks
Archana




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.