November 4, 2013 at 1:36 am
I need help here, I have this table, I may need to create another column I'm not sure yet.
CREATE TABLE [dbo].[Location](
[LocationID] [int] IDENTITY(1,1) NOT NULL,
[Location] [nvarchar](255) NOT NULL,
[Product_Code] [nvarchar](255) NOT NULL,
[LocationProductStockAmt] [decimal](18, 2) NULL)
And I need to create a database object that can store hierarchical linking of locations to each other. Ensure referential integrity is enforced.
One location cannot be linked to more than one parent location.
For example if I have location South Africa and I want to link Gauteng and to Gauteng I want to link Johannesburg. South Africa => Gauteng => Johannesburg
November 4, 2013 at 1:55 am
Sounds like HierarchyID is exactly what you are after
November 4, 2013 at 2:24 am
I'll look at your link thank you
November 4, 2013 at 2:26 am
Edited, try that again now...
November 4, 2013 at 2:32 am
Dave Ballantyne (11/4/2013)
Edited, try that again now...
I get that, but for my table, I have primary key LocationID, then Location which has:
Johannesburg
Pretoria
East London
Cape Town
Bloemfontein
Rustenburg
Cape Town
Polokwane
Kimberly
Welkom
Now this is what I need Gauteng => Johannesburg in my hierarchy. how do I achieve that
November 4, 2013 at 2:49 am
Now this is what I need Gauteng => Johannesburg in my hierarchy. how do I achieve that
place a parentID column ... following will be the values in the ParentID, if you look closely it will give you tree view (HirerchyID).
ID Location ParentID
1 South Africa 0
2 Gauteng 1
3 Johannesburg 2
hope it helps
November 4, 2013 at 6:40 am
twin.devil (11/4/2013)
Now this is what I need Gauteng => Johannesburg in my hierarchy. how do I achieve that
place a parentID column ... following will be the values in the ParentID, if you look closely it will give you tree view (HirerchyID).
ID Location ParentID
1 South Africa 0
2 Gauteng 1
3 Johannesburg 2
hope it helps
Yes it does, but I'm thinking about my data, this row for instead:
ID Location Product_Code LocationProductStockAmt
1 Johannesburg 1231 4000
will be:
INSERT INTO [dbo].[Location] VALUES ('Johannesburg','1231', 4000)
now how will I insert 'South Africa' and 'Gauteng' and still maintain my values,
because now we have:
ID Location ParentID LocationProductStockAmt
1 South Africa 0 4000
2 Gauteng 1 4000
3 Johannesburg 2 4000
how do we deal with that.
November 4, 2013 at 8:07 am
How many rows will this table eventually have and how often will it need to take new inserts or updates?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 4, 2013 at 11:38 pm
Jeff Moden (11/4/2013)
How many rows will this table eventually have and how often will it need to take new inserts or updates?
1. As many as it could
2. Not very often
November 5, 2013 at 12:18 am
hoseam (11/4/2013)
Jeff Moden (11/4/2013)
How many rows will this table eventually have and how often will it need to take new inserts or updates?1. As many as it could
2. Not very often
BWAAA-HAAA!!!! ... OK... in that case, please see the following articles. And, it's easy to maintain and troubleshoot because the Adjacency List is what you use to maintain it. It's fast too. Both methods will convert a million node hierarchy in about 54 seconds on any decent box now-a-days.
http://www.sqlservercentral.com/articles/Hierarchy/94040/
http://www.sqlservercentral.com/articles/T-SQL/94570/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply