Solved

How to loop through a SQL column

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

13 Experts available now in Live!

Get 1:1 Help Now