Solved

How to loop through a SQL column

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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