473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Storing variable values whilst using cursors

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@xx.NET
G5R
(for here i want @allrastypes to be 'G5R,')

Email Address yy@yY.ORG
G1
G3
G5O

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

Can someone help

Thanks
Archana

Feb 21 '07 #1
1 3739
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)
<ar***************@googlemail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
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@xx.NET
G5R
(for here i want @allrastypes to be 'G5R,')

Email Address yy@yY.ORG
G1
G3
G5O

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

Can someone help

Thanks
Archana

Feb 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sean | last post by:
HI, I have a shopping cart in which I am trying to use breadcrumb style navgiation. I need to be able to display the categroy heading relating to the section of the site a visitor is using i.e...
2
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. ...
2
by: Mark Hannon | last post by:
I am trying to wrap my brain around storing form elements inside variables & arrays before I move on to a more complicated project. I created this simple example to experiment and as far as I can...
3
by: mahurshi | last post by:
i'd like to store values of a signal into a variable called signal. signal can take 0, 1, -1, U (where u = undefined, -1 = uninitialized) is there a way i can store this without making the...
4
by: s99999999s2003 | last post by:
hi the database "execute" function returns a list of logical results. Each logical result is a list of row tuples, as explained in the documents. everytime i use it to execute various...
3
by: Brad | last post by:
I am storing an array which contains about a dozen chracter items to a Session variable. Later, I need to use this array so I am doing the following: Dim eventTypes As String() =...
1
by: None | last post by:
Hi, I have developed webshop application using asp.net 1.1. I'm using DataGrid in one of the pages of my site. During the page load the DataGrid will be binded by around 7500 products(rows). At...
1
by: Sigmazen | last post by:
Hi I am looking in to utilising the new functionality of DPSIs on zOS UDB (DB2 v8), but I have a question regarding the following scenario. - The first table is an Account table whose columns...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.