Recently i have read about UDT. i have created a type but i have a problem with that one. please look into the following
---drop type ssn
CREATE TYPE ssn
FROM VARCHAR(11) NOT NULL;
DECLARE @er ssn;
IF Object_id('TEMPDB.DBO.#ter', 'U') IS NOT NULL
DROP TABLE #ter;
CREATE TABLE #ter (
PERIOD_SID INT
,PERIOD_QUAR VARCHAR(10) PRIMARY KEY (PERIOD_SID)
)
INSERT INTO #ter (
PERIOD_SID
,PERIOD_QUAR
)
SELECT *
FROM (
VALUES (
(1)
,(@er)
)
) V(p, q)
I have create a type ssn with varchar(11) not null, and ran the above one logic, it execute successfully
As per my assumption it should throw an error.
I need to know why the above logic run successfully.
EDIT
as per suggestion i have added this udt as a column in AQL server , since in oracle we can create a column with collections similar to UDT
IF Object_id('TEMPDB.DBO.#ter1', 'U') IS NOT NULL
DROP TABLE #ter1;
CREATE TABLE #ter1 (
PERIOD_SID INT
,PERIOD_QUAR ssn PRIMARY KEY (PERIOD_SID)
)
An error was encounter while creating the table saying there was no such datatype "ssn"
Thanks in advance
Here is the reason
The null_type parameter only defines the default nullability for this data type. If nullability is explicitly defined when the alias data type is used during table creation, it takes precedence over the defined nullability.
This is taken from sp_addtype
but it should be the same case with CREATE TYPE
. sp_addtype
is also used to create user defined data type
In your case, we cannot create a variable which will not accept NOT NULL
values. So I think the property is overridden
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With