Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the biggest data type to use as a local variable in a stored procedure? [closed]

I have the next issue:

--DECLARE @TEST NVARCHAR(MAX)
--DECLARE @TEST2 NVARCHAR(MAX)

DECLARE @TEST NTEXT
DECLARE @TEST2 NTEXT

NVARCHAR(MAX) is to small for the amount of text in need to put when executing a stored procedure, also, TEXT, NTEXT and IMAGE data types are invalid for local variables, what can I do to sidestep this issue and store the oversized text like.

Thanks in advance

like image 991
Tudoran Bogdan Avatar asked Oct 30 '12 09:10

Tudoran Bogdan


People also ask

How do you DECLARE a variable in SQL stored procedure?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

What is a reason for using local variables in a query?

Local variables are useful when you only need that data within a particular expression. For example, if you need to reuse a calculated value in multiple places within a single expression, you can store that in a local variable.

What is char data type in SQL?

CHAR(size) A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters - can be from 0 to 255. Default is 1. VARCHAR(size)


1 Answers

NVARCHAR(MAX) is to small for the amount of text in need to put when executing a stored procedure

Well, bad news: this is the largest data type available! 2GB of storage, there just isn't anything that can hold more than that. In fact all large types have the same size: VARCHAR(MAX), NVARCHAR(MAX), VARBINARY(MAX): they all have 2GB max size (As a side note the deprecated legacy types have exactly the same max size). Only FILESTREAM can exceed this size, but you cannot declare a variable as FILESTREAM.

So this really begs the question: what the heck are you doing in a stored procedure to add +2GB of data in a variable? You cannot possible have a justified reason for this, so you should reconsider your approach. Use the disk, Luke, not the RAM! Consider a @table variable or a #temp table...

like image 76
Remus Rusanu Avatar answered Sep 24 '22 17:09

Remus Rusanu