0

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
)
2
  • The code you've shown works fine for me. So not sure what your issue is. "I can not create elements of that type" - can you show that attempt? Commented Feb 20, 2014 at 9:48
  • This what I am trying to do >> imgur.com/iPtc4nl Commented Feb 20, 2014 at 10:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Using that type as a column wouldn't be a "nested table" - at least not in the sense as it is used in Oracle. It would be a column based on a "struct", i.e. a single value with "sub-values". A nested table (as used in Oracle) would be able to store multiple rows of that UDT. But I agree that this is better done through a 1:N relationship
@a_horse_with_no_name - it seemed to resemble Oracle's nested tables to me. And we don't know whether the OP was intending to store 0, 1 or multiple rows in this column, for each outer row, if they'd have succeeded in creating this table
you are right, after a closer look at the UDT, Hamed most probably wanted to create a "nested table".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.