tags:

views:

217

answers:

4

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
I'm sorry I don't see how this is useful.
Nathan W
Hmm, maybe you didn't read the question (what there is of it).
Lance Roberts
+1 Why is this modded down?
Andomar
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
@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
not useful..i want to print character by character of 5 rows without using cursor
The answer does not contain misleading information and it's trying to be helpful. I think the downvote is misguided.
Andomar
@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
+2  A: 

If you really want to loop through the rows, you need cursor.

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
without a cursor please
@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
This is a general approach for looping through rows.... If you doesn't like it somebody would....
Svetlozar Angelov
Colin: Point taken..i didn't mean to sound rude
+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
A: 

please try this

Sequentially Through a Set of Records T-SQL

2) loop-through-select-statement

amexn
please give more details about your question
amexn
try this examplehttp://www.databasejournal.com/features/mssql/article.php/3111031/T-SQL-Programming-Part-3---Processing-Sequentially-Through-a-Set-of-Records.htm
amexn