tags:

views:

352

answers:

5

How can I insert over 8000 bytes into SQL using C#? I have a large amount of text I want to store in a varchar, 8000 bytes seems to be the limit, can I increase this?

Thanks

+6  A: 

which version of SQL are you using? SQL 2008 supports the varchar(MAX) variable which allows a much larger amount of text.

Update: Sorry, that's Microsoft SQL 2005 and greater ... varchar(MAX) can hold 2GB of data .... you didn't specify if you're using MS-SQL so I made an assumption.

Scott Vercuski
MAX is supported from SQL 2005
AdaTheDev
Hi, I was testing with 2000, how can I test what version of SQL I'm programming against so I can use (MAX) when it's 2005 or above?Thanks
Jamie
SELECT @@VERSION gives me the server version, thanks all.
Jamie
See http://sqlserver2000.databases.aspfaq.com/how-do-i-know-which-version-of-sql-server-i-m-running.html to find out what version of SQL Server you're running against.
md5sum
+3  A: 

Use nvarchar(max) or varchar(max), as nText and Text are deprecated.

Oded
+3  A: 

From SQL2005 onwards, you can define VARCHAR(MAX) or NVARCHAR(MAX) which does give you all the benefits of a VARCHAR without the downsides of TEXT.

Wim Hollebrandse
+3  A: 

You can use nText for anything in unicode over 8000 characters. The size of the nText field is 1,073,741,823 characters. If you're not using unicode, and need even more space, you can use Text, which has a capacity of 2,147,483,647 characters.

However, nText and Text have been deprecated in favor of using VARCHAR(MAX), which can hold up to 2 GB of data. The VARCHAR(MAX) data type uses an overflow page for anything over 8 KB in a row, so performance may suffer if this gets too out of control.

md5sum
nText is obsolete as of SQL Server 2005. The new nvarchar(MAX) data type (and varchar(max) and varbinary(max) replace it with the following benefits: The same comparison and computations can be done as regular varchar columns, and performance is increased as values are inlined when possible instead of always being stored in a seperate location.
David
I see that... I hadn't used the nText field in years since I haven't needed to store anything that big in a database for a good while.
md5sum
A: 

FYI : exec function has limit of 8000 bytes as well example:

EXEC 'Insert into bla(col) values(''data'')'  

this will fail if the over all string sent to exec function is over 8000 bytes

to mitigate this issue use

EXEC sp_executesql @tmpUpdQuery,
               N'
               @PtmpColValue varchar(max),
               @Pident varchar(max)' ,                                          
               @tmpColValue,
               @tmpId;
Greg