473,769 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop with Read Only cursor?

Ed
Hello,

I posted a question about looping with Select in a While
loop, a few days ago. Repliers to my post advised me that
a Cursor would be much better (thanks all for your
replies). I found a looping example using cursors on the
net, but I don't know how to declare a read only cursor.
Note: my objective is to simulate an array of field names
for creating an xml doc/string (to use with OpenXML
function). So I created a small table with 22 field names
(22 rows). Originally, I had 2 columns - a RowNum col and
a fldName col. But with the following example using a
Cursor, it seems that I only need 1 column, the fldName
col, although this seems like more lines of code than the
Select method. The cursor example follows. The question
is "How to declare a read only cursor". I also included
my Select example - actually the full UDF using Select in
the While loop - if anyone could advise me between the two
examples which would be more suited for my situation.

Cursor Example
-----------------------------------------------------
declare @fldName varchar(50)
declare @RowNum int
declare fldList cursor for
select fldName from tblflds
OPEN fldList
FETCH NEXT FROM fldList
INTO @fldName
set @RowNum = 0
WHILE @@FETCH_STATUS = 0
BEGIN
set @RowNum = @RowNum + 1
print cast(@RowNum as char(2)) + ' ' + @fldName
FETCH NEXT FROM fldList
INTO @fldName
END
CLOSE fldList
DEALLOCATE fldList
--------------------------------------------------------

UDF using Select in While loop Example
----------------------------------------------------
CREATE FUNCTION ConvertToXML( @str1 varchar (8000))
--@str1 contains a list of values from external source
--UDF will create xml string by adding a fieldname to
--each value from @str1, comma delimited
returns varchar (8000)

as

begin

declare @fld varchar(255)
declare @fldNum int
declare @rownum int --used to increment Select statement
declare @xml varchar(8000) --resultant xml string
declare @val varchar(255) --each value in @str1
declare @pos1 int --get comma delimeter position of @str1
declare @pos2 int --get comma delimeter position of @str1

set @xml = '<ROOT><Worktab le '
set @rownum = 0
set @pos1 = 1
select top 1 @fldNum = rownum, @fld = fldName from tblflds
while @rowNum < 22
begin
set @pos2 = charindex(',', @str1, @pos1)
set @val = substring(@str1 , @pos1, @pos2 - @pos1)
set @xml = @xml + @fld + '="' + @val + '" '
select top 1 @fldNum = rownum, @fld = fldName from
tblflds where rownum > @fldnum
set @rowNum = @rownum + 1
set @pos1 = @pos2 + 1
End
set @xml = substring(@xml, 1, len(@xml))
set @xml = @xml + '/></ROOT>'

return @xml

end
----------------------------------------------------------

This UDF works fine with the Select loop. But if this is
a kludge and the cursor method would be more
correct/professional/better habit to be in, please
advise. I am also open to suggestions if there is an
easier way to parse @str1 or create @xml.

Thanks,
Ed

Jul 19 '05 #1
0 1511

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

Similar topics

6
12216
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been a C programmer since I was 14, so a construct like: while info = mydbcursor.fetchone(): print "Information: "+str(info)
3
3354
by: r rk | last post by:
I am trying to write a utility/query to get a report from a table. Below is the some values in the table: table name: dba_daily_resource_usage_v1 conn|loginame|dbname|cum_cpu|cum_io|cum_mem|last_batch ------------------------------------------------------------ 80 |farmds_w|Farm_R|4311 |88 |5305 |11/15/2004 11:30 80 |abcdes_w|efgh_R|5000 |88 |4000 |11/15/2004 12:30 45 |dcp_webu|DCP |5967 |75 |669 |11/16/2004...
4
10750
by: bourgon | last post by:
Working on some new code, I'm coming across WHILE loops used instead of cursors. I was curious if anyone had any stats on how the speed of doing this compares to the speed of a cursor. I typically avoid cursors for performance sake, but I'm not sure how this avoids the speed hit of a cursor, since it's doing essentially the same thing. Many thanks.
15
2678
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
2
3404
by: satishchandra999 | last post by:
I have SP, which has a cursor iterations. Need to call another SP for every loop iteration of the cursor. The pseudo code is as follows.. Create proc1 as Begin Variable declrations... declare EffectiveDate_Cursor cursor for select field1,fld2 from tab1,tab2 where tab1.effectivedate<Getdate()
2
23123
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint DECLARE @intPeriod smallint DECLARE @strTekst varchar(50)
14
5907
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
1
5774
by: yeohyc | last post by:
Hi All, I have a problem here. I have done a request for move order it was stored into a custom table with the format: (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-01(Q)2500 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-02(Q)2500 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-03(Q)2500 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-04(Q)2500 System Reads in (O) = Move Order
5
9253
by: t_rectenwald | last post by:
I have a python script that uses the cx_Oracle module. I have a list of values that I iterate through via a for loop and then insert into the database. This works okay, but I'm not sure whether I can use one cursor for all inserts, and define it outside of the loop, or instantiate and close the cursor within the loop itself. For example, I have: for i in hostlist: cursor = connection.cursor() sql= "insert into as_siebel_hosts_temp...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.