Solved

How to loop through a SQL column

Posted on 2010-11-30
3
414 Views
Last Modified: 2012-06-21
Hi Guys

I have a table called TempMemo.
It consists of 2 columns: AccountId varchar(50)
                                        Memo text

I need to copy the data into a another table called SLMemo.

It consists of 2 columns: AccountId varchar(50)
                                        Memo varchar(1000)

The memo filled in Tempmemo will have more than 1000 characters.

I need all the data to come in.

So if a row in Memo in TempMemo has 5000 characters  then what should be imported:

5 lines of 1000 with the same Accountid.

Remebering the character len will be different.
I hope this makes sense.

Cheers
Shezad

0
Comment
Question by:Shezad Ahmed
  • 2
3 Comments
 
LVL 7

Expert Comment

by:rmm2001
ID: 34240336
There's probably better ways to do this but...
DECLARE @count INT
DECLARE @memoLen INT
DECLARE @memo TEXT
DECLARE @AccountID VARCHAR(50)
SET @count = (SELECT COUNT(9) FROM TempMemo)

WHILE (@count > 0)
  SELECT @AccountID = AccountID, @memo = Memo, @memoLen = LEN(Memo)
  FROM TempMemo
 
  WHILE (@MemoLen > 0)
    INSERT NewTable (AccountID, Memo)
    VALUES @AccountID, LEFT(Memo, 1000)
   
    SET @memoLen = @memoLen - 1000
  END
 
  SET @count = @count - 1
END  
0
 
LVL 8

Accepted Solution

by:
raulggonzalez earned 500 total points
ID: 34252770
Hi,

The solution I've got is using a cursor, it's well known that cursors are not the best, but it works.

Hope it helps.


DECLARE @TempMemo 

	TABLE (AccountID VARCHAR(50),

			Memo TEXT)



DECLARE @SLMemo 

	TABLE (AccountID VARCHAR(50),

			Memo VARCHAR(1000))





INSERT INTO @TempMemo

VALUES(

'account1',

'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'



)



INSERT INTO @TempMemo

VALUES(

'account2',

'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'



)





DECLARE @AccountID VARCHAR(50)

DECLARE @Memo VARCHAR(MAX) -- SQL 2005 doesn't allow TEXT variables, so I created VARCHAR(MAX)

DECLARE @Memo1000 VARCHAR(1000)

DECLARE @MemoLen INT

DECLARE @start INT  



SET @start = 1



DECLARE cr CURSOR

FORWARD_ONLY 

FOR

SELECT  AccountId , Memo text

FROM @TempMemo



OPEN cr

FETCH NEXT FROM cr INTO @AccountID, @Memo

WHILE @@FETCH_STATUS = 0

BEGIN

   SET @MemoLen = LEN(@Memo)

   IF @MemoLen > 1000

   BEGIN

      WHILE @MemoLen > 0

      BEGIN

         SET @Memo1000 = SUBSTRING(@Memo, @start, 1000)

         INSERT INTO @SLMemo (AccountId ,Memo)

         VALUES (@AccountID, @Memo1000)

         

         SET @start = @start + 1000

         SET @MemoLen = @MemoLen - 1000

         

      END

   END

   

   ELSE

   BEGIN

      INSERT INTO @SLMemo (AccountId ,Memo)

      VALUES (@AccountID, @Memo)

    END



FETCH NEXT FROM cr INTO @AccountID, @Memo



END

CLOSE cr

DEALLOCATE cr



SELECT * FROM @SLMemo

Open in new window

0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34256009
Just realize that can do the same without the IF block...

Cheers
DECLARE @TempMemo 

	TABLE (AccountID VARCHAR(50),

			Memo TEXT)



DECLARE @SLMemo 

	TABLE (AccountID VARCHAR(50),

			Memo VARCHAR(1000))





INSERT INTO @TempMemo

VALUES(

'account1',

'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'



)



INSERT INTO @TempMemo

VALUES(

'account2',

'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'



)





DECLARE @AccountID VARCHAR(50)

DECLARE @Memo VARCHAR(MAX) -- SQL 2005 doesn't allow TEXT variables, so I created VARCHAR(MAX)

DECLARE @Memo1000 VARCHAR(1000)

DECLARE @MemoLen INT

DECLARE @start INT  



SET @start = 1



DECLARE cr CURSOR

FORWARD_ONLY 

FOR

SELECT  AccountId , Memo text

FROM @TempMemo



OPEN cr

FETCH NEXT FROM cr INTO @AccountID, @Memo

WHILE @@FETCH_STATUS = 0

BEGIN

   SET @MemoLen = LEN(@Memo)

      WHILE @MemoLen > 0

      BEGIN

         SET @Memo1000 = SUBSTRING(@Memo, @start, 1000)

         INSERT INTO @SLMemo (AccountId ,Memo)

         VALUES (@AccountID, @Memo1000)

         

         SET @start = @start + 1000

         SET @MemoLen = @MemoLen - 1000

         

      END

   

FETCH NEXT FROM cr INTO @AccountID, @Memo



END

CLOSE cr

DEALLOCATE cr



SELECT * FROM @SLMemo

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now