Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instead of throwing an error type block executed successfully

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 enter image description here

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)
    )

enter image description here

An error was encounter while creating the table saying there was no such datatype "ssn"

Thanks in advance

like image 731
Smart003 Avatar asked Jun 12 '17 06:06

Smart003


1 Answers

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

like image 131
Pரதீப் Avatar answered Nov 15 '22 09:11

Pரதீப்