Solved

How to loop through a SQL column

Posted on 2010-11-30
3
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

749 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