locked
looping on varchar data type in sql RRS feed

  • Question

  • hi

    i have to do some operation using looping. Can we do this on non-numeric data.

    Please help me with this.

    this should be done in sql.

    Thanks.

    Thursday, August 18, 2011 11:05 AM

Answers

  • Some thing like this

    ------

    set nocount on

    Declare @tab table(name varchar(20))

    Insert into @tab

    Values('ram'),( 'raj'),( 'kiran'),( 'sekhar'), ('ram'),( 'kiran'), ('raj')

    Declare @name varchar(20)

    Declare C cursor for select distinct name from @tab order by 1 

    open C

    fetch next from c into @name

    while @@FETCH_STATUS = 0

    BEGIN

    /*

    You can put your required logic here 

    */

    print @name 

    fetch next from c into @name

    end

    close C

    deallocate C


    http://uk.linkedin.com/in/ramjaddu
    • Marked as answer by Sudeep Puvvadi Thursday, August 18, 2011 11:46 AM
    Thursday, August 18, 2011 11:40 AM

All replies

  • Could you provide us sample?


    Balmukund Lakhani | Please mark solved if I've answered your question, vote for it as helpful to help other user's find a solution quicker
    --------------------------------------------------------------------------------
    This posting is provided "AS IS" with no warranties, and confers no rights.
    --------------------------------------------------------------------------------
    My Blog | Team Blog | @Twitter
    Thursday, August 18, 2011 11:13 AM
  • consider as there will be some names like

    ram, raj, kiran, sekhar, ram, kiran, raj ...................

    In this way there will be many duplicates. so for all distinct names i should do some operation ( ex: print x + y; ).

    Thursday, August 18, 2011 11:27 AM
  • Some thing like this

    ------

    set nocount on

    Declare @tab table(name varchar(20))

    Insert into @tab

    Values('ram'),( 'raj'),( 'kiran'),( 'sekhar'), ('ram'),( 'kiran'), ('raj')

    Declare @name varchar(20)

    Declare C cursor for select distinct name from @tab order by 1 

    open C

    fetch next from c into @name

    while @@FETCH_STATUS = 0

    BEGIN

    /*

    You can put your required logic here 

    */

    print @name 

    fetch next from c into @name

    end

    close C

    deallocate C


    http://uk.linkedin.com/in/ramjaddu
    • Marked as answer by Sudeep Puvvadi Thursday, August 18, 2011 11:46 AM
    Thursday, August 18, 2011 11:40 AM
  • thanks a lot RamJaddu
    • Marked as answer by Sudeep Puvvadi Thursday, August 18, 2011 11:46 AM
    • Unmarked as answer by Sudeep Puvvadi Thursday, August 18, 2011 11:46 AM
    Thursday, August 18, 2011 11:46 AM