SQL Server front-end (T-SQL)

Names and data types

Identifiers, schemas and the SQL Server data types and how each is stored.

Names#

  • [x] and "x" are identifiers; N'..' strings; 0x1F becomes X'1F'.
  • Names of tables and databases are not case sensitive in T-SQL but are in the engine: they are lower-cased for the engine. The name as written is kept (in the front-end's table __mssql_tbl) and shown by sys.tables and INFORMATION_SCHEMA.
  • Schemas: dbo.t is t. Another schema s.t becomes the table s__t (a name prefix). CREATE SCHEMA s is accepted and recorded for the session (CREATE SCHEMA of dbo, sys, guest, INFORMATION_SCHEMA is error 2714); DROP SCHEMA forgets it. db.dbo.t and db..t are the table t in the database db. A four-part name loses its server part.
  • #t is a temporary table of the session (an engine temporary table); ##t is the same (it is not visible to other sessions).

Data types#

T-SQLengine columnwire type of an answer
int, bigint, smallintsamesame
tinyinttinyint unsignedtinyint
bittinyint(1)bit
decimal(p,s), numericdecimal(p,s) (default 18,0)same
money, smallmoneydecimal(19,4), decimal(10,4)money, smallmoney
float, realdouble, floatsame
char(n), nchar(n)char(n)same (values padded to n)
`varchar(nmax), nvarchar(nmax)`
text, ntext, xmllongtextvarchar(max), nvarchar(max), xml
binary(n), `varbinary(nmax)`binary, varbinary, longblob
date, time(n)date, time(min(n,6))same
datetime, smalldatetimedatetime(3), datetime(0)same
datetime2(n)datetime(min(n,6))datetime2(n) (the 7th digit is 0)
datetimeoffsetvarchar(40)datetimeoffset (offset +00:00)
uniqueidentifierchar(36)same (upper case)
sql_variantlongtext holding type<SOH>valuesql_variant (the value with its base type)

The declared type of every column is kept by the front-end in its own tables __mssql_cols and __mssql_tbl of the database (the engine keeps no column comments), and the answers of SELECT, sys.columns and INFORMATION_SCHEMA.COLUMNS are typed from them. Tables made by another front-end are typed from the engine's types.