I have a SQL Server table with 2 columns, Code and CodeDesc. I want to use T-SQL to loop through the rows and print each character of CodeDesc. How to do it?
+1
A:
SELECT CodeDesc FROM Table1
Then print all the returned Data.
(Of course you don't use T-SQL to do the printing)
Lance Roberts
2009-09-14 06:52:27
I'm sorry I don't see how this is useful.
Nathan W
2009-09-14 06:53:35
Hmm, maybe you didn't read the question (what there is of it).
Lance Roberts
2009-09-14 06:54:19
+1 Why is this modded down?
Andomar
2009-09-14 06:56:59
I read the question but he/she asked how to loop though the rows and print the values, and your answer just shows how to select results not loop or print.
Nathan W
2009-09-14 06:58:27
@Andomar, probably because the question asks to "print each character of CodeDesc" ... I didn't downvote because the OP's question is worded so poorly, but it seems clear that this answer is solving a different problem.
overslacked
2009-09-14 06:58:45
not useful..i want to print character by character of 5 rows without using cursor
2009-09-14 07:04:12
The answer does not contain misleading information and it's trying to be helpful. I think the downvote is misguided.
Andomar
2009-09-14 07:05:19
@Nathan W T-SQL is not (should not) be used for the presentation of data. T-SQL should be used for the querying of data.
Colin Mackay
2009-09-14 07:05:59
+2
A:
If you really want to loop through the rows, you need cursor.
DECLARE @temp YOURTYPE
DECLARE c CURSOR
FOR SELECT CodeDesc
FROM authors
OPEN c
FETCH NEXT FROM c
INTO @temp
WHILE @@FETCH_STATUS = 0
BEGIN
-do something with temp
FETCH NEXT INTO @temp
END
CLOSE c
DEALLOCATE c
Svetlozar Angelov
2009-09-14 06:53:40
@Parry Don't be so terse in your response - It is makes you sound rude. Why don't you want to use a cursor? As far as I can see it is the best fit solution so far if you are determined to do all of this in T-SQL
Colin Mackay
2009-09-14 07:07:55
This is a general approach for looping through rows.... If you doesn't like it somebody would....
Svetlozar Angelov
2009-09-14 07:14:19
+1
A:
Since this sounds like homework I'm going to tell you how to go about doing it instead of giving you code that does it:
In a WHILE loop, use SUBSTRING to get and print the character. To find the length of the string, use the LEN function.
overslacked
2009-09-14 06:54:56
A:
amexn
2009-09-14 06:57:35