I have a stored procedure that takes a string of a pipe delimited array of IDs and parses them out. I want to have this happen in a transaction, so don't want to pass them in one at a time.
If I use varchar(max) as to not limit the size of the argument passed in, will this cause problems? I don't see the limit being hit, but I also don't want to guess or place an arbitrary limit on the string.
CREATE PROCEDURE myProc
@IDs varchar(max)
AS
BEGIN
...
END
GO
CHAR, VARCHAR, and VARCHAR(MAX) String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column. If you have huge strings (over 8,000 bytes), then VARCHAR(MAX) should be used. In order to store VARCHAR columns, the length information along with the data is stored.
The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.
varchar [ ( n | max ) ] Use n to define the string size in bytes and can be a value from 1 through 8,000, or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
There's not much to it. varchar(max)
behaves just like any varchar less than 8000 characters until you go above 8000 characters. There should be little to no difference between varchar(200)
and varchar(max)
if the actual data is less than 8000 characters. If you're expecting smaller inputs but can't rule out bigger inputs, a varchar(max)
is great.
I don't know if it would cause problems but I prefer to pass what amount to multi-element 'arrays' in to sprocs using XML. It makes it clearer what kind of data it is and there are nice SQL-XML tools to 'transform' the XML into a pseudo-table you can join against. Then, the translation from pipe-delimited -> XML can happen outside the DB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With