Di SQL Server, Anda dapat menggunakan TYPEPROPERTY()
berfungsi untuk mengembalikan informasi tentang tipe data.
Anda memberikan nama tipe data, serta properti yang ingin Anda kembalikan, dan itu mengembalikan informasi tentang properti itu untuk tipe data yang diberikan.
Anda dapat menggunakan TYPEPROPERTY()
untuk tipe data sistem dan tipe data yang ditentukan pengguna.
Sintaks
Sintaksnya seperti ini:
TYPEPROPERTY (type , property)
Dimana ketik adalah tipe data, dan properti adalah properti tertentu yang Anda inginkan informasinya.
Ada 5 kemungkinan properti:AllowsNull , Id Pemilik , Presisi , Skala , dan MenggunakanAnsiTrim .
Untuk informasi lebih detail tentang properti ini dan cara kerjanya dengan fungsi ini, lihat dokumentasi Microsoft.
Contoh 1 – Penggunaan Dasar
Berikut adalah contoh dasar untuk mendemonstrasikan cara kerjanya.
SELECT TYPEPROPERTY('int', 'Precision') AS Result;
Hasil:
+----------+ | Result | |----------| | 10 | +----------+
Hasil ini memberi tahu kita bahwa int tipe data memiliki presisi 10.
Contoh 2 – Semua Properti
Berikut adalah contoh yang mengembalikan semua properti untuk datetime2 tipe data.
SELECT TYPEPROPERTY( 'datetime2', 'AllowsNull') AS [AllowsNull], TYPEPROPERTY( 'datetime2', 'OwnerId') AS [OwnerId], TYPEPROPERTY( 'datetime2', 'Precision') AS [Precision], TYPEPROPERTY( 'datetime2', 'Scale') AS [Scale], TYPEPROPERTY( 'datetime2', 'UsesAnsiTrim') AS [UsesAnsiTrim];
Hasil:
+--------------+-----------+-------------+---------+----------------+ | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |--------------+-----------+-------------+---------+----------------| | 1 | 4 | 27 | 7 | NULL | +--------------+-----------+-------------+---------+----------------+
Alasan UsesAnsiTrim
is NULL karena properti itu hanya berlaku untuk tipe data biner dan string.
Untuk mendemonstrasikan, ini dia lagi, kecuali menggunakan tipe data string:
SELECT TYPEPROPERTY( 'varchar', 'AllowsNull') AS [AllowsNull], TYPEPROPERTY( 'varchar', 'OwnerId') AS [OwnerId], TYPEPROPERTY( 'varchar', 'Precision') AS [Precision], TYPEPROPERTY( 'varchar', 'Scale') AS [Scale], TYPEPROPERTY( 'varchar', 'UsesAnsiTrim') AS [UsesAnsiTrim];
Hasil:
+--------------+-----------+-------------+---------+----------------+ | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |--------------+-----------+-------------+---------+----------------| | 1 | 4 | 8000 | NULL | 1 | +--------------+-----------+-------------+---------+----------------+
Kali ini Scale
adalah NULL. Itu karena properti ini hanya berlaku untuk numerik dan jenis tanggal tertentu.
Contoh 3 – Menentukan Skala atau Presisi
Beberapa tipe data memungkinkan Anda untuk menentukan skala atau presisi, tetapi TYPEPROPERTY()
tidak akan menerimanya:
SELECT TYPEPROPERTY( 'datetime2(3)', 'Scale') AS [Datetime2 Scale], TYPEPROPERTY( 'datetime2(3)', 'Precision') AS [Datetime2 Precision], TYPEPROPERTY( 'varchar(255)', 'Precision') AS [Varchar Precision];
Hasil:
+-------------------+-----------------------+---------------------+ | Datetime2 Scale | Datetime2 Precision | Varchar Precision | |-------------------+-----------------------+---------------------| | NULL | NULL | NULL | +-------------------+-----------------------+---------------------+
Contoh 4 – Tipe Data Buatan Pengguna
TYPEPROPERTY()
fungsi juga bekerja pada tipe data yang ditentukan pengguna.
Ini contohnya.
SELECT TYPEPROPERTY( 'clientcode', 'AllowsNull') AS [AllowsNull], TYPEPROPERTY( 'clientcode', 'OwnerId') AS [OwnerId], TYPEPROPERTY( 'clientcode', 'Precision') AS [Precision], TYPEPROPERTY( 'clientcode', 'Scale') AS [Scale], TYPEPROPERTY( 'clientcode', 'UsesAnsiTrim') AS [UsesAnsiTrim];
Hasil:
+--------------+-----------+-------------+---------+----------------+ | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |--------------+-----------+-------------+---------+----------------| | 0 | 1 | 8 | NULL | 1 | +--------------+-----------+-------------+---------+----------------+
Contoh 5 – Menyertakan Awalan Skema Tipe Data
Anda juga dapat mengawali tipe data dengan nama skema.
SELECT TYPEPROPERTY( 'sys.int', 'AllowsNull') AS [AllowsNull], TYPEPROPERTY( 'sys.int', 'OwnerId') AS [OwnerId], TYPEPROPERTY( 'sys.int', 'Precision') AS [Precision], TYPEPROPERTY( 'sys.int', 'Scale') AS [Scale], TYPEPROPERTY( 'sys.int', 'UsesAnsiTrim') AS [UsesAnsiTrim];
Hasil:
+--------------+-----------+-------------+---------+----------------+ | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |--------------+-----------+-------------+---------+----------------| | 1 | 4 | 10 | 0 | NULL | +--------------+-----------+-------------+---------+----------------+
Contoh 6 – Mengembalikan Info untuk Semua Tipe Data di Database
Berikut adalah contoh yang mengembalikan informasi properti untuk semua tipe data dalam database saat ini.
Untuk melakukan ini, saya meminta sys.types
tampilan sistem. Saya juga menggunakan SCHEMA_NAME()
berfungsi untuk mendapatkan nama skema berdasarkan ID-nya.
SELECT SCHEMA_NAME(schema_id) AS [Schema], name AS [Data Type], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'AllowsNull') AS [AllowsNull], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'OwnerId') AS [OwnerId], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'Precision') AS [Precision], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'Scale') AS [Scale], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'UsesAnsiTrim') AS [UsesAnsiTrim] FROM sys.types ORDER BY name;
Hasil:
+----------+------------------+--------------+-----------+-------------+---------+----------------+ | Schema | Data Type | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |----------+------------------+--------------+-----------+-------------+---------+----------------| | sys | bigint | 1 | 4 | 19 | 0 | NULL | | sys | binary | 1 | 4 | 8000 | NULL | 1 | | sys | bit | 1 | 4 | 1 | NULL | NULL | | sys | char | 1 | 4 | 8000 | NULL | 1 | | dbo | clientcode | 0 | 1 | 8 | NULL | 1 | | sys | date | 1 | 4 | 10 | 0 | NULL | | sys | datetime | 1 | 4 | 23 | 3 | NULL | | sys | datetime2 | 1 | 4 | 27 | 7 | NULL | | sys | datetimeoffset | 1 | 4 | 34 | 7 | NULL | | sys | decimal | 1 | 4 | 38 | 38 | NULL | | sys | float | 1 | 4 | 53 | NULL | NULL | | sys | geography | 1 | 4 | -1 | NULL | NULL | | sys | geometry | 1 | 4 | -1 | NULL | NULL | | sys | hierarchyid | 1 | 4 | 892 | NULL | NULL | | sys | image | 1 | 4 | 2147483647 | NULL | NULL | | sys | int | 1 | 4 | 10 | 0 | NULL | | sys | money | 1 | 4 | 19 | 4 | NULL | | sys | nchar | 1 | 4 | 4000 | NULL | NULL | | sys | ntext | 1 | 4 | 1073741823 | NULL | NULL | | sys | numeric | 1 | 4 | 38 | 38 | NULL | | sys | nvarchar | 1 | 4 | 4000 | NULL | NULL | | sys | real | 1 | 4 | 24 | NULL | NULL | | sys | smalldatetime | 1 | 4 | 16 | 0 | NULL | | sys | smallint | 1 | 4 | 5 | 0 | NULL | | sys | smallmoney | 1 | 4 | 10 | 4 | NULL | | sys | sql_variant | 1 | 4 | 0 | NULL | 1 | | sys | sysname | 0 | 4 | 128 | NULL | NULL | | sys | text | 1 | 4 | 2147483647 | NULL | NULL | | sys | time | 1 | 4 | 16 | 7 | NULL | | sys | timestamp | 0 | 4 | 8 | NULL | NULL | | sys | tinyint | 1 | 4 | 3 | 0 | NULL | | sys | uniqueidentifier | 1 | 4 | 16 | NULL | NULL | | sys | varbinary | 1 | 4 | 8000 | NULL | 1 | | sys | varchar | 1 | 4 | 8000 | NULL | 1 | | sys | xml | 1 | 4 | -1 | NULL | NULL | +----------+------------------+--------------+-----------+-------------+---------+----------------+
Meskipun perlu diingat bahwa sys.types sudah berisi presisi dan skala, jadi Anda juga bisa melakukan ini:
SELECT SCHEMA_NAME(schema_id) AS [Schema], name AS [Data Type], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'AllowsNull') AS [AllowsNull], TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'OwnerId') AS [OwnerId], Precision, Scale, TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'UsesAnsiTrim') AS [UsesAnsiTrim] FROM sys.types ORDER BY name;
Hasil:
+----------+------------------+--------------+-----------+-------------+---------+----------------+ | Schema | Data Type | AllowsNull | OwnerId | Precision | Scale | UsesAnsiTrim | |----------+------------------+--------------+-----------+-------------+---------+----------------| | sys | bigint | 1 | 4 | 19 | 0 | NULL | | sys | binary | 1 | 4 | 0 | 0 | 1 | | sys | bit | 1 | 4 | 1 | 0 | NULL | | sys | char | 1 | 4 | 0 | 0 | 1 | | dbo | clientcode | 0 | 1 | 0 | 0 | 1 | | sys | date | 1 | 4 | 10 | 0 | NULL | | sys | datetime | 1 | 4 | 23 | 3 | NULL | | sys | datetime2 | 1 | 4 | 27 | 7 | NULL | | sys | datetimeoffset | 1 | 4 | 34 | 7 | NULL | | sys | decimal | 1 | 4 | 38 | 38 | NULL | | sys | float | 1 | 4 | 53 | 0 | NULL | | sys | geography | 1 | 4 | 0 | 0 | NULL | | sys | geometry | 1 | 4 | 0 | 0 | NULL | | sys | hierarchyid | 1 | 4 | 0 | 0 | NULL | | sys | image | 1 | 4 | 0 | 0 | NULL | | sys | int | 1 | 4 | 10 | 0 | NULL | | sys | money | 1 | 4 | 19 | 4 | NULL | | sys | nchar | 1 | 4 | 0 | 0 | NULL | | sys | ntext | 1 | 4 | 0 | 0 | NULL | | sys | numeric | 1 | 4 | 38 | 38 | NULL | | sys | nvarchar | 1 | 4 | 0 | 0 | NULL | | sys | real | 1 | 4 | 24 | 0 | NULL | | sys | smalldatetime | 1 | 4 | 16 | 0 | NULL | | sys | smallint | 1 | 4 | 5 | 0 | NULL | | sys | smallmoney | 1 | 4 | 10 | 4 | NULL | | sys | sql_variant | 1 | 4 | 0 | 0 | 1 | | sys | sysname | 0 | 4 | 0 | 0 | NULL | | sys | text | 1 | 4 | 0 | 0 | NULL | | sys | time | 1 | 4 | 16 | 7 | NULL | | sys | timestamp | 0 | 4 | 0 | 0 | NULL | | sys | tinyint | 1 | 4 | 3 | 0 | NULL | | sys | uniqueidentifier | 1 | 4 | 0 | 0 | NULL | | sys | varbinary | 1 | 4 | 0 | 0 | 1 | | sys | varchar | 1 | 4 | 0 | 0 | 1 | | sys | xml | 1 | 4 | 0 | 0 | NULL | +----------+------------------+--------------+-----------+-------------+---------+----------------+