I’ve got to write a test that requires large amounts of data to be stored in a text column.
When I try this (insert 2 billion X characters):
INSERT INTO table VALUES ( REPLICATE('X', 2000000000) )
This is what I get:
SELECT *, DATALENGTH(textCol) FROM table
XXXXXXXXXXXXX…. 8000
I was hoping for more than 8000. Any ideas where I’m going wrong? My google-fu is failing me.
Caveat: Yes, text columns are deprecated. I’m sure there are lots of very valid and sensible reasons why it’s a bad idea to want to use them as bulk data stores. Assume I’m dealing with a legacy system that happens to have text columns storing large amounts of bulk data and I have to write tests to figure out how my bit of third party code can deal with that.
The
REPLICATEfunction returns data in the same type as was passed in. Because you’re passing in a normal quoted string, that creates avarchar(not to be confused with avarchar(MAX)), which has a maximum capacity of 8000 characters.Try this:
I can’t let you go without the obligatory recommendation that you use
varchar(MAX)(ornvarchar(MAX)) columns instead oftext, but this should do the trick. I’m not in front of SSMS right now, but you may have toconvert(text, REPLICATE(convert(varchar(MAX), 'X'), 2000000000)), but I don’t think so. Either way, this should do the trick.