SQL query to delete unsued templates

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
bigbangtech
Commander (CMDR)
Commander (CMDR)
Posts: 182
Joined: Mon Oct 10, 2005 6:27 pm

SQL query to delete unsued templates

Post by bigbangtech » Tue Apr 28, 2009 2:00 pm

Does anyone have a short SQL query that would find and delete any product templates that are not associated with a product?

We imported a store with 1000's of items, deleted a great deal of the items and are now stuck with 100's of templates that we need to wade through before we get to the correct one.
AC7.3

Need A Bulb? - Light bulbs for the building maintenance and construction industries

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: SQL query to delete unsued templates

Post by nickc » Tue Apr 28, 2009 3:39 pm

Code: Select all

SELECT * FROM ac_ProductTemplates
WHERE ProductTemplateId NOT IN
( SELECT DISTINCT ProductTemplateId FROM ac_InputFields where InputFieldId IN
  ( SELECT DISTINCT InputFieldId FROM ac_ProductTemplateFields )
)
Change the initial "SELECT * FROM" to "DELETE" after vetting the results of the select.
All after backing up, of course :)

bigbangtech
Commander (CMDR)
Commander (CMDR)
Posts: 182
Joined: Mon Oct 10, 2005 6:27 pm

Re: SQL query to delete unsued templates

Post by bigbangtech » Wed Apr 29, 2009 9:16 am

Thanks Nick, that worked like a charm!

I do have a lot of rows still left in ac_ProductTemplateFields that aren't connected to anything though...
nickc wrote:

Code: Select all

SELECT * FROM ac_ProductTemplates
WHERE ProductTemplateId NOT IN
( SELECT DISTINCT ProductTemplateId FROM ac_InputFields where InputFieldId IN
  ( SELECT DISTINCT InputFieldId FROM ac_ProductTemplateFields )
)
Change the initial "SELECT * FROM" to "DELETE" after vetting the results of the select.
All after backing up, of course :)
AC7.3

Need A Bulb? - Light bulbs for the building maintenance and construction industries

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: SQL query to delete unsued templates

Post by nickc » Wed Apr 29, 2009 11:37 am

Code: Select all

SELECT * FROM ac_ProductTemplateFields WHERE InputFieldId NOT IN
( SELECT DISTINCT InputFieldId FROM ac_InputFields )
Same technique to remove orphans in ac_ProductTemplateFields.

Post Reply