Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variable to specify 'size' when declaring a VARBINARY

In SQL Server (2008 R2), instead of doing this:

DECLARE @testVar VARBINARY(64);

I would like to do this:

DECLARE @varSize INT;
SET @varSize = 64;
DECLARE @testVar VARBINARY(@varSize);

But I get this error:

Incorrect syntax near '@varSize'.

How can I do something like this or force SQL to evaluate @varSize?

like image 767
pcantin Avatar asked Feb 19 '23 03:02

pcantin


1 Answers

For a variable, why don't you just use MAX?

DECLARE @testVar VARBINARY(MAX);

This isn't the 70s anymore. Your system can handle it. In fact if what you want to do were possible, I suspect you'd waste more resources doing that than you would just declaring the variable as MAX in the first place.

like image 153
Aaron Bertrand Avatar answered Feb 25 '23 12:02

Aaron Bertrand