is it possible to use an int variable to set the size of another variable in sql?

There is no need to worried for performance. The NVARCHAR(x) will consume only the space it needs to hold the value.

You can use dynamic T-SQL statement like this (but there is no point):

DECLARE @asdf int
SET @asdf = 5

DECLARE @DynamicTSQL NVARCHAR(MAX) = N'
DECLARE @qwer nvarchar('+CAST(@asdf AS VARCHAR(12))+')
SET @qwer = ''1234567890'';
SELECT @qwer
';

EXEC sp_executesql @DynamicTSQL

You can store the value in sql_variant column. It basically allows to store different types in one column. Once stored, you can cast them to the target type and perform appropriate operations depending it.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top