December 12, 2006 at 12:28 am
Hi,
I have some code that concatinates a string via a cursor into a varchar(8000) variable that then gets saved into the varchar(8000) field. I need to change the field size to be text, however I have not figured out how to concatinate strings that are greater than 8000 chars since there is no local variable of type text that can temporarily hold this data.
All help much appreciated!
Olja
December 12, 2006 at 6:38 am
situations like this is where I switch from doing everything in TSQL, to grabbing the data in .NET or vb6, where the String variable type does not have this limitation;
I'll do the work in a program, and then have the program insert/update the finished results into the TEXT field.
a cursor is the same as a recordset or datatable, and looping thru the whole set is really easy in a programming language.
in 2005, the VARCHAR(MAX) doesn't have the 8000 char handicap, so that's an option as well.
Lowell
December 12, 2006 at 2:40 pm
Lowell, I did consider this! The issue is that a lot of my business logic resides at the db level and moving this particular functionality into asp.net will result in having to shift much of the code from tsql to c#.
December 12, 2006 at 3:10 pm
What about using table variable with text column....
MohammedU
Microsoft SQL Server MVP
December 12, 2006 at 3:15 pm
Mohammed, how would i go about doing this. I have a cursor which I loop through and previously I would just append the text value to the varchar. How would I do this with the text column?
December 12, 2006 at 3:24 pm
I was thinking it can be used in conjuction with UPDATETEXT or WRITETEXT functions... but it all depends on requirement...
Can you post a sample code...
MohammedU
Microsoft SQL Server MVP
December 13, 2006 at 6:19 am
Here's a simple example of how you could do that with UPDATETEXT. Remember that the output length of the text column when displayed in a SELECT statement in Query Analyzer is limit to a max of about 8000 characters. That's why I included datalength() at the end, so you can see the actual length.
CREATE TABLE #temp
(
id int
, data text
)
INSERT #temp (id, data)
SELECT 1, 'This is a row'
UNION ALL
SELECT 2, 'This is row 2.'
UNION ALL
SELECT 3, null
-- Example that appends 1000 lines to a text column
DECLARE @id int
, @count int
, @ptr varbinary(16)
, @updatestring varchar(8000)
, @eoln varchar(3)
SET @eoln = '.' + Char(13) + Char(10)
SET @id = 2 -- arbitrary selection for example purposes only
SELECT @ptr = TextPtr(data)
FROM #temp
WHERE id = @id
SET @count = 0 -- loop counter for this exmaple
WHILE @count < 1000
BEGIN
SET @count = @count + 1
SET @updatestring = 'This is appended line ' + CONVERT(varchar(10), @count) + @eoln
UPDATETEXT #temp.data @ptr null 0 @updatestring
END --WHILE
SELECT id, datalength(data), data FROM #temp
DROP TABLE #temp
December 13, 2006 at 3:02 pm
Thanks for the replies and samples! I managed to get my code updating the text fields as expected.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply