If SQL Server USER_NAME() Is A sysname, Why Does It Return NVARCHAR(256)? -


i'm working column want hold value of user_name() function in sql server 2008 r2. in bol, function documented having return type of nvarchar(256).

however, in 1 of examples, this:

select name sysusers name = user_name(1) 

and sysusers.name column has type sysname, (as understand it) nvarchar(128) not null. inconsistency further solidified in documentation current_user, says "this function equivalent user_name()", indicates type sysname (of course, goes on have example uses current_user default varchar(30) column...)

what gives? bol wrong type of user_name()? , safe me use nvarchar(128)?

well, let's take look:

select name, system_type_id, user_type_id, max_length   sys.types name in (n'sysname', n'nvarchar'); 

results:

name      system_type_id  user_type_id  max_length --------  --------------  ------------  ---------- nvarchar  231             231           8000 sysname   231             256           256 

sysname alias type nvarchar defined number of characters of 128 (not 256). reason sys.types says 256 nvarchar has 2 bytes per character - that's 128 characters x 2 bytes = 256 bytes. not mean same thing "length" do.

there no answer reason "why" bol says - books online wrong, that's all. proof:

select x = user_name() #foo; exec tempdb..sp_help '#foo'; 

partial results:

column_name  type      length -----------  --------  ------ x            nvarchar  256  -----------------------^^^ ---- again, 128 characters 

and yes, should safe using nvarchar(128), cost match documentation, in case? stop looking @ deprecated system tables sysusers guidance on data type choices , corroborating documentation. while sys.database_principals , sys.server_principals use sysname in case, they're more reliable place check how sql server works today, unless investigating how sql server worked 13+ years ago.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -