What is the SQL Server SYSNAME data type for? BOL says:
The sysname data type is used for table columns, variables, and stored procedure parameters that store object names.
but I don't really get that. Is there a use-case you can provide?
What is the SQL Server SYSNAME data type for? BOL says:
The sysname data type is used for table columns, variables, and stored procedure parameters that store object names.
but I don't really get that. Is there a use-case you can provide?
sysname is used by sp_send_dbmail, a stored procedure that "Sends an e-mail message to the specified recipients" and located in the msdb database.
According to Microsoft,
If you ever have the need for creating some dynamic sql it is appropriate to use
sysname
as data type for variables holding table names, column names and server names.FWIW, you can pass a table name to useful system SP's like this, should you wish to explore a database that way :
Just as an FYI....
select * from sys.types where system_type_id = 231
gives you two rows.(i'm not sure what this means yet but i'm 100% sure it's messing up my code right now)
edit: i guess what it means is that you should join by the user_type_id in this situation (my situation) or possibly both the user_type_id and th esystem_type_id
This query:
yields:
and this:
gives you this:
Let me list a use case below. Hope it helps. Here I'm trying to find the Table Owner of the Table 'Stud_dtls' from the DB 'Students'. As Mikael mentioned, sysname could be used when there is a need for creating some dynamic sql which needs variables holding table names, column names and server names. Just thought of providing a simple example to supplement his point.
sysname
is a built in datatype limited to 128 Unicode characters that, IIRC, is used primarily to store object names when creating scripts. Its value cannot beNULL
It is basically the same as using
nvarchar(128) NOT NULL
EDIT
As mentioned by @Jim in the comments, I don't think there is really a business case where you would use
sysname
to be honest. It is mainly used by Microsoft when building the internalsys
tables and stored procedures etc within SQL Server.For example, by executing
Exec sp_help 'sys.tables'
you will see that the columnname
is defined assysname
this is because the value of this is actually an object in itself (a table)I would worry too much about it.
It's also worth noting that for those people still using SQL Server 6.5 and lower (are there still people using it?) the built in type of
sysname
is the equivalent ofvarchar(30)
Documentation
sysname
is defined with the documentation fornchar
andnvarchar
, in the remarks section:To clarify the above remarks, by default sysname is defined as
NOT NULL
it is certainly possible to define it as nullable. It is also important to note that the exact definition can vary between instances of SQL Server.Using Special Data Types