insert into table with self referential key

Orders

OrderId (Int identity) ,    AssociatedOrderId (foreign key to OrderId, allows null) ,   OrderName, ......

I need to insert into the above table from a tvp. How do i map the AssociatedOrderId during the insert?

February 14th, 2014 4:19pm

I answered a similar question earlier today:
http://social.Msdn.microsoft.com/Forums/en-US/transactsql/thread/0430e4c1-eeb3-4a4c-b008-308b5795435b#43be232a-09c2-476f-b3c7-2f88c4dd0b76

The gist is that as long as you insist on using IDENTITY, you need to take them one by one, or at least add the associate keys later. If you roll your own, you have more control.

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2014 6:40pm

Thanks for trying to post DDL, but you have no idea what a table is. IDENTITY is a table property that counts the physical insertion attempt to a file on a disk. It has absolutely nothing to do with a valid data model. 

A key cannot be NULL-able, so your narrative cannot ever be a table

Again, back to basics. A table models either an entity or a relationship, never both. 

This is an entity: 

CREATE TABLE Orders
(order_nbr CHAR(15) NOT NULL PRIMARY KEY,
 order_name VARCHAR(25) NOT NULL);

This is a relationship. But thanks to the lack of specs, we have no idea if it is 1:1, 1:m or n:m. I will guess, based on nothing you posted 1:m

CREATE TABLE Associated_Orders
(main_order_nbr CHAR(15) NOT NULL
 REFERENCES Orders (order_nbr)
 ON DELETE CASACDE
 ON UPDATE CASACDE, 
 associated_order_nbr CHAR(15) NOT NULL PRIMARY KEY
 REFERENCES Orders (order_nbr)
 ON DELETE CASACDE
 ON UPDATE CASACDE, 

>> I need to insert into the above table from a tvp. How do i map theAssociatedOrderId during the insert? <<

Would like a lecture on why we do not use TVPs? 
February 14th, 2014 9:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics