I SQL Server kan du bruge TYPE_NAME()
funktion til at returnere navnet på en datatype baseret på dens ID. Dette kan være nyttigt, når du forespørger i en systemvisning såsom sys.columns
der returnerer typens ID, men ikke dens navn.
Du kan bruge TYPE_NAME()
for systemdatatyper og brugerdefinerede datatyper.
Eksempel 1 – Grundlæggende brug
Her er et grundlæggende eksempel for at demonstrere, hvordan det virker.
SELECT TYPE_NAME(34) AS Result;
Resultat:
+----------+ | Result | |----------| | image | +----------+
Dette resultat fortæller os, at type-id'et 34 bruges til billedet type.
Eksempel 2 – Et mere brugbart eksempel
Her er et mere nyttigt eksempel.
USE Music; SELECT o.name AS [Object Name], c.name AS [Column Name], TYPE_NAME(c.user_type_id) AS [Type Name] FROM sys.objects AS o JOIN sys.columns AS c ON o.object_id = c.object_id WHERE o.type_desc = 'USER_TABLE';
Resultat:
+---------------+---------------+-------------+ | Object Name | Column Name | Type Name | |---------------+---------------+-------------| | Artists | ArtistId | int | | Artists | ArtistName | nvarchar | | Artists | ActiveFrom | date | | Artists | CountryId | int | | Genres | GenreId | int | | Genres | Genre | nvarchar | | Albums | AlbumId | int | | Albums | AlbumName | nvarchar | | Albums | ReleaseDate | date | | Albums | ArtistId | int | | Albums | GenreId | int | | Country | CountryId | int | | Country | CountryName | nvarchar | +---------------+---------------+-------------+
Denne forespørgsel returnerer brugertabeller sammen med deres kolonner og datatypen for hver kolonne.
Sådan ser det ud, hvis jeg fjerner TYPE_NAME()
:
USE Music; SELECT o.name AS [Object Name], c.name AS [Column Name], c.user_type_id FROM sys.objects AS o JOIN sys.columns AS c ON o.object_id = c.object_id WHERE o.type_desc = 'USER_TABLE';
Resultat:
+---------------+---------------+----------------+ | Object Name | Column Name | user_type_id | |---------------+---------------+----------------| | Artists | ArtistId | 56 | | Artists | ArtistName | 231 | | Artists | ActiveFrom | 40 | | Artists | CountryId | 56 | | Genres | GenreId | 56 | | Genres | Genre | 231 | | Albums | AlbumId | 56 | | Albums | AlbumName | 231 | | Albums | ReleaseDate | 40 | | Albums | ArtistId | 56 | | Albums | GenreId | 56 | | Country | CountryId | 56 | | Country | CountryName | 231 | +---------------+---------------+----------------+
Det er ikke så nemt at læse type-id'et.
Eksempel 3 – Brugerdefinerede typer
Brugerdefinerede typer er inkluderet. Her er et eksempel, der inkluderer et brugerdefineret typealias i resultaterne.
USE Test; SELECT o.name AS [Object Name], c.name AS [Column Name], TYPE_NAME(c.user_type_id) AS [Type Name], CASE WHEN t.is_user_defined = 1 THEN 'Yes' ELSE 'No' END AS [User Defined?] FROM sys.objects AS o JOIN sys.columns AS c ON o.object_id = c.object_id JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE o.type_desc = 'USER_TABLE' AND o.name = 'Client';
Resultat:
+---------------+---------------+-------------+-----------------+ | Object Name | Column Name | Type Name | User Defined? | |---------------+---------------+-------------+-----------------| | Client | ClientCode | clientcode | Yes | | Client | FirstName | varchar | No | | Client | LastName | varchar | No | +---------------+---------------+-------------+-----------------+
Eksempel 4 – Brug af TYPE_NAME() i en WHERE-sætning
Du kan bruge TYPE_NAME()
(og enhver anden systemfunktion) i en WHERE
klausul (og hvor som helst et udtryk er tilladt).
Her ændrer jeg det forrige eksempel, så jeg bruger TYPE_NAME()
i WHERE
klausul.
USE Test; SELECT o.name AS [Object Name], c.name AS [Column Name], TYPE_NAME(c.user_type_id) AS [Type Name], CASE WHEN t.is_user_defined = 1 THEN 'Yes' ELSE 'No' END AS [User Defined?] FROM sys.objects AS o JOIN sys.columns AS c ON o.object_id = c.object_id JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE TYPE_NAME(c.user_type_id) = 'clientcode';
Resultat:
+---------------+---------------+-------------+-----------------+ | Object Name | Column Name | Type Name | User Defined? | |---------------+---------------+-------------+-----------------| | Client | ClientCode | clientcode | Yes | +---------------+---------------+-------------+-----------------+
Eksempel 5 – Ugyldigt type-id eller utilstrækkelig tilladelse
Hvis du angiver et ugyldigt type-id, eller du ikke har tilstrækkelig tilladelse til at referere til typen, vil resultatet være NULL.
SELECT TYPE_NAME(258) AS Result;
Resultat:
+----------+ | Result | |----------| | NULL | +----------+
Få type-id'et
Hvis du allerede kender datatypens navn, men du vil have dens ID, kan du bruge TYPE_ID()
for at returnere id'et for en datatype baseret på dens navn.