Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

T-SQl Array/Cursor

Status
Not open for further replies.

RobS23

Programmer
Jun 4, 2001
161
GB


I think it's pretty clear what I'm up to. Essentially I want the cursor to act
like an recordset. This example is the essence of it - I will be doing a little
more to the string so, take my word for it, it isn't just the a case of throwing
away the Cursor and building a string. Also note that I am not selecting from a
table, rather, I will be building strings that need to be stored. In VB this
would be an array where each item is later concatenated with another string.

DECLARE @Row varchar(10)
DECLARE @Output varchar(50)
DECLARE @i smallint

---I'll be looping here adding values
select @i = 0

while @i <>6

begin

DECLARE Cursor1 CURSOR FOR
SELECT 'Row' + @i


select @i = @i +1
end


---Then I'll be building a string from the stored values

OPEN Cursor1

FETCH NEXT FROM Cursor1
INTO @Row


WHILE @@FETCH_STATUS < 0
BEGIN

FETCH NEXT FROM Cursor1

INTO @Row

select @Output = @Output + @row /*Obviously there will be
additional stuff here*/




END
-- Finally I do 'stuff' with the concatenated string
print @Output

CLOSE Cursor1
DEALLOCATE Cursor1
 
I see lots of errors in your procedure. However, I see no need for a cursor to do what you want. So here is a more efficient script.

DECLARE @Row varchar(10)
DECLARE @Output varchar(50)
DECLARE @i smallint

---I'll be looping here adding values
select @i = 0

while @i <> 6
begin
Set @row='Row' +ltrim(str(@i))

Select @Output = @Output + @row

-- Obviously there will be additional stuff here

Set @i = @i +1
end

-- Finally I do 'stuff' with the concatenated string

print @Output
Terry L. Broadbent
Programming and Computing Resources
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top