Solved

Update table based on nested select from the same table

Posted on 2006-07-12
7
352 Views
Last Modified: 2006-11-18
I have a table of categories filled with some of our own data and data from an XML feed.  I have no access to the original data from the XML feed.

Data looks something like this at the moment

CategoryID    |     CatName      |    ParentID   |   XMLCode   |   XMLParent
-------------------------------------------------------------------------------------
  1                      Cars                     0                  1234              0
  2                      Bikes                    0                  9999              0
  3                      Mirrors                 0                  19673            1234
  4                      Handlebars            0                 96734            9999
  5                      Cheese                 0               <NULL>         <NULL>
  6                      French                  5               <NULL>         <NULL>

Each category can have a parent of another category so the eventual structure is a tree like listing.  The categories from the XML feed will get updated semi-regularly in case any get added or changed etc...  I need to be able to map the parentIDs for the XML categories based on the XMLCode and XMLParent.

Using the example above The ParentID For "Mirrors" would get set to "1" and the ParentID for "handlebars" would get set to "2".  These are arrived at using the XMLParent which are already present from the XML feed.  The "Cheese" and "French" records would be ignored as the XMLdata is not present.

Using this forum and other resources I have come up with the following which runs but does nothing to the data.  The table is called tbl_FPCat.

UPDATE    tblup
SET              tblup.ParentID = nested.CategoryID
FROM         tbl_FPCat AS tblup JOIN
                          (SELECT     CategoryID, XMLCode, XMLParent
                            FROM          tbl_FPCat
                            WHERE      XMLCode= XMLParent) AS nested ON tblup.XMLParent = nested.XMLCode
WHERE     (tblup.XMLCode <> NULL) OR (tblup.XMLCode <> '')

I dont even know if this is possible or even if I am on the right track at this point.  Any help greatly appreciated.  Anything not clear - just ask.

C
0
Comment
Question by:snavebelac
  • 4
  • 2
7 Comments
 
LVL 28

Expert Comment

by:imran_fast
Comment Utility
try this


update tbl_FPCat set ParentID = b.categoryid
from tbl_FPCat b
where b.XMLCode = tbl_FPCat.XMLParent
go
0
 
LVL 8

Expert Comment

by:Kobe_Lenjou
Comment Utility
UPDATE    tblup
SET              tblup.ParentID = (select CategoryID from tblup T where T.XMLParent  = tblup.xmlcode)
WHERE     (tblup.XMLCode <> NULL) OR (tblup.XMLCode <> '')
0
 
LVL 6

Author Comment

by:snavebelac
Comment Utility
thanks for the response

when I run that I get the following error

"The column prefix 'tbl_FPCat' does not match with a table name or alias name used in the query."

which is odd...  I checked the names of everything and it is all correct  any thoughts?

C
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Author Comment

by:snavebelac
Comment Utility
that last respnse was to imran_fast
0
 
LVL 6

Author Comment

by:snavebelac
Comment Utility
Kobe_Lenjou

I tried that configuration once before - I tried again and I get this error...

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated."

the XMLCode column is unique apart from the NULLs and zero length strings which are being filtered.

Any ideas ?

Thanks

C
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 500 total points
Comment Utility
try this


update a set a.ParentID = b.categoryid
from tbl_FPCat a
inner join tbl_fpcat b on
b.XMLCode = a.XMLParent
go
0
 
LVL 6

Author Comment

by:snavebelac
Comment Utility
imran_fast

That worked great - thanks - knew it had to be simple

C
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now