Question

How to create a user defined type in SQL Server using VS2012? I tried to do the step shown in the image but no thing appears.

Also I have tried to create Type using the following code but I can not create elements of that type in my table.

CREATE TYPE CartListType AS TABLE 
(
   [ItemNo]   INT            NOT NULL,
   [NameEn]   NVARCHAR (150) NULL,
   [Price]    FLOAT (53)     NULL,
   [Quantity] INT            NULL,
   [ImageSrc] VARCHAR (100)  NULL
)
Was it helpful?

Solution

There's no concept of nested tables in SQL Server. So whilst the code you've shown is correct for declaring a user-defined table (UDT) type, what you cannot do is then use that type as the declared type of a column in another table. All you can use UDTs for are for variable and parameter declarations1.

The more relational thing to do would be to just create this as a normal table with an additional column to act as a foreign key back to the Bills table. An alternative, if you really do want to store the data "in-row" (although it'll actually be stored in separate pages physically) would be to declare the column as XML, and use the XML facilities instead to store and manipulate this data.


1 I've said something with certainty, so someone's bound to pop up and mention some third use case I've forgotten, but the general point stands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top