I'm trying to create the following table
CREATE TABLE Ingredient.Ingredient
(
GUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL,
Name NVARCHAR(MAX) NOT NULL UNIQUE
)
but I've come to realize that the max size of a NVARCHAR UNIQUE column is 450 (at least in the current version of SQL Server). In order to not use magic literals I've created a user-defined function that returns the current max size of a NVARCHAR UNIQUE column.
CREATE FUNCTION [Max NVARCHAR Index Size]()
RETURNS INTEGER
BEGIN
RETURN(450)
END
This function runs correctly when called as
SELECT dbo.[Max NVARCHAR Index Size]()
I was hoping to use this function in a CREATE TABLE statement, but it errors as shown below.
CREATE TABLE Ingredient.Ingredient
(
GUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL,
Name NVARCHAR(dbo.[Max NVARCHAR Index Size]()) NOT NULL UNIQUE
)
Error:
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '('
To try and circumvent this I made a variable with the value of the function, and then using the variable, but that didn't work either.
DECLARE
@NVARCHARIndexSize INTEGER = dbo.[MAX NVARCHAR Index Size]()
CREATE TABLE Ingredient.Ingredient
(
GUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL,
Name NVARCHAR(@NVARCHARIndexSize) NOT NULL UNIQUE
)
Error:
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '@NVARCHARIndexSize'
where line 13 is Name NVARCHAR(@NVARCHARIndexSize) NOT NULL UNIQUE.
Is there a way to use variables/functions instead of literals in a CREATE TABLE statement?
Thanks in advance.
nvarcharusing a parameter as the size.