Solved

How to loop through a SQL column

Posted on 2010-11-30
3
422 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 36
SSRS 2013 - Creating a summarized report 19 35
T-SQL:  Collapsing 9 25
T-SQL: New to using transactions 9 30
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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

778 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